aboutsummaryrefslogtreecommitdiff
path: root/backends/networking/curl/curljsonrequest.cpp
diff options
context:
space:
mode:
authorAlexander Tkachev2016-05-31 01:51:32 +0600
committerAlexander Tkachev2016-08-24 16:07:55 +0600
commiteb63b50b7f0841e40365f3fbafa9810e8b190872 (patch)
tree4f348f12298c15e8a5885b5a74ce3788493b9a7e /backends/networking/curl/curljsonrequest.cpp
parent001b417f33beeb3b2da11f58105b971dc7e6f600 (diff)
downloadscummvm-rg350-eb63b50b7f0841e40365f3fbafa9810e8b190872.tar.gz
scummvm-rg350-eb63b50b7f0841e40365f3fbafa9810e8b190872.tar.bz2
scummvm-rg350-eb63b50b7f0841e40365f3fbafa9810e8b190872.zip
CLOUD: Refactor Request
Added ErrorResponse and ErrorCallback. Each Request now has an ErrorCallback, which should be called instead of usual callback in case of failure.
Diffstat (limited to 'backends/networking/curl/curljsonrequest.cpp')
-rw-r--r--backends/networking/curl/curljsonrequest.cpp30
1 files changed, 14 insertions, 16 deletions
diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp
index fee0932129..df982bc814 100644
--- a/backends/networking/curl/curljsonrequest.cpp
+++ b/backends/networking/curl/curljsonrequest.cpp
@@ -31,8 +31,8 @@
namespace Networking {
-CurlJsonRequest::CurlJsonRequest(JsonCallback cb, Common::String url):
- CurlRequest(0, url), _jsonCallback(cb), _contentsStream(DisposeAfterUse::YES) {}
+CurlJsonRequest::CurlJsonRequest(JsonCallback cb, ErrorCallback ecb, Common::String url):
+ CurlRequest(nullptr, ecb, url), _jsonCallback(cb), _contentsStream(DisposeAfterUse::YES) {}
CurlJsonRequest::~CurlJsonRequest() { delete _jsonCallback; }
@@ -65,34 +65,32 @@ void CurlJsonRequest::handle() {
if (_contentsStream.write(buf, readBytes) != readBytes)
warning("MemoryWriteStreamDynamic was unable to write all the bytes");
- if (_stream->eos()) {
- if (_stream->httpResponseCode() != 200)
- warning("HTTP response code is not 200 OK (it's %ld)", _stream->httpResponseCode());
-
+ if (_stream->eos()) {
char *contents = getPreparedContents();
- if (_stream->httpResponseCode() != 200)
- debug("%s", contents);
Common::JSONValue *json = Common::JSON::parse(contents);
- finishJson(json);
+ if (json) {
+ finishSuccess(json); //it's JSON even if's not 200 OK? That's fine!..
+ } else {
+ if (_stream->httpResponseCode() == 200) //no JSON, but 200 OK? That's fine!..
+ finishSuccess(nullptr);
+ else
+ finishError(ErrorResponse(this, false, true, contents, _stream->httpResponseCode()));
+ }
}
}
}
void CurlJsonRequest::restart() {
if (_stream) delete _stream;
- _stream = 0;
+ _stream = nullptr;
_contentsStream = Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES);
//with no stream available next handle() will create another one
}
-void CurlJsonRequest::finishJson(Common::JSONValue *json) {
- Request::finish();
+void CurlJsonRequest::finishSuccess(Common::JSONValue *json) {
+ Request::finishSuccess();
if (_jsonCallback) (*_jsonCallback)(JsonResponse(this, json)); //potential memory leak, free it in your callbacks!
else delete json;
}
-void CurlJsonRequest::finish() {
- finishJson(0);
-}
-
} // End of namespace Networking