Skip to content

Commit e9465ef

Browse files
authored
Update example_integration.cpp
1 parent ac5f2c7 commit e9465ef

File tree

1 file changed

+102
-15
lines changed

1 file changed

+102
-15
lines changed

examples/example_integration.cpp

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include <chrono>
33
#include <iostream>
44
#include <random>
5-
#include <string>
65
#include <thread>
6+
77
using namespace std;
88
using namespace cly;
99

@@ -30,24 +30,112 @@ void printLog(LogLevel level, const string &msg) {
3030
cout << lvl << msg << endl;
3131
}
3232

33-
// // Custom HTTP client
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
3440
// HTTPResponse customClient(bool use_post, const std::string &path, const std::string &data) {
3541
// HTTPResponse response;
3642
// response.success = false;
37-
// cout << "Will send the request!!" << endl;
43+
// cout << "Making real HTTP request to: " << path << endl;
3844

39-
// // asynchronous operation
40-
// std::thread requestThread([&response]() {
41-
// std::this_thread::sleep_for(std::chrono::seconds(2)); // network delay
42-
// // you should add extra logic to check if request is successful normally
43-
// response.success = true;
44-
// response.data = R"({"some":"data"})"; // response data
45-
// });
45+
// CURL *curl;
46+
// CURLcode res;
47+
// std::string readBuffer;
4648

47-
// // Wait for the request to complete
48-
// requestThread.join();
49-
// cout << "Got RESPONSE:[" + response.data.dump() + "]" << endl;
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+
// }
50137

138+
// cout << "Request completed. Success: " << (response.success ? "true" : "false") << endl;
51139
// return response;
52140
// }
53141

@@ -63,13 +151,12 @@ int main() {
63151
// HTTPClientFunction clientPtr = customClient;
64152
// ct.setHTTPClient(clientPtr);
65153
// ct.alwaysUsePost(true);
66-
67154
ct.setLogger(printLog);
68155
ct.SetPath("databaseFileName.db"); // this will be only built into account if the correct configurations are set
69156
ct.setDeviceID("test-device-id");
70157
// ct.setSalt("test-salt");
71158
// OS, OS_version, device, resolution, carrier, app_version);
72-
// ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
159+
ct.SetMetrics("Windows 10", "10.22", "Mac", "800x600", "Carrier", "1.0");
73160

74161
// start the SDK (initialize the SDK)
75162
string _appKey = "YOUR_APP_KEY";

0 commit comments

Comments
 (0)