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

dropboxinforequest.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/dropboxinforequest.h"
00024 #include "backends/cloud/cloudmanager.h"
00025 #include "backends/cloud/storage.h"
00026 #include "backends/networking/curl/connectionmanager.h"
00027 #include "backends/networking/curl/curljsonrequest.h"
00028 #include "backends/networking/curl/networkreadstream.h"
00029 #include "common/json.h"
00030 
00031 namespace Cloud {
00032 namespace Dropbox {
00033 
00034 #define DROPBOX_API_GET_CURRENT_ACCOUNT "https://api.dropboxapi.com/2/users/get_current_account"
00035 #define DROPBOX_API_GET_SPACE_USAGE "https://api.dropboxapi.com/2/users/get_space_usage"
00036 
00037 DropboxInfoRequest::DropboxInfoRequest(Common::String token, Storage::StorageInfoCallback cb, Networking::ErrorCallback ecb):
00038     Networking::Request(nullptr, ecb), _token(token), _infoCallback(cb),
00039     _workingRequest(nullptr), _ignoreCallback(false) {
00040     start();
00041 }
00042 
00043 DropboxInfoRequest::~DropboxInfoRequest() {
00044     _ignoreCallback = true;
00045     if (_workingRequest)
00046         _workingRequest->finish();
00047     delete _infoCallback;
00048 }
00049 
00050 void DropboxInfoRequest::start() {
00051     _ignoreCallback = true;
00052     if (_workingRequest)
00053         _workingRequest->finish();
00054     _ignoreCallback = false;
00055 
00056     Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, Networking::JsonResponse>(this, &DropboxInfoRequest::userResponseCallback);
00057     Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxInfoRequest, Networking::ErrorResponse>(this, &DropboxInfoRequest::errorCallback);
00058     Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorResponseCallback, DROPBOX_API_GET_CURRENT_ACCOUNT);
00059     request->addHeader("Authorization: Bearer " + _token);
00060     request->addHeader("Content-Type: application/json");
00061     request->addPostField("null"); //use POST
00062 
00063     _workingRequest = ConnMan.addRequest(request);
00064 }
00065 
00066 void DropboxInfoRequest::userResponseCallback(Networking::JsonResponse response) {
00067     Common::JSONValue *json = response.value;
00068     _workingRequest = nullptr;
00069     if (_ignoreCallback) {
00070         delete json;
00071         return;
00072     }
00073 
00074     Networking::ErrorResponse error(this);
00075     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00076     if (rq && rq->getNetworkReadStream())
00077         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00078 
00079     if (json == nullptr) {
00080         error.response = "Failed to parse JSON, null passed!";
00081         finishError(error);
00082         return;
00083     }
00084 
00085     if (!json->isObject()) {
00086         error.response = "Passed JSON is not an object!";
00087         finishError(error);
00088         delete json;
00089         return;
00090     }
00091 
00092     //Dropbox documentation states there are no errors for this API method
00093     Common::JSONObject info = json->asObject();
00094     if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "name", "DropboxInfoRequest") &&
00095         Networking::CurlJsonRequest::jsonIsObject(info.getVal("name"), "DropboxInfoRequest")) {
00096         Common::JSONObject nameInfo = info.getVal("name")->asObject();
00097         if (Networking::CurlJsonRequest::jsonContainsString(nameInfo, "display_name", "DropboxInfoRequest")) {
00098             _name = nameInfo.getVal("display_name")->asString();
00099         }
00100     }
00101     if (Networking::CurlJsonRequest::jsonContainsString(info, "account_id", "DropboxInfoRequest")) {
00102         _uid = info.getVal("account_id")->asString();
00103     }
00104     if (Networking::CurlJsonRequest::jsonContainsString(info, "email", "DropboxInfoRequest")) {
00105         _email = info.getVal("email")->asString();
00106     }
00107     CloudMan.setStorageUsername(kStorageDropboxId, _email);
00108     delete json;
00109 
00110     Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, Networking::JsonResponse>(this, &DropboxInfoRequest::quotaResponseCallback);
00111     Networking::ErrorCallback errorResponseCallback = new Common::Callback<DropboxInfoRequest, Networking::ErrorResponse>(this, &DropboxInfoRequest::errorCallback);
00112     Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorResponseCallback, DROPBOX_API_GET_SPACE_USAGE);
00113     request->addHeader("Authorization: Bearer " + _token);
00114     request->addHeader("Content-Type: application/json");
00115     request->addPostField("null"); //use POST
00116 
00117     _workingRequest = ConnMan.addRequest(request);
00118 }
00119 
00120 void DropboxInfoRequest::quotaResponseCallback(Networking::JsonResponse response) {
00121     Common::JSONValue *json = response.value;
00122     _workingRequest = nullptr;
00123     if (_ignoreCallback) {
00124         delete json;
00125         return;
00126     }
00127 
00128     Networking::ErrorResponse error(this);
00129     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00130     if (rq && rq->getNetworkReadStream())
00131         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00132 
00133     if (json == nullptr) {
00134         error.response = "Failed to parse JSON, null passed!";
00135         finishError(error);
00136         return;
00137     }
00138 
00139     if (!json->isObject()) {
00140         error.response = "Passed JSON is not an object!";
00141         finishError(error);
00142         delete json;
00143         return;
00144     }
00145 
00146     //Dropbox documentation states there are no errors for this API method
00147     Common::JSONObject info = json->asObject();
00148 
00149     if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(info, "used", "DropboxInfoRequest")) {
00150         error.response = "Passed JSON misses 'used' attribute!";
00151         finishError(error);
00152         delete json;
00153         return;
00154     }
00155 
00156     uint64 used = info.getVal("used")->asIntegerNumber(), allocated = 0;
00157 
00158     if (Networking::CurlJsonRequest::jsonContainsAttribute(info, "allocation", "DropboxInfoRequest") &&
00159         Networking::CurlJsonRequest::jsonIsObject(info.getVal("allocation"), "DropboxInfoRequest")) {
00160         Common::JSONObject allocation = info.getVal("allocation")->asObject();
00161         if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(allocation, "allocated", "DropboxInfoRequest")) {
00162             error.response = "Passed JSON misses 'allocation/allocated' attribute!";
00163             finishError(error);
00164             delete json;
00165             return;
00166         }
00167 
00168         allocated = allocation.getVal("allocated")->asIntegerNumber();
00169     }
00170 
00171     finishInfo(StorageInfo(_uid, _name, _email, used, allocated));
00172     delete json;
00173 }
00174 
00175 void DropboxInfoRequest::errorCallback(Networking::ErrorResponse error) {
00176     _workingRequest = nullptr;
00177     if (_ignoreCallback) return;
00178     finishError(error);
00179 }
00180 
00181 void DropboxInfoRequest::handle() {}
00182 
00183 void DropboxInfoRequest::restart() { start(); }
00184 
00185 void DropboxInfoRequest::finishInfo(StorageInfo info) {
00186     Request::finishSuccess();
00187     if (_infoCallback)
00188         (*_infoCallback)(Storage::StorageInfoResponse(this, info));
00189 }
00190 
00191 } // End of namespace Dropbox
00192 } // End of namespace Cloud


Generated on Sat Nov 30 2019 05:00:22 for ResidualVM by doxygen 1.7.1
curved edge   curved edge