Skip to content

Commit b991820

Browse files
Merge pull request #9 from lk-libs/builder-pattern-conversion
Builder pattern conversion
2 parents 176453a + 559647c commit b991820

File tree

4 files changed

+422
-653
lines changed

4 files changed

+422
-653
lines changed

README.md

Lines changed: 99 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ target_link_libraries(myProject PRIVATE libcpp-http-client CURL::libcurl)
5959
Below you can see the simplest use case sending QueryString parameters to an API via HTTP GET.
6060

6161
> [!IMPORTANT]
62-
> Please do not use it this way, if more than one call will be made . You do not use the non-blocking
62+
> Please do not use it this way, if more than one call will be made. You do not use the non-blocking
6363
> feature in this way. Just keep reading...
6464
6565
```cpp
@@ -70,10 +70,13 @@ using namespace lklibs;
7070

7171
int main() {
7272

73-
HttpClient httpClient;
73+
HttpRequest httpRequest("https://api.myproject.com");
7474

7575
// The simplest but slowest method if multiple calls will be made
76-
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test").get();
76+
auto response = httpRequest
77+
.setQueryString("param1=7&param2=test")
78+
.send()
79+
.get();
7780

7881
std::cout << "Succeed: " << response.succeed << std::endl;
7982
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -98,13 +101,17 @@ using namespace lklibs;
98101

99102
int main() {
100103

101-
HttpClient httpClient;
102-
103-
auto response1 = httpClient.getRequest("https://api.myproject.com/foo").get();
104-
auto response2 = httpClient.getRequest("https://api.myproject.com/bar").get();
105-
auto response3 = httpClient.getRequest("https://api.myproject.com/baz").get();
106-
auto response4 = httpClient.getRequest("https://api.myproject.com/qux").get();
107-
auto response5 = httpClient.getRequest("https://api.myproject.com/quux").get();
104+
HttpRequest httpRequest1("https://api.myproject.com/foo");
105+
HttpRequest httpRequest2("https://api.myproject.com/bar");
106+
HttpRequest httpRequest3("https://api.myproject.com/baz");
107+
HttpRequest httpRequest4("https://api.myproject.com/qux");
108+
HttpRequest httpRequest5("https://api.myproject.com/quux");
109+
110+
auto response1 = httpRequest1.send().get();
111+
auto response2 = httpRequest2.send().get();
112+
auto response3 = httpRequest3.send().get();
113+
auto response4 = httpRequest4.send().get();
114+
auto response5 = httpRequest5.send().get();
108115

109116
// Takes 2.5 seconds in total
110117

@@ -124,13 +131,17 @@ using namespace lklibs;
124131

125132
int main() {
126133

127-
HttpClient httpClient;
128-
129-
auto future1 = httpClient.getRequest("https://api.myproject.com/foo");
130-
auto future2 = httpClient.getRequest("https://api.myproject.com/bar");
131-
auto future3 = httpClient.getRequest("https://api.myproject.com/baz");
132-
auto future4 = httpClient.getRequest("https://api.myproject.com/qux");
133-
auto future5 = httpClient.getRequest("https://api.myproject.com/quux");
134+
HttpRequest httpRequest1("https://api.myproject.com/foo");
135+
HttpRequest httpRequest2("https://api.myproject.com/bar");
136+
HttpRequest httpRequest3("https://api.myproject.com/baz");
137+
HttpRequest httpRequest4("https://api.myproject.com/qux");
138+
HttpRequest httpRequest5("https://api.myproject.com/quux");
139+
140+
auto future1 = httpRequest.send();
141+
auto future2 = httpRequest.send();
142+
auto future3 = httpRequest.send();
143+
auto future4 = httpRequest.send();
144+
auto future5 = httpRequest.send();
134145

135146
auto response1 = future1.get();
136147
auto response2 = future2.get();
@@ -144,7 +155,7 @@ int main() {
144155
}
145156
```
146157

147-
All functions in the library return a future and allow the next line to run without blocking the flow.
158+
**"send"** function in the library return a future and allow the next line to run without blocking the flow.
148159

149160

150161
## What does exception free mean?
@@ -166,9 +177,9 @@ using namespace lklibs;
166177

167178
int main() {
168179

169-
HttpClient httpClient;
180+
HttpRequest httpRequest("https://www.myinvalidurl.com");
170181

171-
auto response = httpClient.getRequest("https://www.myinvalidurl.com").get();
182+
auto response = httpRequest.send().get();
172183

173184
// Instead of throwing an exception, the succeed field of the response object is set to false
174185
std::cout << "Succeed: " << response.succeed << std::endl;
@@ -189,8 +200,7 @@ int main() {
189200
In the examples so far, we have used the **"textData"** property of the returning response object.
190201
However, we need binary data for requests made to binary files such as images. In such cases,
191202
we can ensure that the returned data is returned in **"binaryData"** of type
192-
***"std::vector&lt;unsigned char&gt;"*** instead of **"textData"** by passing the value **"true"**
193-
to the **"returnAsBinary"** parameter.
203+
***"std::vector&lt;unsigned char&gt;"*** instead of **"textData"** by calling **"returnAsBinary()"** method before send as follow.
194204

195205
```cpp
196206
#include <fstream>
@@ -200,10 +210,13 @@ using namespace lklibs;
200210

201211
int main() {
202212

203-
HttpClient httpClient;
213+
HttpRequest httpRequest("https://api.myproject.com/image/7");
204214

205-
// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
206-
auto response = httpClient.getRequest("https://api.myproject.com/image/7", true).get();
215+
// If you need to retrieve binary data such as an image, just call the "returnAsBinary" method before send
216+
auto response = httpRequest
217+
.returnAsBinary()
218+
.send()
219+
.get();
207220

208221
std::cout << "Succeed: " << response.succeed << std::endl;
209222
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -218,8 +231,7 @@ int main() {
218231

219232
## Sending custom HTTP headers
220233

221-
If you need to send custom HTTP HEADERs during the request, you can send them in
222-
std::map<std::string, std::string> format.
234+
If you need to send custom HTTP HEADERs during the request, you can add them to the request as key-value pairs with **"addHeader()"** method.
223235

224236
```cpp
225237
#include <fstream>
@@ -229,16 +241,15 @@ using namespace lklibs;
229241

230242
int main() {
231243

232-
HttpClient httpClient;
233-
234-
// You can send custom headers in a string/string map
235-
auto headers = std::map<std::string, std::string>();
236-
237-
headers["Custom-Header1"] = "value1";
238-
headers["Custom-Header2"] = "value2";
239-
240-
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();
241-
244+
HttpRequest httpRequest("https://api.myproject.com");
245+
246+
// You can send custom headers as key-value pairs
247+
auto response = httpRequest
248+
.addHeader("Custom-Header1", "value1")
249+
.addHeader("Custom-Header2", "value2")
250+
.send()
251+
.get();
252+
242253
std::cout << "Succeed: " << response.succeed << std::endl;
243254

244255
return 0;
@@ -248,8 +259,7 @@ int main() {
248259

249260
## POST request with form data
250261

251-
Next is submitting form data via HTTP POST. All you have to do is use **"postRequest"** instead
252-
of **"getRequest"**. You can pass the form data to the **"payload"** variable as seen in the sample code below.
262+
Next is submitting form data via HTTP POST. All you have to do is use **"setMethod"** to change HTTP method type. You can pass the form data with **"setPaylod"** method as seen in the sample code below.
253263

254264
```cpp
255265
#include <fstream>
@@ -259,12 +269,14 @@ using namespace lklibs;
259269

260270
int main() {
261271

262-
HttpClient httpClient;
272+
HttpRequest httpRequest("https://api.myproject.com");
263273

264274
// You can send a POST request with form data in the payload
265-
std::string payload = "param1=7&param2=test";
266-
267-
auto response = httpClient.postRequest("https://api.myproject.com", payload).get();
275+
auto response = httpRequest
276+
.setMethod(HttpMethod::POST)
277+
.setPayload("param1=7&param2=test")
278+
.send()
279+
.get();
268280

269281
std::cout << "Succeed: " << response.succeed << std::endl;
270282
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -288,16 +300,15 @@ using namespace lklibs;
288300

289301
int main() {
290302

291-
HttpClient httpClient;
292-
293-
std::string payload = R"({"param1": 7, "param2": "test"})";
294-
303+
HttpRequest httpRequest("https://api.myproject.com");
304+
295305
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
296-
auto headers = std::map<std::string, std::string>();
297-
298-
headers["Content-Type"] = "application/json";
299-
300-
auto response = httpClient.postRequest("https://api.myproject.com", payload, headers).get();
306+
auto response = httpRequest
307+
.setMethod(HttpMethod::POST)
308+
.setPayload(R"({"param1": 7, "param2": "test"})")
309+
.addHeader("Content-Type", "application/json")
310+
.send()
311+
.get();
301312

302313
std::cout << "Succeed: " << response.succeed << std::endl;
303314
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -320,13 +331,26 @@ using namespace lklibs;
320331

321332
int main() {
322333

323-
HttpClient httpClient;
334+
HttpRequest httpRequest1("https://api.myproject.com");
324335

325-
std::string payload = "param1=7&param2=test";
336+
auto future1 = httpRequest
337+
.setMethod(HttpMethod::PUT)
338+
.setPayload("param1=7&param2=test")
339+
.send();
340+
341+
HttpRequest httpRequest2("https://api.myproject.com");
326342

327-
auto future1 = httpClient.putRequest("https://api.myproject.com", payload);
328-
auto future2 = httpClient.deleteRequest("https://api.myproject.com", payload);
329-
auto future3 = httpClient.patchRequest("https://api.myproject.com?param1=7&param2=test");
343+
auto future2 = httpRequest
344+
.setMethod(HttpMethod::DELETE_)
345+
.setPayload("param1=7&param2=test")
346+
.send();
347+
348+
HttpRequest httpRequest3("https://api.myproject.com");
349+
350+
auto future3 = httpRequest
351+
.setMethod(HttpMethod::PATCH)
352+
.setQueryString("param1=7&param2=test")
353+
.send();
330354

331355
auto response1 = future1.get();
332356
auto response2 = future2.get();
@@ -339,9 +363,8 @@ int main() {
339363

340364
## How to ignore SSL certificate errors?
341365

342-
If you need to ignore SSL certificate errors for any valid reason, you can continue
343-
working by passing **"true"** value to the **"ignoreSslErrors"** variable of the
344-
HttpClient class.
366+
If you need to ignore SSL certificate errors for any valid reason, you can call "ignoreSslErrors"
367+
method before sending the request.
345368

346369
```cpp
347370
#include <fstream>
@@ -351,12 +374,13 @@ using namespace lklibs;
351374

352375
int main() {
353376

354-
HttpClient httpClient;
355-
356-
// If you need to ignore SSL errors, you can set the "ignoreSslErrors" field to true
357-
httpClient.ignoreSslErrors = true;
377+
HttpRequest httpRequest("https://api.myinvalidssl.com");
358378

359-
auto response = httpClient.getRequest("https://api.myinvalidssl.com").get();
379+
// If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
380+
auto response = httpRequest
381+
.ignoreSslErrors()
382+
.send()
383+
.get();
360384

361385
return 0;
362386
}
@@ -381,58 +405,21 @@ section to the documentation.
381405

382406
## Full function list
383407

384-
You can find the complete list of functions in the library below. In fact, they are just
385-
overloaded versions of 5 functions in total.
408+
You can find the complete list of functions in the library below. Since all methods except
409+
send return the class itself, so they can be added one after the other like a chain.
386410

387411
> [!TIP]
388412
> All methods and parameters descriptions are also available within the code as comment for IDEs.
389413
390414
```cpp
391-
- getRequest
392-
- std::future<HttpResult> getRequest(const std::string &url)
393-
- std::future<HttpResult> getRequest(const std::string &url, bool returnAsBinary)
394-
- std::future<HttpResult> getRequest(const std::string &url, const std::map<std::string, std::string> &headers)
395-
- std::future<HttpResult> getRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
396-
397-
398-
- postRequest
399-
- std::future<HttpResult> postRequest(const std::string &url)
400-
- std::future<HttpResult> postRequest(const std::string &url, const std::string &payload)
401-
- std::future<HttpResult> postRequest(const std::string &url, bool returnAsBinary)
402-
- std::future<HttpResult> postRequest(const std::string &url, const std::map<std::string, std::string> &headers)
403-
- std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
404-
- std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
405-
- std::future<HttpResult> postRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
406-
- std::future<HttpResult> postRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
407-
408-
409-
- putRequest
410-
- std::future<HttpResult> putRequest(const std::string &url)
411-
- std::future<HttpResult> putRequest(const std::string &url, const std::string &payload)
412-
- std::future<HttpResult> putRequest(const std::string &url, bool returnAsBinary)
413-
- std::future<HttpResult> putRequest(const std::string &url, const std::map<std::string, std::string> &headers)
414-
- std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
415-
- std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
416-
- std::future<HttpResult> putRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
417-
- std::future<HttpResult> putRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
418-
419-
420-
- deleteRequest
421-
- std::future<HttpResult> deleteRequest(const std::string &url)
422-
- std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload)
423-
- std::future<HttpResult> deleteRequest(const std::string &url, bool returnAsBinary)
424-
- std::future<HttpResult> deleteRequest(const std::string &url, const std::map<std::string, std::string> &headers)
425-
- std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary)
426-
- std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, const std::map<std::string, std::string> &headers)
427-
- std::future<HttpResult> deleteRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
428-
- std::future<HttpResult> deleteRequest(const std::string &url, const std::string &payload, bool returnAsBinary, const std::map<std::string, std::string> &headers)
429-
430-
431-
- patchRequest
432-
- std::future<HttpResult> patchRequest(const std::string &url)
433-
- std::future<HttpResult> patchRequest(const std::string &url, bool returnAsBinary)
434-
- std::future<HttpResult> patchRequest(const std::string &url, const std::map<std::string, std::string> &headers)
435-
- std::future<HttpResult> patchRequest(const std::string &url, bool returnAsBinary, const std::map<std::string, std::string> &headers)
415+
- HttpRequest &setMethod(const HttpMethod &method) noexcept
416+
- HttpRequest &setQueryString(const std::string &queryString) noexcept
417+
- HttpRequest &setPayload(const std::string &payload) noexcept
418+
- HttpRequest &returnAsBinary() noexcept
419+
- HttpRequest &ignoreSslErrors() noexcept
420+
- HttpRequest &addHeader(const std::string &key, const std::string &value) noexcept
421+
- std::future<HttpResult> send() noexcept
422+
436423
```
437424
438425

0 commit comments

Comments
 (0)