Skip to content

Commit 7a3ee0e

Browse files
authored
Merge pull request #134 from Countly/http-client-example
Http client example
2 parents b6aa925 + e9465ef commit 7a3ee0e

File tree

1 file changed

+113
-1
lines changed

1 file changed

+113
-1
lines changed

examples/example_integration.cpp

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,115 @@ void printLog(LogLevel level, const string &msg) {
3030
cout << lvl << msg << endl;
3131
}
3232

33+
// // Callback function to write response data
34+
// static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
35+
// ((std::string*)userp)->append((char*)contents, size * nmemb);
36+
// return size * nmemb;
37+
// }
38+
39+
// // Custom HTTP client for macOS
40+
// HTTPResponse customClient(bool use_post, const std::string &path, const std::string &data) {
41+
// HTTPResponse response;
42+
// response.success = false;
43+
// cout << "Making real HTTP request to: " << path << endl;
44+
45+
// CURL *curl;
46+
// CURLcode res;
47+
// std::string readBuffer;
48+
49+
// curl = curl_easy_init();
50+
// if(curl) {
51+
// // Set URL
52+
// curl_easy_setopt(curl, CURLOPT_URL, path.c_str());
53+
54+
// // Set callback function to write data
55+
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
56+
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
57+
58+
// // Set timeout
59+
// curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
60+
61+
// // Follow redirects
62+
// curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
63+
64+
// // SSL verification (set to 0 for testing, 1 for production)
65+
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
66+
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
67+
68+
// // Set User-Agent
69+
// curl_easy_setopt(curl, CURLOPT_USERAGENT, "Countly-SDK-CPP/1.0");
70+
71+
// if (use_post) {
72+
// // Set POST method
73+
// curl_easy_setopt(curl, CURLOPT_POST, 1L);
74+
75+
// if (!data.empty()) {
76+
// // Set POST data
77+
// curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
78+
// curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
79+
// }
80+
81+
// // Set content type for POST
82+
// struct curl_slist *headers = NULL;
83+
// headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
84+
// curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
85+
// } else {
86+
// // For GET requests, append data as query parameters
87+
// if (!data.empty()) {
88+
// std::string fullUrl = path;
89+
// fullUrl += (path.find('?') != std::string::npos) ? "&" : "?";
90+
// fullUrl += data;
91+
// curl_easy_setopt(curl, CURLOPT_URL, fullUrl.c_str());
92+
// }
93+
// }
94+
95+
// // Perform the request
96+
// res = curl_easy_perform(curl);
97+
98+
// if(res != CURLE_OK) {
99+
// cout << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
100+
// } else {
101+
// // Get response code
102+
// long response_code;
103+
// curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
104+
105+
// cout << "HTTP Response Code: " << response_code << endl;
106+
// cout << "Raw response: " << readBuffer << endl;
107+
108+
// // Check if HTTP request was successful (2xx status codes)
109+
// if (response_code >= 200 && response_code < 300) {
110+
// // Check if response contains { result: 'Success' } or {"result":"Success"}
111+
// if (readBuffer.find("\"result\"") != std::string::npos &&
112+
// (readBuffer.find("\"Success\"") != std::string::npos ||
113+
// readBuffer.find("'Success'") != std::string::npos)) {
114+
// response.success = true;
115+
// cout << "Success response detected!" << endl;
116+
// } else {
117+
// cout << "Response does not indicate success" << endl;
118+
// }
119+
120+
// // Parse as JSON
121+
// try {
122+
// response.data = nlohmann::json::parse(readBuffer);
123+
// } catch (const std::exception& e) {
124+
// cout << "Failed to parse JSON response: " << e.what() << endl;
125+
// response.data = nlohmann::json::object();
126+
// }
127+
// } else {
128+
// cout << "HTTP request failed with code: " << response_code << endl;
129+
// }
130+
// }
131+
132+
// // Cleanup
133+
// curl_easy_cleanup(curl);
134+
// } else {
135+
// cout << "Failed to initialize curl" << endl;
136+
// }
137+
138+
// cout << "Request completed. Success: " << (response.success ? "true" : "false") << endl;
139+
// return response;
140+
// }
141+
33142
int main() {
34143
cout << "Sample App" << endl;
35144
Countly &ct = Countly::getInstance();
@@ -38,7 +147,10 @@ int main() {
38147
// Please refer to the documentation for more information:
39148
// https://support.count.ly/hc/en-us/articles/4416163384857-C-
40149

41-
ct.alwaysUsePost(true);
150+
// Custom HTTP client
151+
// HTTPClientFunction clientPtr = customClient;
152+
// ct.setHTTPClient(clientPtr);
153+
// ct.alwaysUsePost(true);
42154
ct.setLogger(printLog);
43155
ct.SetPath("databaseFileName.db"); // this will be only built into account if the correct configurations are set
44156
ct.setDeviceID("test-device-id");

0 commit comments

Comments
 (0)