Skip to content

Commit ecc58ae

Browse files
committed
Refactored request
1 parent 742e351 commit ecc58ae

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/http/http.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,18 +240,20 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, Jso
240240
}
241241

242242
// Get Json Object on web server.
243-
void HTTP::request(const char* method, const char* endUrl, const char* data, Json::Object* jOutput, Result& result, const char* content_type){
243+
unsigned int HTTP::request(const char* method, const char* endUrl, const char* data, Json::Object* jOutput, Result& result, const char* content_type){
244+
245+
unsigned int statusCode = 0;
244246

245247
std::string output;
246-
request(method, endUrl, data, output, result, content_type);
248+
statusCode = request(method, endUrl, data, output, result, content_type);
247249
if(result != OK) {
248250

249251
// Give a second chance.
250252
disconnect();
251253

252-
request(method, endUrl, data, output, result, content_type);
254+
statusCode = request(method, endUrl, data, output, result, content_type);
253255
if(result != OK)
254-
return;
256+
return statusCode;
255257
}
256258

257259
try {
@@ -262,7 +264,7 @@ void HTTP::request(const char* method, const char* endUrl, const char* data, Jso
262264
catch(Exception& e){
263265
printf("parser() failed in Getter. Exception caught: %s\n", e.what());
264266
result = ERROR;
265-
return;
267+
return statusCode;
266268
}
267269
catch(std::exception& e){
268270
printf("parser() failed in Getter. std::exception caught: %s\n", e.what());
@@ -274,6 +276,7 @@ void HTTP::request(const char* method, const char* endUrl, const char* data, Jso
274276
}
275277

276278
result = OK;
279+
return statusCode;
277280
}
278281

279282
// Parse the message and split if necessary.
@@ -420,7 +423,7 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, std
420423
return (result == OK);
421424
}
422425

423-
void HTTP::request(const char* method, const char* endUrl, const char* data, std::string& output, Result& result, const char* content_type){
426+
unsigned int HTTP::request(const char* method, const char* endUrl, const char* data, std::string& output, Result& result, const char* content_type){
424427

425428
/// Example of request.
426429
/// "POST /test.php HTTP/1.0\r\n"
@@ -443,12 +446,14 @@ void HTTP::request(const char* method, const char* endUrl, const char* data, std
443446
assert( !error() );
444447
assert(output.empty());
445448

449+
unsigned int statusCode = 0;
450+
446451
if(!sendMessage(method, endUrl, data, content_type)) {
447452
result = ERROR;
448-
return;
453+
return statusCode;
449454
}
450455

451-
readMessage(output, result);
456+
statusCode = readMessage(output, result);
452457
if(result != OK) {
453458

454459
// Clear ouput in case we didn't get the full response.
@@ -472,6 +477,7 @@ void HTTP::request(const char* method, const char* endUrl, const char* data, std
472477
} */
473478

474479
result = OK;
480+
return statusCode;
475481
}
476482

477483
// Whole process to read the response from HTTP server.

src/http/http.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ class HTTP {
7171
bool request(const char* method, const char* endUrl, const char* data, Json::Object* root, const char* content_type = _APPLICATION_JSON);
7272

7373
/// Generic request that parses the result in Json::Object.
74-
void request(const char* method, const char* endUrl, const char* data, Json::Object* root, Result& result, const char* content_type = _APPLICATION_JSON);
74+
unsigned int request(const char* method, const char* endUrl, const char* data, Json::Object* root, Result& result, const char* content_type = _APPLICATION_JSON);
7575

7676
/// DEPRECATED
7777
/// Generic request that stores result in the string.
7878
bool request(const char* method, const char* endUrl, const char* data, std::string& output, const char* content_type = _APPLICATION_JSON);
7979

8080
/// Generic request that stores result in the string.
81-
void request(const char* method, const char* endUrl, const char* data, std::string& output, Result& result, const char* content_type = _APPLICATION_JSON);
81+
unsigned int request(const char* method, const char* endUrl, const char* data, std::string& output, Result& result, const char* content_type = _APPLICATION_JSON);
8282

8383
/// Generic get request to node.
8484
inline void get(const char* endUrl, const char* data, Json::Object* root){

0 commit comments

Comments
 (0)