ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

dropboxcreatedirectoryrequest.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/cloud/dropbox/dropboxcreatedirectoryrequest.h"
00024 #include "backends/cloud/storage.h"
00025 #include "backends/networking/curl/connectionmanager.h"
00026 #include "backends/networking/curl/curljsonrequest.h"
00027 #include "backends/networking/curl/networkreadstream.h"
00028 #include "common/json.h"
00029 
00030 namespace Cloud {
00031 namespace Dropbox {
00032 
00033 #define DROPBOX_API_CREATE_FOLDER "https://api.dropboxapi.com/2/files/create_folder"
00034 
00035 DropboxCreateDirectoryRequest::DropboxCreateDirectoryRequest(Common::String token, Common::String path, Storage::BoolCallback cb, Networking::ErrorCallback ecb):
00036     Networking::Request(nullptr, ecb), _token(token), _path(path), _boolCallback(cb),
00037     _workingRequest(nullptr), _ignoreCallback(false) {
00038     start();
00039 }
00040 
00041 DropboxCreateDirectoryRequest::~DropboxCreateDirectoryRequest() {
00042     _ignoreCallback = true;
00043     if (_workingRequest)
00044         _workingRequest->finish();
00045     delete _boolCallback;
00046 }
00047 
00048 void DropboxCreateDirectoryRequest::start() {
00049     _ignoreCallback = true;
00050     if (_workingRequest)
00051         _workingRequest->finish();
00052     _ignoreCallback = false;
00053 
00054     Networking::JsonCallback innerCallback = new Common::Callback<DropboxCreateDirectoryRequest, Networking::JsonResponse>(this, &DropboxCreateDirectoryRequest::responseCallback);
00055     Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxCreateDirectoryRequest, Networking::ErrorResponse>(this, &DropboxCreateDirectoryRequest::errorCallback);
00056     Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorResponseCallback, DROPBOX_API_CREATE_FOLDER);
00057     request->addHeader("Authorization: Bearer " + _token);
00058     request->addHeader("Content-Type: application/json");
00059 
00060     Common::JSONObject jsonRequestParameters;
00061     jsonRequestParameters.setVal("path", new Common::JSONValue(_path));
00062     Common::JSONValue value(jsonRequestParameters);
00063     request->addPostField(Common::JSON::stringify(&value));
00064 
00065     _workingRequest = ConnMan.addRequest(request);
00066 }
00067 
00068 void DropboxCreateDirectoryRequest::responseCallback(Networking::JsonResponse response) {
00069     Common::JSONValue *json = response.value;
00070     _workingRequest = nullptr;
00071     if (_ignoreCallback) {
00072         delete json;
00073         return;
00074     }
00075     if (response.request) _date = response.request->date();
00076 
00077     Networking::ErrorResponse error(this);
00078     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00079     if (rq && rq->getNetworkReadStream())
00080         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00081 
00082     if (json == nullptr) {
00083         error.response = "Failed to parse JSON, null passed!";
00084         finishError(error);
00085         return;
00086     }
00087 
00088     if (!json->isObject()) {
00089         error.response = "Passed JSON is not an object!";
00090         finishError(error);
00091         delete json;
00092         return;
00093     }
00094 
00095     Common::JSONObject info = json->asObject();
00096     if (info.contains("id")) {
00097         finishCreation(true);
00098     } else {
00099         if (Networking::CurlJsonRequest::jsonContainsString(info, "error_summary", "DropboxCreateDirectoryRequest")) {
00100             Common::String summary = info.getVal("error_summary")->asString();
00101             if (summary.contains("path") && summary.contains("conflict") && summary.contains("folder")) {
00102                 // existing directory - not an error for CreateDirectoryRequest
00103                 finishCreation(false);
00104                 delete json;
00105                 return;
00106             }
00107         }
00108         error.response = json->stringify(true);
00109         finishError(error);
00110     }
00111 
00112     delete json;
00113 }
00114 
00115 void DropboxCreateDirectoryRequest::errorCallback(Networking::ErrorResponse error) {
00116     _workingRequest = nullptr;
00117     if (_ignoreCallback)
00118         return;
00119     if (error.request)
00120         _date = error.request->date();
00121     finishError(error);
00122 }
00123 
00124 void DropboxCreateDirectoryRequest::handle() {}
00125 
00126 void DropboxCreateDirectoryRequest::restart() { start(); }
00127 
00128 Common::String DropboxCreateDirectoryRequest::date() const { return _date; }
00129 
00130 void DropboxCreateDirectoryRequest::finishCreation(bool success) {
00131     Request::finishSuccess();
00132     if (_boolCallback)
00133         (*_boolCallback)(Storage::BoolResponse(this, success));
00134 }
00135 
00136 } // End of namespace Dropbox
00137 } // End of namespace Cloud


Generated on Sat Feb 9 2019 05:00:30 for ResidualVM by doxygen 1.7.1
curved edge   curved edge