Skip to content

Commit 8a27d67

Browse files
committed
update(http): Adding source code file for HTTPRequest
1 parent b2a17d4 commit 8a27d67

File tree

2 files changed

+143
-122
lines changed

2 files changed

+143
-122
lines changed

src/http/HTTPRequest.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include "HTTPRequest.h"
2+
3+
HTTPRequest::HTTPRequest(std::string url)
4+
{
5+
this->client = new httplib::Client(url);
6+
7+
this->client->set_default_headers({{"Accept-Encoding", "gzip, deflate"}});
8+
}
9+
10+
HTTPRequest::~HTTPRequest()
11+
{
12+
this->multipartItems.clear();
13+
this->params.clear();
14+
this->headers.clear();
15+
16+
this->body.clear();
17+
delete this->client;
18+
}
19+
20+
void HTTPRequest::Get(std::string path)
21+
{
22+
this->executed = true;
23+
this->result = this->client->Get(path, this->params, this->headers);
24+
}
25+
26+
void HTTPRequest::Delete(std::string path)
27+
{
28+
this->executed = true;
29+
this->result = this->client->Delete(path, this->headers);
30+
}
31+
32+
void HTTPRequest::Post(std::string path)
33+
{
34+
this->executed = true;
35+
std::string final_content_type = contentTypesMap.at(this->content_type);
36+
if (this->content_type == ContentType::MULTIPART_FORMDATA)
37+
this->result = this->client->Post(path, this->headers, this->multipartItems);
38+
else
39+
this->result = this->client->Post(path, this->headers, this->body, final_content_type);
40+
}
41+
42+
void HTTPRequest::Put(std::string path)
43+
{
44+
this->executed = true;
45+
std::string final_content_type = contentTypesMap.at(this->content_type);
46+
if (this->content_type == ContentType::MULTIPART_FORMDATA)
47+
this->result = this->client->Put(path, this->headers, this->multipartItems);
48+
else
49+
this->result = this->client->Put(path, this->headers, this->body, final_content_type);
50+
}
51+
52+
void HTTPRequest::Patch(std::string path)
53+
{
54+
this->executed = true;
55+
std::string final_content_type = contentTypesMap.at(this->content_type);
56+
this->result = this->client->Patch(path, this->headers, this->body, final_content_type);
57+
}
58+
59+
void HTTPRequest::AddHeader(std::string key, std::string value)
60+
{
61+
if (this->headers.find(key) == this->headers.end())
62+
this->headers.insert(std::make_pair(key, value));
63+
}
64+
65+
void HTTPRequest::DeleteHeader(std::string key)
66+
{
67+
if (this->headers.find(key) != this->headers.end())
68+
this->headers.erase(key);
69+
}
70+
71+
void HTTPRequest::AddMultipartFile(std::string field, std::string content, std::string filename, std::string file_content_type)
72+
{
73+
httplib::MultipartFormData data{field, content, filename, file_content_type};
74+
75+
this->multipartItems.push_back(data);
76+
}
77+
78+
void HTTPRequest::SetContentType(ContentType content_type)
79+
{
80+
this->content_type = content_type;
81+
}
82+
83+
void HTTPRequest::SetBody(std::string body)
84+
{
85+
this->body = body;
86+
}
87+
88+
void HTTPRequest::SetBasicAuthentication(std::string username, std::string password)
89+
{
90+
this->client->set_basic_auth(username, password);
91+
}
92+
93+
void HTTPRequest::SetBearerAuthenticationToken(std::string token)
94+
{
95+
this->client->set_bearer_token_auth(token);
96+
}
97+
98+
void HTTPRequest::SetFollowRedirect(bool follow)
99+
{
100+
this->client->set_follow_location(follow);
101+
}
102+
103+
std::string HTTPRequest::GetBody()
104+
{
105+
if (!this->executed)
106+
return "";
107+
108+
return this->result->body;
109+
}
110+
111+
int HTTPRequest::GetStatusCode()
112+
{
113+
if (!this->executed)
114+
return 0;
115+
116+
return this->result->status;
117+
}
118+
119+
std::string HTTPRequest::GetError()
120+
{
121+
if (!this->result)
122+
return "";
123+
124+
return httplib::to_string(this->result.error());
125+
}

src/http/HTTPRequest.h

Lines changed: 18 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -23,128 +23,24 @@ class HTTPRequest
2323
bool executed = false;
2424

2525
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();
14844
};
14945

15046
#endif

0 commit comments

Comments
 (0)