From bbaebe594ea7a3b6afac4e40fbb367ed7b679e94 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Thu, 24 Oct 2019 17:30:25 +0200 Subject: NETWORKING: Reworked PostRequest to a more universal API --- backends/networking/curl/postrequest.cpp | 36 +++++++++++++++++++++++++++----- backends/networking/curl/postrequest.h | 12 +++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/backends/networking/curl/postrequest.cpp b/backends/networking/curl/postrequest.cpp index 1dec36b670..5e0bc1aa88 100644 --- a/backends/networking/curl/postrequest.cpp +++ b/backends/networking/curl/postrequest.cpp @@ -28,10 +28,11 @@ namespace Networking { -PostRequest::PostRequest(Common::String url, byte *postData, int postLen, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb): +PostRequest::PostRequest(Common::String url, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _url(url), _jsonCallback(cb), - _workingRequest(nullptr), _ignoreCallback(false), _postData(postData), _postLen(postLen) { - start(); + _workingRequest(nullptr), _ignoreCallback(false), _postData(nullptr), _postLen(0), _jsonData(nullptr) { + + _contentType = "application/octet-stream"; } PostRequest::~PostRequest() { @@ -41,6 +42,19 @@ PostRequest::~PostRequest() { delete _jsonCallback; } +void PostRequest::setPostData(byte *postData, int postLen) { + _postData = postData; + _postLen = postLen; + + _contentType = "application/octet-stream"; +} + +void PostRequest::setJSONData(Common::JSONValue *jsonData) { + _jsonData = jsonData; + + _contentType = "application/json"; +} + void PostRequest::start() { _ignoreCallback = true; if (_workingRequest) @@ -50,9 +64,21 @@ void PostRequest::start() { Networking::JsonCallback innerCallback = new Common::Callback(this, &PostRequest::responseCallback); Networking::ErrorCallback errorResponseCallback = new Common::Callback(this, &PostRequest::errorCallback); Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorResponseCallback, _url); - request->addHeader("Content-Type: application/json"); - request->setBuffer(_postData, _postLen); + if (_postData && _jsonData) { + warning("Error, both data and JSON present while calling %s", _url.c_str()); + + _jsonData = nullptr; + } + + request->addHeader(Common::String::format("Content-Type: %s", _contentType.c_str())); + + if (_postData) + request->setBuffer(_postData, _postLen); + + + if (_jsonData) + request->addPostField(Common::JSON::stringify(_jsonData)); _workingRequest = ConnMan.addRequest(request); } diff --git a/backends/networking/curl/postrequest.h b/backends/networking/curl/postrequest.h index 568979473c..8992d3a1a3 100644 --- a/backends/networking/curl/postrequest.h +++ b/backends/networking/curl/postrequest.h @@ -37,15 +37,23 @@ class PostRequest: public Networking::Request { byte *_postData; int _postLen; + Common::JSONValue *_jsonData; + + Common::String _contentType; - void start(); void responseCallback(Networking::JsonResponse response); void errorCallback(Networking::ErrorResponse error); public: - PostRequest(Common::String url, byte *postData, int postLen, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb); + PostRequest(Common::String url, Networking::JSONValueCallback cb, Networking::ErrorCallback ecb); virtual ~PostRequest(); + void start(); + + void setPostData(byte *postData, int postLen); + void setJSONData(Common::JSONValue *jsonData); + void setContentType(Common::String type) { _contentType = type; } + virtual void handle(); virtual void restart(); virtual Common::String date() const; -- cgit v1.2.3