Skip to content

Commit 742e351

Browse files
committed
Prepare to refactor request
1 parent 297481e commit 742e351

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/http/http.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,24 @@ bool HTTP::error() {
234234

235235
// Get Json Object on web server.
236236
bool HTTP::request(const char* method, const char* endUrl, const char* data, Json::Object* jOutput, const char* content_type){
237+
Result result;
238+
request(method, endUrl, data, jOutput, result, content_type);
239+
return (result == OK);
240+
}
241+
242+
// 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){
237244

238245
std::string output;
239-
if(!request(method, endUrl, data, output, content_type)){
246+
request(method, endUrl, data, output, result, content_type);
247+
if(result != OK) {
240248

241249
// Give a second chance.
242250
disconnect();
243251

244-
if(!request(method, endUrl, data, output, content_type))
245-
return false;
252+
request(method, endUrl, data, output, result, content_type);
253+
if(result != OK)
254+
return;
246255
}
247256

248257
try {
@@ -252,7 +261,8 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, Jso
252261
}
253262
catch(Exception& e){
254263
printf("parser() failed in Getter. Exception caught: %s\n", e.what());
255-
return false;
264+
result = ERROR;
265+
return;
256266
}
257267
catch(std::exception& e){
258268
printf("parser() failed in Getter. std::exception caught: %s\n", e.what());
@@ -263,7 +273,7 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, Jso
263273
EXCEPTION("Unknown exception.");
264274
}
265275

266-
return true;
276+
result = OK;
267277
}
268278

269279
// Parse the message and split if necessary.
@@ -405,6 +415,12 @@ bool HTTP::write(const std::string& outgoing) {
405415

406416

407417
bool HTTP::request(const char* method, const char* endUrl, const char* data, std::string& output, const char* content_type){
418+
Result result;
419+
request(method, endUrl, data, output, result, content_type);
420+
return (result == OK);
421+
}
422+
423+
void HTTP::request(const char* method, const char* endUrl, const char* data, std::string& output, Result& result, const char* content_type){
408424

409425
/// Example of request.
410426
/// "POST /test.php HTTP/1.0\r\n"
@@ -427,18 +443,17 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, std
427443
assert( !error() );
428444
assert(output.empty());
429445

430-
if(!sendMessage(method, endUrl, data, content_type))
431-
return false;
446+
if(!sendMessage(method, endUrl, data, content_type)) {
447+
result = ERROR;
448+
return;
449+
}
432450

433-
Result readResult;
434-
readMessage(output, readResult);
435-
if(readResult != OK) {
451+
readMessage(output, result);
452+
if(result != OK) {
436453

437454
// Clear ouput in case we didn't get the full response.
438455
if(!output.empty())
439456
output.clear();
440-
441-
return false;
442457
}
443458

444459
if(_keepAlive)
@@ -456,7 +471,7 @@ bool HTTP::request(const char* method, const char* endUrl, const char* data, std
456471
}
457472
} */
458473

459-
return true;
474+
result = OK;
460475
}
461476

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

src/http/http.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,20 @@ class HTTP {
6666
HTTP(std::string url, bool keepAlive);
6767
~HTTP();
6868

69+
/// DEPRECATED
6970
/// Generic request that parses the result in Json::Object.
7071
bool request(const char* method, const char* endUrl, const char* data, Json::Object* root, const char* content_type = _APPLICATION_JSON);
7172

73+
/// 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);
75+
76+
/// DEPRECATED
7277
/// Generic request that stores result in the string.
7378
bool request(const char* method, const char* endUrl, const char* data, std::string& output, const char* content_type = _APPLICATION_JSON);
7479

80+
/// 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);
82+
7583
/// Generic get request to node.
7684
inline void get(const char* endUrl, const char* data, Json::Object* root){
7785
request("GET", endUrl, data, root);

0 commit comments

Comments
 (0)