@@ -23,128 +23,24 @@ class HTTPRequest
23
23
bool executed = false ;
24
24
25
25
public:
26
- HTTPRequest (std::string url)
27
- {
28
- this ->client = new httplib::Client (url);
29
-
30
- this ->client ->set_default_headers ({{" Accept-Encoding" , " gzip, deflate" }});
31
- }
32
-
33
- ~HTTPRequest ()
34
- {
35
- this ->multipartItems .clear ();
36
- this ->params .clear ();
37
- this ->headers .clear ();
38
-
39
- this ->body .clear ();
40
- delete this ->client ;
41
- }
42
-
43
- void Get (std::string path)
44
- {
45
- this ->executed = true ;
46
- this ->result = this ->client ->Get (path, this ->params , this ->headers );
47
- }
48
-
49
- void Delete (std::string path)
50
- {
51
- this ->executed = true ;
52
- this ->result = this ->client ->Delete (path, this ->headers );
53
- }
54
-
55
- void Post (std::string path)
56
- {
57
- this ->executed = true ;
58
- std::string final_content_type = contentTypesMap.at (this ->content_type );
59
- if (this ->content_type == ContentType::MULTIPART_FORMDATA)
60
- this ->result = this ->client ->Post (path, this ->headers , this ->multipartItems );
61
- else
62
- this ->result = this ->client ->Post (path, this ->headers , this ->body , final_content_type);
63
- }
64
-
65
- void Put (std::string path)
66
- {
67
- this ->executed = true ;
68
- std::string final_content_type = contentTypesMap.at (this ->content_type );
69
- if (this ->content_type == ContentType::MULTIPART_FORMDATA)
70
- this ->result = this ->client ->Put (path, this ->headers , this ->multipartItems );
71
- else
72
- this ->result = this ->client ->Put (path, this ->headers , this ->body , final_content_type);
73
- }
74
-
75
- void Patch (std::string path)
76
- {
77
- this ->executed = true ;
78
- std::string final_content_type = contentTypesMap.at (this ->content_type );
79
- this ->result = this ->client ->Patch (path, this ->headers , this ->body , final_content_type);
80
- }
81
-
82
- void AddHeader (std::string key, std::string value)
83
- {
84
- this ->headers .insert (std::make_pair (key, value));
85
- }
86
-
87
- void DeleteHeader (std::string key)
88
- {
89
- if (this ->headers .find (key) != this ->headers .end ())
90
- this ->headers .erase (key);
91
- }
92
-
93
- void AddMultipartFile (std::string field, std::string content, std::string filename, std::string file_content_type)
94
- {
95
- httplib::MultipartFormData data{field, content, filename, file_content_type};
96
-
97
- this ->multipartItems .push_back (data);
98
- }
99
-
100
- void SetContentType (ContentType content_type)
101
- {
102
- this ->content_type = content_type;
103
- }
104
-
105
- void SetBody (std::string body)
106
- {
107
- this ->body = body;
108
- }
109
-
110
- void SetBasicAuthentication (std::string username, std::string password)
111
- {
112
- this ->client ->set_basic_auth (username, password);
113
- }
114
-
115
- void SetBearerAuthenticationToken (std::string token)
116
- {
117
- this ->client ->set_bearer_token_auth (token);
118
- }
119
-
120
- void SetFollowRedirect (bool follow)
121
- {
122
- this ->client ->set_follow_location (follow);
123
- }
124
-
125
- std::string GetBody ()
126
- {
127
- if (!this ->executed )
128
- return " " ;
129
-
130
- return this ->result ->body ;
131
- }
132
-
133
- int GetStatusCode ()
134
- {
135
- if (!this ->executed )
136
- return 0 ;
137
-
138
- return this ->result ->status ;
139
- }
140
-
141
- std::string GetError ()
142
- {
143
- if (!this ->result )
144
- return " " ;
145
-
146
- return httplib::to_string (this ->result .error ());
147
- }
26
+ HTTPRequest (std::string url);
27
+ ~HTTPRequest ();
28
+ void Get (std::string path);
29
+ void Delete (std::string path);
30
+ void Post (std::string path);
31
+ void Put (std::string path);
32
+ void Patch (std::string path);
33
+ void AddHeader (std::string key, std::string value);
34
+ void DeleteHeader (std::string key);
35
+ void AddMultipartFile (std::string field, std::string content, std::string filename, std::string file_content_type);
36
+ void SetContentType (ContentType content_type);
37
+ void SetBody (std::string body);
38
+ void SetBasicAuthentication (std::string username, std::string password);
39
+ void SetBearerAuthenticationToken (std::string token);
40
+ void SetFollowRedirect (bool follow);
41
+ std::string GetBody ();
42
+ int GetStatusCode ();
43
+ std::string GetError ();
148
44
};
149
45
150
46
#endif
0 commit comments