-
Notifications
You must be signed in to change notification settings - Fork 1
/
veridic.min.hpp
634 lines (487 loc) · 16.3 KB
/
veridic.min.hpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// Archivo: http.hpp
#pragma once
#ifndef HTTP_H
#define HTTP_H
#define CURL_STATICLIB
#include <curl/curl.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#define PUT_TYPE "PUT"
#define POST_t "POST"
#define DELETE_TYPE "DELETE"
#define DEFAULT "00"
using std::string;
template<typename A,class...LIST>
struct headers_t {
std::vector<LIST...> list;
explicit headers_t(std::initializer_list<LIST...> arguments) : list(arguments) {
if (list.size() > 0){
for(auto &it : list) {
std::cout << it << std::endl;
aloc.push_back(it);
}
initial = true;
}
}
bool initial{false};
std::vector<A> aloc{};
};
typedef headers_t<string,string> Headers;
template <typename A, class... LIST>
struct FIELDS
{
std::vector<LIST...> list;
FIELDS(std::initializer_list<LIST...> arguments) : list(arguments) {}
string http_params{};
string transform()
{
int it = 0;
for (size_t i = 0; i < list.size(); i++)
{
if(it == 0) {
http_params += list[i] + "&";
} else if(it==2 && i) {
http_params+="&"+list[i];
it = 0;
} else {
http_params += list[i];
}
}
http_params[http_params.length()-1] = char(00);
return http_params;
}
};
typedef FIELDS<string, string> POST;
typedef FIELDS<string, string> PUT;
typedef FIELDS<string, string> GET;
typedef FIELDS<string, string> DELETE;
class HTTP
{
private:
std::shared_ptr<CURL *> curl = nullptr;
std::vector<string> http_headers;
std::vector<CURLcode> performs;
unsigned int queryIndice = 0;
string URL{};
string response{};
bool headerify{false};
public:
HTTP(string);
HTTP();
~HTTP();
static size_t callback(void *, size_t, size_t, string *);
int getSimple(string endpoint_= DEFAULT);
int getBase(GET*, string endpoint= DEFAULT, bool headers = false, Headers* base = nullptr);
int get(GET& get, Headers & headers, string endpoint = DEFAULT);
int getHeaders(Headers &headers, string endpoint = DEFAULT);
int postSimple(string endpoint = DEFAULT);
int postHeaders(Headers& headers, string end = DEFAULT);
int post(POST &, string endpoint = DEFAULT, string type = DEFAULT);
int post(POST *, Headers &, string endpoint = DEFAULT, string type = DEFAULT);
int put(PUT &, string endpoint=DEFAULT);
int put(PUT &, Headers &, string endpoint = DEFAULT);
int Delete(DELETE &, string endpoint = DEFAULT);
int Delete(DELETE &, Headers &, string endpoint = DEFAULT);
int custom(POST &field, string type, string endpoint = DEFAULT);
int custom(POST &fields, Headers &headers, string type, string endpoint = DEFAULT);
curl_slist* makeHeaders(std::vector<string>);
void setUrl(string);
int setHeaders(Headers&);
string Response();
string without(string, char);
string genPerfomList();
};
#endif // !HTTP_H
// Archivo: veridic.hpp
#pragma once
#ifndef VERIDIC_HPP
#define VERIDIC_HPP
#include <iostream>
#include <memory.h>
#include <string>
#define PLAIN_TEXT "Content-Type: text/plain"
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
class Veridic {
private:
shared_ptr<HTTP> http = nullptr;
string URL{};
public:
Veridic();
Veridic(string);
bool setUrl(string);
string get(string endpoint = DEFAULT);
string get(Headers &, string endpoint = DEFAULT);
string get(GET &, string endpoint = DEFAULT);
string get(GET &, Headers &, string endpoint = DEFAULT);
string post(string endpoint = DEFAULT);
string post(Headers &, string endpoint = DEFAULT);
string post(POST &, string endpoint = DEFAULT, string type = DEFAULT);
string post(POST &, Headers &, string endpoint = DEFAULT, string type = DEFAULT);
string put(PUT &, string endpoint = DEFAULT);
string put(PUT &, Headers &, string endpoint = DEFAULT);
string Delete(DELETE &, string endpoint = DEFAULT);
string Delete(DELETE &, Headers &, string endpoint = DEFAULT);
string custom(POST &field, string type, string endpoint = DEFAULT);
string custom(POST &fields, Headers &headers, string type, string endpoint = DEFAULT);
};
#endif // !VERIDIC_HPP
// Archivo: utils.hpp
#ifndef UTILS_HPP
#define UTILS_HPP
#include<iostream>
constexpr auto UTILS_ERROR = "-[critic error]-";
constexpr auto UTILS_WARNING = "-[warning]-";
constexpr auto UTILS_SUCCESS = 0;
constexpr auto SYSTEM_DECORATOR = "-[BAD SYSTEM RESPONSE]-";
constexpr auto CURL_DECORATOR = "-[BAD CURL RESPONSE]-";
constexpr auto BAD_RESULT = "-[BAD_USE]-";
constexpr auto HTTP_ERROR = -1;
constexpr auto CURL_ERROR = -2;
constexpr auto HTTP_SUCCESS = 0;
constexpr auto UTILS_USER_ERROR = 3;
template<class... C>
auto screen(std::ostream& out, C const &...content) -> void {
((out<<content), ...);
}
#endif // UTILS_HPP!
// Archivo: http.cpp
HTTP::HTTP(string url) : URL(url) {
curl = std::make_shared<CURL *>(curl_easy_init());
if (!curl) {
screen(std::cout, UTILS_ERROR, SYSTEM_DECORATOR);
}
}
HTTP::HTTP() {}
HTTP::~HTTP() {curl.reset();}
// callback function, this extract response of querys
size_t HTTP::callback(void *buffer, size_t size, size_t nmemb, string *data) {
size_t rsize = size * nmemb;
try {
data->append(static_cast<const char *>(buffer), rsize);
}
catch (std::bad_alloc &e) {
screen(std::clog, UTILS_ERROR, SYSTEM_DECORATOR, &e);
}
return rsize;
}
// base http get
int HTTP::getSimple(std::string endpoint) {
string temp_url = endpoint != DEFAULT ? URL +endpoint : URL;
curl_easy_setopt(*curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(*curl, CURLOPT_URL, temp_url.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, HTTP::callback);
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, &response);
CURLcode perfom = curl_easy_perform(*curl);
performs.push_back(perfom);
try {
if (perfom != CURLE_OK)
screen(std::clog, UTILS_WARNING, CURL_DECORATOR, perfom);
}
catch (std::exception &error) {
screen(std::cout, UTILS_ERROR, &error);
}
curl_easy_cleanup(*curl);
return HTTP_SUCCESS;
}
int HTTP::getBase(GET *fields, std::string endpoint, bool isHead, Headers *headers) {
string temp_url = endpoint != DEFAULT ? URL +endpoint : URL;
struct curl_slist *headersList = nullptr;
if(fields != nullptr) {
temp_url += "?" + fields->transform();
fields = nullptr;
}
curl_easy_setopt(*curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(*curl, CURLOPT_URL, temp_url.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, HTTP::callback);
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, &response);
if (isHead) {
headersList = makeHeaders(headers->aloc);
if (headersList != nullptr){
curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headersList );
}
}
CURLcode perfom = curl_easy_perform(*curl);
performs.push_back(perfom);
if (headersList != nullptr){
curl_slist_free_all(headersList);
headers = nullptr;
}
try {
if (perfom != CURLE_OK)
screen(std::clog, UTILS_WARNING, CURL_DECORATOR, perfom);
}
catch (std::exception &error){
screen(std::cout, UTILS_ERROR, &error);
return CURL_ERROR;
}
curl_easy_cleanup(*curl);
return HTTP_SUCCESS;
}
int HTTP::get(GET &fields, Headers &headers, std::string endpoint) {
return HTTP::getBase(&fields, endpoint, true, &headers);
}
int HTTP::getHeaders(Headers &headers, string endpoint) {
return HTTP::getBase(nullptr, endpoint, true, &headers);
}
// base http post
int HTTP::post(POST &fields, string endpoint, string type) {
string temp_url = endpoint != DEFAULT ? URL +endpoint : URL;
auto postFields = fields.transform();
curl_easy_setopt(*curl, CURLOPT_URL, temp_url.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, HTTP::callback);
curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, &response);
if (type != DEFAULT) {
curl_easy_setopt(*curl, CURLOPT_CUSTOMREQUEST, type.c_str());
} else{
curl_easy_setopt(*curl, CURLOPT_CUSTOMREQUEST, (const char*)POST_t);
}
CURLcode perfom = curl_easy_perform(*curl);
performs.push_back(perfom);
try {
if (perfom != CURLE_OK)
screen(std::clog, UTILS_WARNING, CURL_DECORATOR, perfom);
}
catch (std::exception &error) {
screen(std::cout, UTILS_ERROR, &error);
return CURL_ERROR;
}
curl_easy_cleanup(*curl);
return HTTP_SUCCESS;
}
int HTTP::post(POST *fields, Headers &headers, string endpoint, string type) {
string temp_url = endpoint != DEFAULT ? URL +endpoint : URL;
struct curl_slist *headersList = nullptr;
curl_easy_setopt(*curl, CURLOPT_URL, temp_url.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, HTTP::callback);
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, &response);
if(fields != nullptr) {
auto postFields = fields->transform();
curl_easy_setopt(*curl, CURLOPT_POSTFIELDS, postFields.c_str());
fields = nullptr;
}
if (type != DEFAULT){
curl_easy_setopt(*curl, CURLOPT_CUSTOMREQUEST, type.c_str());
}
std::cout << headers.list.size() <<std::endl;
headersList = makeHeaders(headers.aloc);
curl_easy_setopt(*curl, CURLOPT_HTTPHEADER, headersList );
CURLcode perfom = curl_easy_perform(*curl);
performs.push_back(perfom);
curl_slist_free_all(headersList);
try {
if (perfom != CURLE_OK)
screen(std::clog, UTILS_WARNING, CURL_DECORATOR, perfom);
}
catch (std::exception &error) {
screen(std::cout, UTILS_ERROR, &error);
return CURL_ERROR;
}
curl_easy_cleanup(*curl);
return HTTP_SUCCESS;
}
int HTTP::postHeaders(Headers &headers, std::string end) {
return HTTP::post(nullptr, headers, end, "POST");
}
int HTTP::postSimple(std::string endpoint) {
string temp_url = endpoint != DEFAULT ? URL +endpoint : URL;
curl_easy_setopt(*curl, CURLOPT_URL, temp_url.c_str());
curl_easy_setopt(*curl, CURLOPT_WRITEFUNCTION, HTTP::callback);
curl_easy_setopt(*curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(*curl, CURLOPT_CUSTOMREQUEST, (const char*)POST_t);
CURLcode perfom = curl_easy_perform(*curl);
performs.push_back(perfom);
try {
if (perfom != CURLE_OK)
screen(std::clog, UTILS_WARNING, CURL_DECORATOR, perfom);
}
catch (std::exception &error) {
screen(std::cout, UTILS_ERROR, &error);
return CURL_ERROR;
}
curl_easy_cleanup(*curl);
return HTTP_SUCCESS;
}
int HTTP::put(PUT &_fields, string _endpoint) {
return (_endpoint != DEFAULT) ? post(_fields, _endpoint, PUT_TYPE) : post(_fields, PUT_TYPE);
}
int HTTP::put(PUT &_fields, Headers &_headers, string _endpoint) {
return (_endpoint != DEFAULT) ? post(&_fields, _headers, _endpoint, PUT_TYPE) : post(&_fields, _headers, PUT_TYPE);
}
int HTTP::Delete(DELETE &_fields, string _endpoint) {
return (_endpoint != DEFAULT) ? post(_fields, _endpoint, DELETE_TYPE) : post(_fields, DELETE_TYPE);
}
int HTTP::Delete(DELETE &_fields, Headers &_headers, string _endpoint) {
return (_endpoint != DEFAULT) ? post(&_fields, _headers, _endpoint, DELETE_TYPE) : post(&_fields, _headers, DELETE_TYPE);
}
int HTTP::custom(POST &_fields, string type, string _endpoint) {
return (_endpoint != DEFAULT) ? post(_fields, _endpoint, type) : post(_fields, type);
}
int HTTP::custom(POST &_fields, Headers &_headers, string type, string _endpoint) {
return (_endpoint != DEFAULT) ? post(&_fields, _headers, _endpoint, type) : post(&_fields, _headers, type);
}
int HTTP::setHeaders(Headers &headers)
{
if (!headers.initial)
return UTILS_USER_ERROR;
http_headers = headers.aloc;
headerify = true;
return UTILS_SUCCESS;
}
curl_slist *HTTP::makeHeaders(std::vector<string> headers) {
struct curl_slist *list = nullptr;
if (headers.empty())
return list;
for (auto &it : headers) {
std::cout << it << std::endl;
list = curl_slist_append(list, it.c_str());
}
return list;
}
void HTTP::setUrl(string _url){
URL = _url;
}
// extract response of the querys
string HTTP::Response()
{
return without(response, char(34));
}
// remove separator send of the API
string HTTP::without(string target, char key) {
for (auto &it : target) {
if (it == key)
it = char(00);
}
return target;
}
// extract the list of result of querys
string HTTP::genPerfomList(){
int map{};
for (auto &it : performs){
map += it;
}
return std::to_string(map);
}
// Archivo: veridic.cpp
Veridic::Veridic() {}
Veridic::Veridic(string tasty_url) : URL(tasty_url) {}
bool Veridic::setUrl(string url) {
if (!url.empty()) {
URL = url;
return true;
}
return false;
}
string Veridic::get(string endpoint)
{
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->getSimple(endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::get(GET &fields, string endpoint)
{
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->getBase(&fields, endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::get(GET &fields, Headers& headers, string endpoint)
{
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->get(fields, headers, endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::get(Headers& headers, string endpoint)
{
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->getHeaders(headers, endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::post(POST &fields, string endpoint, string type)
{
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->post(fields, endpoint != DEFAULT ? endpoint : "", type != "" ? type : "" ) != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::post(POST &fields, Headers &headers, string endpoint, string type)
{
if (URL.empty())
return BAD_RESULT;
if (!headers.initial)
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->post(&fields, headers, endpoint != DEFAULT ? endpoint : "", type != "" ? type : "" ) != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::post(std::string endpoint) {
if (URL.empty())
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->postSimple(endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::post(Headers &headers, std::string endpoint) {
if (URL.empty())
return BAD_RESULT;
if (!headers.initial)
return BAD_RESULT;
http = make_shared<HTTP>(URL);
if (http->postHeaders(headers, endpoint != DEFAULT ? endpoint : "") != HTTP_SUCCESS)
return BAD_RESULT;
auto response = http->Response();
http.reset();
return response;
}
string Veridic::put(PUT &_fields, string _endpoint) {
return _endpoint != DEFAULT ? post(_fields, _endpoint, PUT_TYPE) :post(_fields, PUT_TYPE) ;
}
string Veridic::put(PUT &_fields, Headers &_headers, string _endpoint) {
return _endpoint != DEFAULT ? post(_fields,_headers, _endpoint, PUT_TYPE) :post(_fields, _headers, PUT_TYPE) ;
}
string Veridic::Delete(DELETE &_fields, string _endpoint) {
return _endpoint != DEFAULT ? post(_fields, _endpoint, DELETE_TYPE) : post(_fields, DELETE_TYPE);
}
string Veridic::Delete(DELETE &_fields, Headers &_headers, string _endpoint) {
return _endpoint != DEFAULT ? post(_fields, _headers, _endpoint, DELETE_TYPE) : post(_fields, _headers, DELETE_TYPE);
}
string Veridic::custom(POST &_fields, string type, string _endpoint) {
return _endpoint != DEFAULT ?post(_fields, _endpoint, type) : post(_fields, type);
}
string Veridic::custom(POST &_fields, Headers &_headers, string type, string _endpoint) {
return _endpoint != DEFAULT ?post(_fields, _headers , _endpoint, type) : post(_fields, type);
}