-
Notifications
You must be signed in to change notification settings - Fork 1
/
Backend_API.cpp
227 lines (191 loc) · 8.42 KB
/
Backend_API.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "GUI.h"
#include <wx/base64.h>
#include "Backend_API.h"
#include <iostream>
Json::Value Backend_API::sign_up(std::string email, std::string passwd, std::string repeat_passwd){
struct curl_slist *slist1=nullptr;
CURL *curl;
CURLcode res;
std::string strJson;
curl = curl_easy_init();
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
std::string url = addr+"/Auth/register";
std::string query = "{\"email\":\""+email+"\",\"password\":\""+passwd+"\",\"confirmPassword\":\""+repeat_passwd+"\"}";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strJson);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
throw std::runtime_error("Błąd połączenia. Skontaktuj się z pomocą techniczną.");
}
curl_easy_cleanup(curl);
return str2json(strJson);
}
Json::Value Backend_API::login(std::string email, std::string passwd, std::string code){
struct curl_slist *slist1=nullptr;
CURL *curl;
CURLcode res;
std::string strJson;
/* get a curl handle */
curl = curl_easy_init();
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
std::string url = addr+"/Auth/login";
std::string query = "{\"email\": \""+email+"\", \"password\": \""+passwd+"\", \"code\":\""+code+"\"}";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strJson);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
throw std::runtime_error("Błąd połączenia. Skontaktuj się z pomocą techniczną.");
}
// std::cout << strJson << std::endl;
curl_easy_cleanup(curl);
return str2json(strJson);
}
Json::Value Backend_API::get_clients_list(){
CURL *curl;
CURLcode res;
std::string strJson;
curl = curl_easy_init();
if (curl) {
std::string url = addr+"/Client";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strJson);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
throw std::runtime_error("Błąd połączenia. Skontaktuj się z pomocą techniczną.");
}
curl_easy_cleanup(curl);
return str2json(strJson);
}
Json::Value Backend_API::delete_client(std::string token, std::string client_id){
struct curl_slist *slist1=nullptr;
CURL *curl;
CURLcode res;
std::string strJson;
/* get a curl handle */
curl = curl_easy_init();
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
std::string token_header = "Authorization: Bearer " + token;
slist1 = curl_slist_append(slist1, token_header.c_str());
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
std::string url = addr+"/Client/"+client_id;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strJson);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
long http_code;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
throw std::runtime_error(curl_easy_strerror(res));
else if (http_code == 401L)
throw std::runtime_error("Błąd autoryzacji. Spróbuj zalogować się ponownie.");
}
curl_easy_cleanup(curl);
return str2json(strJson);
}
Json::Value Backend_API::post_offer(std::string token,
std::string name,
std::string v_descr,
std::string beg_y,
std::string end_y,
std::string doors_num,
std::string seat_num,
std::string boot_cap,
std::string len,
std::string width,
std::string height,
std::string wheel_b,
std::string back_wheel,
std::string fron_wheel,
std::string gearbox,
std::string drive,
std::string client_id_json,
std::string price,
std::string offer_descr,
std::string email,
std::string phone_num)
{
struct curl_slist *slist1=nullptr;
CURL *curl;
CURLcode res;
std::string strJson;
auto clientJson = str2json(std::string(reinterpret_cast<const char *>(wxBase64Decode(client_id_json).GetData())));
/* get a curl handle */
curl = curl_easy_init();
std::string token_header = "Authorization: Bearer " + token;
slist1 = curl_slist_append(slist1, "Content-Type: application/json");
slist1 = curl_slist_append(slist1, token_header.c_str());
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
std::string url = addr+"/Offer";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
/* Now specify the POST data */
std::string query = "{\"vehicle\": {\"name\":\""+name+"\", \"description\": \""+v_descr+"\", \"productionStartYear\": "+beg_y+", \"productionEndYear\": "+end_y+", \"numberOfDoors\": "+doors_num+", \"numberOfSeats\": "+seat_num+", \"bootCapacity\": "+boot_cap+", \"length\": "+len+", \"width\": "+width+", \"height\": "+height+", \"wheelBase\": "+wheel_b+", \"backWheelTrack\": "+back_wheel+", \"frontWheelTrack\": "+fron_wheel+", \"gearbox\": \""+gearbox+"\", \"drive\": \""+drive+"\", \"clientId\": \""+clientJson["ClientId"].asString()+"\"}, \"price\": "+price+", \"description\": \""+offer_descr+"\", \"contactEmail\": \""+email+"\", \"contactPhoneNumber\": \""+phone_num+"\"}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strJson);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
throw std::runtime_error(curl_easy_strerror(res));
long http_code;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if(http_code == 401L)
throw std::runtime_error("Błąd autoryzacji. Spróbuj zalogować się ponownie.");
}
curl_easy_cleanup(curl);
return str2json(strJson);
}
size_t Backend_API::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
Json::Value Backend_API::str2json(std::string strJson){
Json::Value jsonData;
// std::cout << strJson << std::endl;
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();
Json::String errors;
reader->parse(
strJson.c_str(),
strJson.c_str() + strJson.size(),
&jsonData,
&errors
);
delete reader;
return jsonData;
}