Skip to content

Commit 9271436

Browse files
author
Levent KARAGÖL
committed
Pointers replaced with smart pointers
1 parent 99f98a3 commit 9271436

File tree

4 files changed

+267
-155
lines changed

4 files changed

+267
-155
lines changed

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ using namespace lklibs;
6969

7070
int main() {
7171

72+
HttpClient httpClient;
73+
7274
// The simplest but slowest method if multiple calls will be made
73-
auto response = HttpClient::getRequest("https://api.myproject.com?param1=7&param2=test").get();
75+
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test").get();
7476

7577
std::cout << "Succeed: " << response.succeed << std::endl;
7678
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -95,11 +97,13 @@ using namespace lklibs;
9597

9698
int main() {
9799

98-
auto response1 = HttpClient::getRequest("https://api.myproject.com/foo").get();
99-
auto response2 = HttpClient::getRequest("https://api.myproject.com/bar").get();
100-
auto response3 = HttpClient::getRequest("https://api.myproject.com/baz").get();
101-
auto response4 = HttpClient::getRequest("https://api.myproject.com/qux").get();
102-
auto response5 = HttpClient::getRequest("https://api.myproject.com/quux").get();
100+
HttpClient httpClient;
101+
102+
auto response1 = httpClient.getRequest("https://api.myproject.com/foo").get();
103+
auto response2 = httpClient.getRequest("https://api.myproject.com/bar").get();
104+
auto response3 = httpClient.getRequest("https://api.myproject.com/baz").get();
105+
auto response4 = httpClient.getRequest("https://api.myproject.com/qux").get();
106+
auto response5 = httpClient.getRequest("https://api.myproject.com/quux").get();
103107

104108
// Takes 2.5 seconds in total
105109

@@ -119,11 +123,11 @@ using namespace lklibs;
119123

120124
int main() {
121125

122-
auto future1 = HttpClient::getRequest("https://api.myproject.com/foo");
123-
auto future2 = HttpClient::getRequest("https://api.myproject.com/bar");
124-
auto future3 = HttpClient::getRequest("https://api.myproject.com/baz");
125-
auto future4 = HttpClient::getRequest("https://api.myproject.com/qux");
126-
auto future5 = HttpClient::getRequest("https://api.myproject.com/quux");
126+
auto future1 = httpClient.getRequest("https://api.myproject.com/foo");
127+
auto future2 = httpClient.getRequest("https://api.myproject.com/bar");
128+
auto future3 = httpClient.getRequest("https://api.myproject.com/baz");
129+
auto future4 = httpClient.getRequest("https://api.myproject.com/qux");
130+
auto future5 = httpClient.getRequest("https://api.myproject.com/quux");
127131

128132
auto response1 = future1.get();
129133
auto response2 = future2.get();
@@ -159,7 +163,7 @@ using namespace lklibs;
159163

160164
int main() {
161165

162-
auto response = HttpClient::getRequest("https://www.myinvalidurl.com").get();
166+
auto response = httpClient.getRequest("https://www.myinvalidurl.com").get();
163167

164168
// Instead of throwing an exception, the succeed field of the response object is set to false
165169
std::cout << "Succeed: " << response.succeed << std::endl;
@@ -192,7 +196,7 @@ using namespace lklibs;
192196
int main() {
193197

194198
// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
195-
auto response = HttpClient::getRequest("https://api.myproject.com/image/7", true).get();
199+
auto response = httpClient.getRequest("https://api.myproject.com/image/7", true).get();
196200

197201
std::cout << "Succeed: " << response.succeed << std::endl;
198202
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -224,7 +228,7 @@ int main() {
224228
headers["Custom-Header1"] = "value1";
225229
headers["Custom-Header2"] = "value2";
226230

227-
auto response = HttpClient::getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();
231+
auto response = httpClient.getRequest("https://api.myproject.com?param1=7&param2=test", headers).get();
228232

229233
std::cout << "Succeed: " << response.succeed << std::endl;
230234

@@ -249,7 +253,7 @@ int main() {
249253
// You can send a POST request with form data in the payload
250254
std::string payload = "param1=7&param2=test";
251255

252-
auto response = HttpClient::postRequest("https://api.myproject.com", payload).get();
256+
auto response = httpClient.postRequest("https://api.myproject.com", payload).get();
253257

254258
std::cout << "Succeed: " << response.succeed << std::endl;
255259
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -280,7 +284,7 @@ int main() {
280284

281285
headers["Content-Type"] = "application/json";
282286

283-
auto response = HttpClient::postRequest("https://api.myproject.com", payload, headers).get();
287+
auto response = httpClient.postRequest("https://api.myproject.com", payload, headers).get();
284288

285289
std::cout << "Succeed: " << response.succeed << std::endl;
286290
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -305,9 +309,9 @@ int main() {
305309

306310
std::string payload = "param1=7&param2=test";
307311

308-
auto future1 = HttpClient::putRequest("https://api.myproject.com", payload);
309-
auto future2 = HttpClient::deleteRequest("https://api.myproject.com", payload);
310-
auto future3 = HttpClient::patchRequest("https://api.myproject.com?param1=7&param2=test");
312+
auto future1 = httpClient.putRequest("https://api.myproject.com", payload);
313+
auto future2 = httpClient.deleteRequest("https://api.myproject.com", payload);
314+
auto future3 = httpClient.patchRequest("https://api.myproject.com?param1=7&param2=test");
311315

312316
auto response1 = future1.get();
313317
auto response2 = future2.get();

examples/main.cpp

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

66
void simpleGet() {
77

8+
HttpClient httpClient;
9+
810
// The simplest but slowest method if multiple calls will be made
9-
auto response = HttpClient::getRequest("https://httpbun.com/get?param1=7&param2=test").get();
11+
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test").get();
1012

1113
std::cout << "Succeed: " << response.succeed << std::endl;
1214
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -15,10 +17,12 @@ void simpleGet() {
1517

1618
void nonBlockingGet() {
1719

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

2327
// Then all the answers are received. Thus, 3 requests are sent in parallel
2428
auto response1 = future1.get();
@@ -40,8 +44,10 @@ void nonBlockingGet() {
4044

4145
void receiveBinaryData() {
4246

47+
HttpClient httpClient;
48+
4349
// If you need to retrieve binary data such as an image, just pass the "returnAsBinary" parameter as true
44-
auto response = HttpClient::getRequest("https://httpbun.com/bytes/100", true).get();
50+
auto response = httpClient.getRequest("https://httpbun.com/bytes/100", true).get();
4551

4652
std::cout << "Succeed: " << response.succeed << std::endl;
4753
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -52,8 +58,10 @@ void receiveBinaryData() {
5258

5359
void receiveError() {
5460

61+
HttpClient httpClient;
62+
5563
// This is an exception free library. If an error occurs, no exception is thrown
56-
auto response = HttpClient::getRequest("https://httpbun.com/not_found").get();
64+
auto response = httpClient.getRequest("https://httpbun.com/not_found").get();
5765

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

6876
void sendingHttpHeaders() {
6977

78+
HttpClient httpClient;
79+
7080
// You can send custom headers in a string/string map
7181
auto headers = std::map<std::string, std::string>();
7282

7383
headers["Custom-Header1"] = "value1";
7484
headers["Custom-Header2"] = "value2";
7585

76-
auto response = HttpClient::getRequest("https://httpbun.com/get?param1=7&param2=test", headers).get();
86+
auto response = httpClient.getRequest("https://httpbun.com/get?param1=7&param2=test", headers).get();
7787

7888
std::cout << "Succeed: " << response.succeed << std::endl;
7989
}
8090

8191
void simplePostWithFormData() {
8292

93+
HttpClient httpClient;
94+
8395
// You can send a POST request with form data in the payload
8496
std::string payload = "param1=7&param2=test";
8597

86-
auto response = HttpClient::postRequest("https://httpbun.com/post", payload).get();
98+
auto response = httpClient.postRequest("https://httpbun.com/post", payload).get();
8799

88100
std::cout << "Succeed: " << response.succeed << std::endl;
89101
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -92,14 +104,16 @@ void simplePostWithFormData() {
92104

93105
void simplePostWithJSONData() {
94106

107+
HttpClient httpClient;
108+
95109
std::string payload = R"({"param1": 7, "param2": "test"})";
96110

97111
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
98112
auto headers = std::map<std::string, std::string>();
99113

100114
headers["Content-Type"] = "application/json";
101115

102-
auto response = HttpClient::postRequest("https://httpbun.com/post", payload, headers).get();
116+
auto response = httpClient.postRequest("https://httpbun.com/post", payload, headers).get();
103117

104118
std::cout << "Succeed: " << response.succeed << std::endl;
105119
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -108,10 +122,12 @@ void simplePostWithJSONData() {
108122

109123
void simplePutWithFormData() {
110124

125+
HttpClient httpClient;
126+
111127
// You can send a PUT request with form data in the payload just like POST
112128
std::string payload = "param1=7&param2=test";
113129

114-
auto response = HttpClient::putRequest("https://httpbun.com/put", payload).get();
130+
auto response = httpClient.putRequest("https://httpbun.com/put", payload).get();
115131

116132
std::cout << "Succeed: " << response.succeed << std::endl;
117133
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -120,10 +136,12 @@ void simplePutWithFormData() {
120136

121137
void simpleDeleteWithFormData() {
122138

139+
HttpClient httpClient;
140+
123141
// You can send a DELETE request with form data in the payload just like POST
124142
std::string payload = "param1=7&param2=test";
125143

126-
auto response = HttpClient::deleteRequest("https://httpbun.com/delete", payload).get();
144+
auto response = httpClient.deleteRequest("https://httpbun.com/delete", payload).get();
127145

128146
std::cout << "Succeed: " << response.succeed << std::endl;
129147
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -132,8 +150,10 @@ void simpleDeleteWithFormData() {
132150

133151
void simplePatch() {
134152

153+
HttpClient httpClient;
154+
135155
// You can send a PATCH request with QueryString just like GET
136-
auto response = HttpClient::patchRequest("https://httpbun.com/patch?param1=7&param2=test").get();
156+
auto response = httpClient.patchRequest("https://httpbun.com/patch?param1=7&param2=test").get();
137157

138158
std::cout << "Succeed: " << response.succeed << std::endl;
139159
std::cout << "Http Status Code: " << response.statusCode << std::endl;

0 commit comments

Comments
 (0)