Skip to content

Commit 7d1db7d

Browse files
author
Levent KARAGÖL
committed
Method overloading structure converted to Builder Pattern
1 parent 176453a commit 7d1db7d

File tree

3 files changed

+329
-540
lines changed

3 files changed

+329
-540
lines changed

examples/main.cpp

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

66
void simpleGet() {
77

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

1010
// The simplest but slowest method if multiple calls will be made
11-
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test").get();
11+
auto response = httpClient
12+
.setQueryString("param1=7&param2=test")
13+
.send()
14+
.get();
1215

1316
std::cout << "Succeed: " << response.succeed << std::endl;
1417
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -17,12 +20,14 @@ void simpleGet() {
1720

1821
void nonBlockingGet() {
1922

20-
HttpClient httpClient;
23+
HttpClient httpClient1("https://httpbun.com/get");
24+
HttpClient httpClient2("https://httpbun.com/get");
25+
HttpClient httpClient3("https://httpbun.com/get");
2126

2227
// All requests are made one after the other without waiting for a response
23-
auto future1 = httpClient.getRequest("https://httpbun.com/get?param1=1&param2=test1");
24-
auto future2 = httpClient.getRequest("https://httpbun.com/get?param1=2&param2=test2");
25-
auto future3 = httpClient.getRequest("https://httpbun.com/get?param1=3&param2=test3");
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();
2631

2732
// Then all the answers are received. Thus, 3 requests are sent in parallel
2833
auto response1 = future1.get();
@@ -44,10 +49,13 @@ void nonBlockingGet() {
4449

4550
void receiveBinaryData() {
4651

47-
HttpClient httpClient;
52+
HttpClient httpClient("https://httpbun.com/bytes/100");
4853

49-
// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
50-
auto response = httpClient.getRequest("https://httpbun.com/bytes/100", true).get();
54+
// If you need to retrieve binary data such as an image, just call the "returnAsBinary" method
55+
auto response = httpClient
56+
.returnAsBinary()
57+
.send()
58+
.get();
5159

5260
std::cout << "Succeed: " << response.succeed << std::endl;
5361
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -58,10 +66,10 @@ void receiveBinaryData() {
5866

5967
void receiveError() {
6068

61-
HttpClient httpClient;
69+
HttpClient httpClient("https://httpbun.com/not_found");
6270

6371
// This is an exception free library. If an error occurs, no exception is thrown
64-
auto response = httpClient.getRequest("https://httpbun.com/not_found").get();
72+
auto response = httpClient.send().get();
6573

6674
// Instead, the succeed field of the response object is set to false
6775
std::cout << "Succeed: " << response.succeed << std::endl;
@@ -75,27 +83,28 @@ void receiveError() {
7583

7684
void sendingHttpHeaders() {
7785

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

80-
// You can send custom headers in a string/string map
81-
auto headers = std::map<std::string, std::string>();
82-
83-
headers["Custom-Header1"] = "value1";
84-
headers["Custom-Header2"] = "value2";
85-
86-
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test", headers).get();
88+
// You can send custom headers as key-value pairs
89+
auto response = httpClient
90+
.addHeader("Custom-Header1", "value1")
91+
.addHeader("Custom-Header2", "value2")
92+
.send()
93+
.get();
8794

8895
std::cout << "Succeed: " << response.succeed << std::endl;
8996
}
9097

9198
void simplePostWithFormData() {
9299

93-
HttpClient httpClient;
100+
HttpClient httpClient("https://httpbun.com/post");
94101

95102
// You can send a POST request with form data in the payload
96-
std::string payload = "param1=7&param2=test";
97-
98-
auto response = httpClient.postRequest("https://httpbun.com/post", payload).get();
103+
auto response = httpClient
104+
.setMethod(HttpMethod::POST)
105+
.setPayload("param1=7&param2=test")
106+
.send()
107+
.get();
99108

100109
std::cout << "Succeed: " << response.succeed << std::endl;
101110
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -104,16 +113,15 @@ void simplePostWithFormData() {
104113

105114
void simplePostWithJSONData() {
106115

107-
HttpClient httpClient;
108-
109-
std::string payload = R"({"param1": 7, "param2": "test"})";
116+
HttpClient httpClient("https://httpbun.com/post");
110117

111118
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
112-
auto headers = std::map<std::string, std::string>();
113-
114-
headers["Content-Type"] = "application/json";
115-
116-
auto response = httpClient.postRequest("https://httpbun.com/post", payload, headers).get();
119+
auto response = httpClient
120+
.setMethod(HttpMethod::POST)
121+
.setPayload(R"({"param1": 7, "param2": "test"})")
122+
.addHeader("Content-Type", "application/json")
123+
.send()
124+
.get();
117125

118126
std::cout << "Succeed: " << response.succeed << std::endl;
119127
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -122,12 +130,14 @@ void simplePostWithJSONData() {
122130

123131
void simplePutWithFormData() {
124132

125-
HttpClient httpClient;
133+
HttpClient httpClient("https://httpbun.com/put");
126134

127135
// You can send a PUT request with form data in the payload just like POST
128-
std::string payload = "param1=7&param2=test";
129-
130-
auto response = httpClient.putRequest("https://httpbun.com/put", payload).get();
136+
auto response = httpClient
137+
.setMethod(HttpMethod::PUT)
138+
.setPayload("param1=7&param2=test")
139+
.send()
140+
.get();
131141

132142
std::cout << "Succeed: " << response.succeed << std::endl;
133143
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -136,12 +146,14 @@ void simplePutWithFormData() {
136146

137147
void simpleDeleteWithFormData() {
138148

139-
HttpClient httpClient;
149+
HttpClient httpClient("https://httpbun.com/delete");
140150

141151
// You can send a DELETE request with form data in the payload just like POST
142-
std::string payload = "param1=7&param2=test";
143-
144-
auto response = httpClient.deleteRequest("https://httpbun.com/delete", payload).get();
152+
auto response = httpClient
153+
.setMethod(HttpMethod::DELETE_)
154+
.setPayload("param1=7&param2=test")
155+
.send()
156+
.get();
145157

146158
std::cout << "Succeed: " << response.succeed << std::endl;
147159
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -150,10 +162,14 @@ void simpleDeleteWithFormData() {
150162

151163
void simplePatch() {
152164

153-
HttpClient httpClient;
165+
HttpClient httpClient("https://httpbun.com/patch");
154166

155167
// You can send a PATCH request with QueryString just like GET
156-
auto response = httpClient.patchRequest("https://httpbun.com/patch?param1=7&param2=test").get();
168+
auto response = httpClient
169+
.setMethod(HttpMethod::PATCH)
170+
.setQueryString("param1=7&param2=test")
171+
.send()
172+
.get();
157173

158174
std::cout << "Succeed: " << response.succeed << std::endl;
159175
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -162,12 +178,10 @@ void simplePatch() {
162178

163179
void ignoreSslErrors() {
164180

165-
HttpClient httpClient;
166-
167-
// If you need to ignore SSL errors, you can set the "ignoreSslErrors" field to true
168-
httpClient.ignoreSslErrors = true;
181+
HttpClient httpClient("https://self-signed-cert.httpbun.com");
169182

170-
auto response = httpClient.getRequest("https://self-signed-cert.httpbun.com").get();
183+
// If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
184+
auto response = httpClient.ignoreSslErrors().send().get();
171185

172186
std::cout << "Succeed: " << response.succeed << std::endl;
173187
std::cout << "Http Status Code: " << response.statusCode << std::endl;

0 commit comments

Comments
 (0)