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

boxlistdirectorybyidrequest.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/box/boxlistdirectorybyidrequest.h"
00024 #include "backends/cloud/box/boxstorage.h"
00025 #include "backends/cloud/box/boxtokenrefresher.h"
00026 #include "backends/cloud/iso8601.h"
00027 #include "backends/cloud/storage.h"
00028 #include "backends/networking/curl/connectionmanager.h"
00029 #include "backends/networking/curl/curljsonrequest.h"
00030 #include "backends/networking/curl/networkreadstream.h"
00031 #include "common/json.h"
00032 
00033 namespace Cloud {
00034 namespace Box {
00035 
00036 #define BOX_LIST_DIRECTORY_LIMIT 1000
00037 #define BOX_FOLDERS_API_LINK "https://api.box.com/2.0/folders/%s/items?offset=%u&limit=%u&fields=%s"
00038 
00039 BoxListDirectoryByIdRequest::BoxListDirectoryByIdRequest(BoxStorage *storage, Common::String id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb):
00040     Networking::Request(nullptr, ecb), _requestedId(id), _storage(storage), _listDirectoryCallback(cb),
00041     _workingRequest(nullptr), _ignoreCallback(false) {
00042     start();
00043 }
00044 
00045 BoxListDirectoryByIdRequest::~BoxListDirectoryByIdRequest() {
00046     _ignoreCallback = true;
00047     if (_workingRequest) _workingRequest->finish();
00048     delete _listDirectoryCallback;
00049 }
00050 
00051 void BoxListDirectoryByIdRequest::start() {
00052     _ignoreCallback = true;
00053     if (_workingRequest) _workingRequest->finish();
00054     _files.clear();
00055     _ignoreCallback = false;
00056 
00057     makeRequest(0);
00058 }
00059 
00060 void BoxListDirectoryByIdRequest::makeRequest(uint32 offset) {
00061     Common::String url = Common::String::format(
00062         BOX_FOLDERS_API_LINK,
00063         _requestedId.c_str(),
00064         offset,
00065         BOX_LIST_DIRECTORY_LIMIT,
00066         "id,type,name,size,modified_at"
00067     );
00068 
00069     Networking::JsonCallback callback = new Common::Callback<BoxListDirectoryByIdRequest, Networking::JsonResponse>(this, &BoxListDirectoryByIdRequest::responseCallback);
00070     Networking::ErrorCallback failureCallback = new Common::Callback<BoxListDirectoryByIdRequest, Networking::ErrorResponse>(this, &BoxListDirectoryByIdRequest::errorCallback);
00071     Networking::CurlJsonRequest *request = new BoxTokenRefresher(_storage, callback, failureCallback, url.c_str());
00072     request->addHeader("Authorization: Bearer " + _storage->accessToken());
00073     _workingRequest = ConnMan.addRequest(request);
00074 }
00075 
00076 void BoxListDirectoryByIdRequest::responseCallback(Networking::JsonResponse response) {
00077     _workingRequest = nullptr;
00078     if (_ignoreCallback) {
00079         delete response.value;
00080         return;
00081     }
00082 
00083     if (response.request)
00084         _date = response.request->date();
00085 
00086     Networking::ErrorResponse error(this);
00087     Networking::CurlJsonRequest *rq = (Networking::CurlJsonRequest *)response.request;
00088     if (rq && rq->getNetworkReadStream())
00089         error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode();
00090 
00091     Common::JSONValue *json = response.value;
00092     if (json == nullptr) {
00093         error.response = "Failed to parse JSON, null passed!";
00094         finishError(error);
00095         return;
00096     }
00097 
00098     if (!json->isObject()) {
00099         error.response = "Passed JSON is not an object!";
00100         finishError(error);
00101         delete json;
00102         return;
00103     }
00104 
00105     Common::JSONObject responseObject = json->asObject();
00106     //debug(9, "%s", json->stringify(true).c_str());
00107 
00108     //TODO: handle error messages passed as JSON
00109     /*
00110     if (responseObject.contains("error") || responseObject.contains("error_summary")) {
00111         warning("Box returned error: %s", responseObject.getVal("error_summary")->asString().c_str());
00112         error.failed = true;
00113         error.response = json->stringify();
00114         finishError(error);
00115         delete json;
00116         return;
00117     }
00118     */
00119 
00120     //check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults
00121     if (responseObject.contains("entries")) {
00122         if (!responseObject.getVal("entries")->isArray()) {
00123             error.response = Common::String::format(
00124                 "\"entries\" found, but that's not an array!\n%s",
00125                 responseObject.getVal("entries")->stringify(true).c_str()
00126             );
00127             finishError(error);
00128             delete json;
00129             return;
00130         }
00131 
00132         Common::JSONArray items = responseObject.getVal("entries")->asArray();
00133         for (uint32 i = 0; i < items.size(); ++i) {
00134             if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "BoxListDirectoryByIdRequest")) continue;
00135 
00136             Common::JSONObject item = items[i]->asObject();
00137 
00138             if (!Networking::CurlJsonRequest::jsonContainsString(item, "id", "BoxListDirectoryByIdRequest")) continue;
00139             if (!Networking::CurlJsonRequest::jsonContainsString(item, "name", "BoxListDirectoryByIdRequest")) continue;
00140             if (!Networking::CurlJsonRequest::jsonContainsString(item, "type", "BoxListDirectoryByIdRequest")) continue;
00141             if (!Networking::CurlJsonRequest::jsonContainsString(item, "modified_at", "BoxListDirectoryByIdRequest")) continue;
00142             if (!Networking::CurlJsonRequest::jsonContainsStringOrIntegerNumber(item, "size", "BoxListDirectoryByIdRequest")) continue;
00143 
00144             Common::String id = item.getVal("id")->asString();
00145             Common::String name = item.getVal("name")->asString();
00146             bool isDirectory = (item.getVal("type")->asString() == "folder");
00147             uint32 size;
00148             if (item.getVal("size")->isString()) {
00149                 size = item.getVal("size")->asString().asUint64();
00150             } else {
00151                 size = item.getVal("size")->asIntegerNumber();
00152             }
00153             uint32 timestamp = ISO8601::convertToTimestamp(item.getVal("modified_at")->asString());
00154 
00155             //as we list directory by id, we can't determine full path for the file, so we leave it empty
00156             _files.push_back(StorageFile(id, "", name, size, timestamp, isDirectory));
00157         }
00158     }
00159 
00160     uint32 received = 0;
00161     uint32 totalCount = 0;
00162     if (responseObject.contains("total_count") && responseObject.getVal("total_count")->isIntegerNumber())
00163         totalCount = responseObject.getVal("total_count")->asIntegerNumber();
00164     if (responseObject.contains("offset") && responseObject.getVal("offset")->isIntegerNumber())
00165         received = responseObject.getVal("offset")->asIntegerNumber();
00166     if (responseObject.contains("limit") && responseObject.getVal("limit")->isIntegerNumber())
00167         received += responseObject.getVal("limit")->asIntegerNumber();
00168     bool hasMore = (received < totalCount);
00169 
00170     if (hasMore) makeRequest(received);
00171     else finishListing(_files);
00172 
00173     delete json;
00174 }
00175 
00176 void BoxListDirectoryByIdRequest::errorCallback(Networking::ErrorResponse error) {
00177     _workingRequest = nullptr;
00178     if (_ignoreCallback) return;
00179     if (error.request) _date = error.request->date();
00180     finishError(error);
00181 }
00182 
00183 void BoxListDirectoryByIdRequest::handle() {}
00184 
00185 void BoxListDirectoryByIdRequest::restart() { start(); }
00186 
00187 Common::String BoxListDirectoryByIdRequest::date() const { return _date; }
00188 
00189 void BoxListDirectoryByIdRequest::finishListing(Common::Array<StorageFile> &files) {
00190     Request::finishSuccess();
00191     if (_listDirectoryCallback) (*_listDirectoryCallback)(Storage::ListDirectoryResponse(this, files));
00192 }
00193 
00194 } // End of namespace Box
00195 } // End of namespace Cloud


Generated on Sat Feb 16 2019 05:00:46 for ResidualVM by doxygen 1.7.1
curved edge   curved edge