-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connector.cpp
143 lines (115 loc) · 4.59 KB
/
Connector.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
#ifndef EVERESTAPI_CONNECTOR_CPP
#define EVERESTAPI_CONNECTOR_CPP
#include "curl/curl.h"
#include <sys/stat.h>
#include "iostream"
using namespace std;
class Connector {
private:
CURL *curl;
public:
struct Response {
long code;
string response;
};
Connector() {
init();
}
~Connector() {
curl_easy_cleanup(curl);
}
void init() {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "build/cookie.txt");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "build/cookie.txt");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
//Debug
//curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
Response postRequest(string URL, string data) {
if (!curl) {
throw runtime_error("curl crushed");
}
Response newResponse;
curl_easy_setopt(curl, CURLOPT_HTTPPOST, true);
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
if (!data.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &newResponse.response);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &newResponse.code);
curl_easy_cleanup(curl);
return newResponse;
}
Response getRequest(string URL) {
if (!curl) {
throw runtime_error("curl crushed");
}
Response newResponse;
curl_easy_setopt(curl, CURLOPT_HTTPGET, true);
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &newResponse.response);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &newResponse.code);
curl_easy_cleanup(curl);
return newResponse;
}
Response deleteRequest(string URL) {
if (!curl) {
throw runtime_error("curl crushed");
}
Response newResponse;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &newResponse.response);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &newResponse.code);
curl_easy_cleanup(curl);
return newResponse;
}
Response uploadFile(string URL, string fullName) {
if (!curl) {
throw runtime_error("curl crushed");
}
FILE* file = fopen(fullName.c_str(), "rb");
if (!file) {
throw runtime_error("file error");
}
struct stat file_info;
if(fstat(fileno(file), &file_info) != 0) {
throw runtime_error("file reading error");
}
struct curl_slist *headers = NULL;
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
headers = curl_slist_append(headers, "Expect:");
//headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &fread);
curl_easy_setopt(curl, CURLOPT_READDATA, file);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
Response newResponse;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &newResponse.response);
curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &newResponse.code);
curl_easy_cleanup(curl);
fclose(file);
return newResponse;
}
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
((string *) userp)->append((char *) contents, size * nmemb);
return size * nmemb;
}
};
#endif