Skip to content

Commit b29eb43

Browse files
committed
Method overloading structure converted to Builder Pattern
1 parent 7d1db7d commit b29eb43

File tree

3 files changed

+121
-121
lines changed

3 files changed

+121
-121
lines changed

examples/main.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ using namespace lklibs;
55

66
void simpleGet() {
77

8-
HttpClient httpClient("https://httpbun.com/get");
8+
HttpRequest httpRequest("https://httpbun.com/get");
99

1010
// The simplest but slowest method if multiple calls will be made
11-
auto response = httpClient
11+
auto response = httpRequest
1212
.setQueryString("param1=7&param2=test")
1313
.send()
1414
.get();
@@ -20,14 +20,14 @@ void simpleGet() {
2020

2121
void nonBlockingGet() {
2222

23-
HttpClient httpClient1("https://httpbun.com/get");
24-
HttpClient httpClient2("https://httpbun.com/get");
25-
HttpClient httpClient3("https://httpbun.com/get");
23+
HttpRequest httpRequest1("https://httpbun.com/get");
24+
HttpRequest httpRequest2("https://httpbun.com/get");
25+
HttpRequest httpRequest3("https://httpbun.com/get");
2626

2727
// All requests are made one after the other without waiting for a response
28-
auto future1 = httpClient1.setQueryString("param1=1&param2=test1").send();
29-
auto future2 = httpClient2.setQueryString("param1=2&param2=test2").send();
30-
auto future3 = httpClient3.setQueryString("param1=3&param2=test3").send();
28+
auto future1 = httpRequest1.setQueryString("param1=1&param2=test1").send();
29+
auto future2 = httpRequest2.setQueryString("param1=2&param2=test2").send();
30+
auto future3 = httpRequest3.setQueryString("param1=3&param2=test3").send();
3131

3232
// Then all the answers are received. Thus, 3 requests are sent in parallel
3333
auto response1 = future1.get();
@@ -49,10 +49,10 @@ void nonBlockingGet() {
4949

5050
void receiveBinaryData() {
5151

52-
HttpClient httpClient("https://httpbun.com/bytes/100");
52+
HttpRequest httpRequest("https://httpbun.com/bytes/100");
5353

5454
// If you need to retrieve binary data such as an image, just call the "returnAsBinary" method
55-
auto response = httpClient
55+
auto response = httpRequest
5656
.returnAsBinary()
5757
.send()
5858
.get();
@@ -66,10 +66,10 @@ void receiveBinaryData() {
6666

6767
void receiveError() {
6868

69-
HttpClient httpClient("https://httpbun.com/not_found");
69+
HttpRequest httpRequest("https://httpbun.com/not_found");
7070

7171
// This is an exception free library. If an error occurs, no exception is thrown
72-
auto response = httpClient.send().get();
72+
auto response = httpRequest.send().get();
7373

7474
// Instead, the succeed field of the response object is set to false
7575
std::cout << "Succeed: " << response.succeed << std::endl;
@@ -83,10 +83,10 @@ void receiveError() {
8383

8484
void sendingHttpHeaders() {
8585

86-
HttpClient httpClient("https://httpbun.com/get?param1=7&param2=test");
86+
HttpRequest httpRequest("https://httpbun.com/get?param1=7&param2=test");
8787

8888
// You can send custom headers as key-value pairs
89-
auto response = httpClient
89+
auto response = httpRequest
9090
.addHeader("Custom-Header1", "value1")
9191
.addHeader("Custom-Header2", "value2")
9292
.send()
@@ -97,10 +97,10 @@ void sendingHttpHeaders() {
9797

9898
void simplePostWithFormData() {
9999

100-
HttpClient httpClient("https://httpbun.com/post");
100+
HttpRequest httpRequest("https://httpbun.com/post");
101101

102102
// You can send a POST request with form data in the payload
103-
auto response = httpClient
103+
auto response = httpRequest
104104
.setMethod(HttpMethod::POST)
105105
.setPayload("param1=7&param2=test")
106106
.send()
@@ -113,10 +113,10 @@ void simplePostWithFormData() {
113113

114114
void simplePostWithJSONData() {
115115

116-
HttpClient httpClient("https://httpbun.com/post");
116+
HttpRequest httpRequest("https://httpbun.com/post");
117117

118118
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
119-
auto response = httpClient
119+
auto response = httpRequest
120120
.setMethod(HttpMethod::POST)
121121
.setPayload(R"({"param1": 7, "param2": "test"})")
122122
.addHeader("Content-Type", "application/json")
@@ -130,10 +130,10 @@ void simplePostWithJSONData() {
130130

131131
void simplePutWithFormData() {
132132

133-
HttpClient httpClient("https://httpbun.com/put");
133+
HttpRequest httpRequest("https://httpbun.com/put");
134134

135135
// You can send a PUT request with form data in the payload just like POST
136-
auto response = httpClient
136+
auto response = httpRequest
137137
.setMethod(HttpMethod::PUT)
138138
.setPayload("param1=7&param2=test")
139139
.send()
@@ -146,10 +146,10 @@ void simplePutWithFormData() {
146146

147147
void simpleDeleteWithFormData() {
148148

149-
HttpClient httpClient("https://httpbun.com/delete");
149+
HttpRequest httpRequest("https://httpbun.com/delete");
150150

151151
// You can send a DELETE request with form data in the payload just like POST
152-
auto response = httpClient
152+
auto response = httpRequest
153153
.setMethod(HttpMethod::DELETE_)
154154
.setPayload("param1=7&param2=test")
155155
.send()
@@ -162,10 +162,10 @@ void simpleDeleteWithFormData() {
162162

163163
void simplePatch() {
164164

165-
HttpClient httpClient("https://httpbun.com/patch");
165+
HttpRequest httpRequest("https://httpbun.com/patch");
166166

167167
// You can send a PATCH request with QueryString just like GET
168-
auto response = httpClient
168+
auto response = httpRequest
169169
.setMethod(HttpMethod::PATCH)
170170
.setQueryString("param1=7&param2=test")
171171
.send()
@@ -178,10 +178,10 @@ void simplePatch() {
178178

179179
void ignoreSslErrors() {
180180

181-
HttpClient httpClient("https://self-signed-cert.httpbun.com");
181+
HttpRequest httpRequest("https://self-signed-cert.httpbun.com");
182182

183183
// If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
184-
auto response = httpClient.ignoreSslErrors().send().get();
184+
auto response = httpRequest.ignoreSslErrors().send().get();
185185

186186
std::cout << "Succeed: " << response.succeed << std::endl;
187187
std::cout << "Http Status Code: " << response.statusCode << std::endl;

src/libcpp-http-client.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,24 @@ namespace lklibs {
8989
};
9090

9191
/**
92-
* @brief HTTP client class that makes asynchronous HTTP requests
92+
* @brief HTTP request class that makes asynchronous HTTP calls
9393
*/
94-
class HttpClient {
94+
class HttpRequest {
9595
public:
9696

9797
/**
98-
* @brief Constructor for the HttpClient class
98+
* @brief Constructor for the HttpRequest class
9999
*
100100
* @param url: URL for the request
101101
*/
102-
explicit HttpClient(const std::string &url) {
102+
explicit HttpRequest(const std::string &url) {
103103

104104
this->url = url;
105105

106106
curl_global_init(CURL_GLOBAL_DEFAULT);
107107
}
108108

109-
~HttpClient() {
109+
~HttpRequest() {
110110
curl_global_cleanup();
111111
}
112112

@@ -115,7 +115,7 @@ namespace lklibs {
115115
*
116116
* @param method: HTTP method to be used for the request
117117
*/
118-
HttpClient &setMethod(const HttpMethod &method) {
118+
HttpRequest &setMethod(const HttpMethod &method) {
119119

120120
this->method = [method] {
121121
switch (method) {
@@ -142,7 +142,7 @@ namespace lklibs {
142142
*
143143
* @param queryString: Query string to be sent with the request
144144
*/
145-
HttpClient &setQueryString(const std::string &queryString) {
145+
HttpRequest &setQueryString(const std::string &queryString) {
146146

147147
if (this->url.find('?') != std::string::npos) {
148148

@@ -163,7 +163,7 @@ namespace lklibs {
163163
*
164164
* @param payload: Payload to be sent with the request
165165
*/
166-
HttpClient &setPayload(const std::string &payload) {
166+
HttpRequest &setPayload(const std::string &payload) {
167167

168168
this->payload = payload;
169169

@@ -173,7 +173,7 @@ namespace lklibs {
173173
/**
174174
* @brief Set the return format for the request as binary
175175
*/
176-
HttpClient &returnAsBinary() {
176+
HttpRequest &returnAsBinary() {
177177

178178
this->returnFormat = ReturnFormat::BINARY;
179179

@@ -183,7 +183,7 @@ namespace lklibs {
183183
/**
184184
* @brief Ignore SSL errors when making HTTP requests
185185
*/
186-
HttpClient &ignoreSslErrors() {
186+
HttpRequest &ignoreSslErrors() {
187187

188188
this->sslErrorsWillBeIgnored = true;
189189

@@ -196,7 +196,7 @@ namespace lklibs {
196196
* @param key: Header key
197197
* @param value: Header value
198198
*/
199-
HttpClient &addHeader(const std::string &key, const std::string &value) {
199+
HttpRequest &addHeader(const std::string &key, const std::string &value) {
200200

201201
this->headers[key] = value;
202202

0 commit comments

Comments
 (0)