1+ #pragma once
2+
3+ #ifndef HTTP_H
4+ #define HTTP_H
5+
6+ #define CURL_STATICLIB
7+ #include < curl/curl.h>
8+ #include < iostream>
9+ #include < string>
10+ #include < memory>
11+ #include < vector>
12+
13+ #include " utils/utils.hpp"
14+
15+ #define PUT_TYPE " PUT"
16+ #define DELETE_TYPE " DELETE"
17+ #define DEFAULT " 00"
18+
19+ using std::string;
20+
21+
22+
23+ template <typename A,class ...LIST>
24+ struct headers {
25+ std::vector<LIST...> list;
26+ headers ();
27+ headers (std::initializer_list<LIST...> arguments) : list(arguments) {
28+ for (auto &it : list) {
29+ aloc.push_back (it);
30+ }
31+ initial = true ;
32+ }
33+ bool initial{false };
34+ std::vector<A> aloc{};
35+ };
36+ typedef headers<string,string> Headers;
37+
38+
39+
40+ template <typename A, class ... LIST>
41+ struct FIELDS
42+ {
43+ std::vector<LIST...> list;
44+ FIELDS (std::initializer_list<LIST...> arguments) : list(arguments) {}
45+
46+ string http_params{};
47+ string transform ()
48+ {
49+ int it = 0 ;
50+ for (size_t i = 0 ; i < list.size (); i++)
51+ {
52+ if (it == 0 ) {
53+ http_params += list[i] + " &" ;
54+ } else if (it==2 && i) {
55+ http_params+=" &" +list[i];
56+ it = 0 ;
57+ } else {
58+ http_params += list[i];
59+ }
60+ }
61+ http_params[http_params.length ()-1 ] = char (00 );
62+ return http_params;
63+ }
64+ };
65+
66+ typedef FIELDS<string, string> POST;
67+ typedef FIELDS<string, string> PUT;
68+ typedef FIELDS<string, string> GET;
69+ typedef FIELDS<string, string> DELETE;
70+
71+
72+ class HTTP
73+ {
74+ private:
75+ std::shared_ptr<CURL *> curl = nullptr ;
76+
77+ std::vector<string> http_headers;
78+ std::vector<CURLcode> performs;
79+ unsigned int queryIndice = 0 ;
80+ string URL{};
81+ string response{};
82+ bool headerify{false };
83+
84+ public:
85+ HTTP (string);
86+ HTTP ();
87+ ~HTTP ();
88+ static size_t callback (void *, size_t , size_t , string *);
89+
90+ int get (string opcional_endpoint_= DEFAULT);
91+ int get (GET&, string opcional_endpoint= DEFAULT);
92+
93+ int post (POST &, string endpoint = DEFAULT, string type = DEFAULT);
94+ int post (POST &, Headers &, string endpoint = DEFAULT, string type = DEFAULT);
95+
96+
97+ int put (PUT &, string endpoint=DEFAULT);
98+ int put (PUT &, Headers &, string endpoint = DEFAULT);
99+
100+ int Delete (DELETE &, string endpoint = DEFAULT);
101+ int Delete (DELETE &, Headers &, string endpoint = DEFAULT);
102+
103+ int custom (POST &field, string type, string endpoint = DEFAULT);
104+ int custom (POST &fields, Headers &headers, string type, string endpoint = DEFAULT);
105+
106+
107+ curl_slist* makeHeaders (std::vector<string>);
108+ void setUrl (string);
109+ int setHeaders (Headers&);
110+
111+ string Response ();
112+ string without (string, char );
113+ string genPerfomList ();
114+ };
115+
116+ #endif // !HTTP_H
0 commit comments