-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiClientSimple.cpp
167 lines (142 loc) · 5.15 KB
/
apiClientSimple.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Created by mykkode on 19.01.2019.
//
#include "apiClientSimple.hpp"
#include <sstream>
size_t apiClientSimple::parseHeaderElement(char *buffer, size_t size, size_t nitems, void *userdata) {
auto * theResponse = (apiClientResponse *) userdata;
size_t length;
length = nitems * size;
char * target;
target = (char *)memchr( buffer, ':', nitems);
if(target != nullptr){
*target = 0;
if(target + 2 < buffer + length -1) {
target += 2;
}
theResponse -> headerInsert(buffer, target);
}
else {
target = (char *)memchr( buffer, ' ', nitems);
if(target != nullptr) {
int httpCode = (target[1] -'0') * 100 + (target[2] -'0')*10 + (target[3] -'0');
theResponse -> headerInsert("HTTP", std::to_string(httpCode ) + " ");
}
}
return length;
}
size_t apiClientSimple::writeResponse(char *ptr, size_t size, size_t nmemb, void *userdata) {
auto * theResponse = (apiClientResponse *) userdata;
size_t length;
length = nmemb * size;
theResponse->appendResponse(ptr, length);
return length;
}
bool apiClientSimple::initialized = false;
apiClientSimple::apiClientSimple() {
if(!initialized){
throw(apiClientException(apiClientException::API_CLIENT_EXCEPTION_NOT_INITIALIZED));
}
handler = curl_easy_init();
if(handler == nullptr){
throw(apiClientException(apiClientException::API_CLIENT_EXCEPTION_ERROR_INITIALIZING));
}
curl_easy_setopt(handler, CURLOPT_HEADERFUNCTION, apiClientSimple::parseHeaderElement);
curl_easy_setopt(handler, CURLOPT_WRITEFUNCTION, writeResponse);
}
void apiClientSimple::initialize() {
apiClientException::initialize();
if(!initialized){
curl_global_init(CURL_GLOBAL_ALL);
initialized = true;
}
}
void apiClientSimple::deinitialize() {
initialized = false;
curl_global_cleanup();
}
void apiClientSimple::setUrl(std::string newUrl) {
url = newUrl;
std::string::iterator it = url.end() - 1;
if(*it == '/'){
url.pop_back();
}
curl_easy_setopt(handler, CURLOPT_URL, (url + endpoint) . c_str());
}
void apiClientSimple::setEndpoint(const std::string newEndpoint) {
endpoint = newEndpoint;
std::string::iterator it = endpoint.begin();
if(*it != '/'){
endpoint.insert(it, '/');
}
curl_easy_setopt(handler, CURLOPT_URL, (url + endpoint) . c_str());
}
void apiClientSimple::modifyHeader(const std::string newHeader[], int numberOfHeaders){
struct curl_slist *headers = nullptr;
for(int i = 0; i<numberOfHeaders; i++){
headers = curl_slist_append(headers, (newHeader[i]).c_str());
}
}
void apiClientSimple::reset() {
curl_easy_reset(handler);
}
apiClientResponse * apiClientSimple::apiDelete() {
CURLcode errCode;
auto theResponse = new apiClientResponse();
curl_easy_setopt(handler, CURLOPT_HEADERDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_WRITEDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_CUSTOMREQUEST, "DELETE");
errCode = curl_easy_perform(handler);
if(errCode != CURLE_OK) {
throw (apiClientException(apiClientException::API_CLIENT_EXCEPTION_WHILE_PERFORMING));
}
return theResponse;
}
apiClientResponse * apiClientSimple::apiGet() {
CURLcode errCode;
auto theResponse = new apiClientResponse();
curl_easy_setopt(handler, CURLOPT_HEADERDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_WRITEDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_HTTPGET, 1L);
errCode = curl_easy_perform(handler);
if(errCode != CURLE_OK) {
throw (apiClientException(apiClientException::API_CLIENT_EXCEPTION_WHILE_PERFORMING));
}
return theResponse;
}
apiClientResponse * apiClientSimple::apiPost(std::string postData) {
CURLcode errCode;
auto theResponse = new apiClientResponse();
curl_easy_setopt(handler, CURLOPT_HEADERDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_WRITEDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_POST, 1L);
curl_easy_setopt(handler, CURLOPT_POSTFIELDS, postData.c_str());
errCode = curl_easy_perform(handler);
if(errCode != CURLE_OK) {
throw (apiClientException(apiClientException::API_CLIENT_EXCEPTION_WHILE_PERFORMING));
}
return theResponse;
}
apiClientResponse * apiClientSimple::apiPut() {
}
apiClientResponse * apiClientSimple::apiPatch(std::string patchData) {
CURLcode errCode;
auto theResponse = new apiClientResponse();
curl_easy_setopt(handler, CURLOPT_HEADERDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_WRITEDATA, theResponse);
curl_easy_setopt(handler, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_easy_setopt(handler, CURLOPT_POSTFIELDS, patchData.c_str());
errCode = curl_easy_perform(handler);
if(errCode != CURLE_OK) {
throw (apiClientException(apiClientException::API_CLIENT_EXCEPTION_WHILE_PERFORMING));
}
return theResponse;
}
apiClientSimple::~apiClientSimple() {
curl_easy_cleanup(handler);
}
apiClientSimple::apiClientSimple(const apiClientSimple & toCopy){
handler = curl_easy_duphandle(toCopy.handler);
url = toCopy.url;
endpoint = toCopy.endpoint;
}