-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl_functions.cpp
99 lines (81 loc) · 3.07 KB
/
curl_functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "curl_function.h"
#include <iostream>
#include <curl/curl.h>
#include <string>
using namespace std;
// Callback function for cURL to handle the response
size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* output) {
size_t totalSize = size * nmemb;
output->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
string get_price(const string& ticker_symbol, const string& api) {
CURL* curl = curl_easy_init();
if (!curl) {
cerr << "Failed to initialize cURL." << endl;
return "";
}
// Set up the URL
string url = "https://api.twelvedata.com/price?symbol=" + ticker_symbol + "&apikey=" + api;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
// Create a string to store the JSON response
string jsonResponse;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &jsonResponse);
// Perform the HTTP request
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "cURL request failed: " << curl_easy_strerror(res) << endl;
return "";
}
// Clean up cURL
curl_easy_cleanup(curl);
// Parse the JSON data using JsonCpp
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();
Json::Value root;
string errors;
if (!reader->parse(jsonResponse.c_str(), jsonResponse.c_str() + jsonResponse.size(), &root, &errors)) {
cerr << "Failed to parse JSON: " << errors << endl;
delete reader;
return "";
}
delete reader;
// Extract and return the price
string price = root["price"].asString();
return price;
}
Json::Value get_stock_quote(const string& ticker_symbol, const string& api) {
CURL* curl = curl_easy_init();
if (!curl) {
cerr << "Failed to initialize cURL." << endl;
return Json::Value(); // Return an empty JSON value on error
}
// Set up the URL
string url = "https://api.twelvedata.com/quote?symbol=" + ticker_symbol + "&apikey=" + api;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
// Create a string to store the JSON response
string jsonResponse;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &jsonResponse);
// Perform the HTTP request
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "cURL request failed: " << curl_easy_strerror(res) << endl;
return Json::Value(); // Return an empty JSON value on error
}
// Clean up cURL
curl_easy_cleanup(curl);
// Parse the JSON data using JsonCpp
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();
Json::Value root;
string errors;
if (!reader->parse(jsonResponse.c_str(), jsonResponse.c_str() + jsonResponse.size(), &root, &errors)) {
cerr << "Failed to parse JSON: " << errors << endl;
delete reader;
return Json::Value(); // Return an empty JSON value on error
}
delete reader;
return root;
}