diff options
author | Alexander Tkachev | 2016-07-22 20:54:02 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | d57e0c89b5b20dac247cb2f43450014d84719ba6 (patch) | |
tree | bd4378856a83d3fe914cf734b87b2dbbbe75c838 | |
parent | f3959e140106334b33cf74832fc5b20d27407d0a (diff) | |
download | scummvm-rg350-d57e0c89b5b20dac247cb2f43450014d84719ba6.tar.gz scummvm-rg350-d57e0c89b5b20dac247cb2f43450014d84719ba6.tar.bz2 scummvm-rg350-d57e0c89b5b20dac247cb2f43450014d84719ba6.zip |
CLOUD: #define all OAuth2/API-related URLs
15 files changed, 160 insertions, 67 deletions
diff --git a/backends/cloud/box/boxlistdirectorybyidrequest.cpp b/backends/cloud/box/boxlistdirectorybyidrequest.cpp index 0171b52a0f..c013f1eb2a 100644 --- a/backends/cloud/box/boxlistdirectorybyidrequest.cpp +++ b/backends/cloud/box/boxlistdirectorybyidrequest.cpp @@ -34,6 +34,7 @@ namespace Cloud { namespace Box { #define BOX_LIST_DIRECTORY_LIMIT 1000 +#define BOX_FOLDERS_API_LINK "https://api.box.com/2.0/folders/%s/items?offset=%u&limit=%u&fields=%s" BoxListDirectoryByIdRequest::BoxListDirectoryByIdRequest(BoxStorage *storage, Common::String id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _requestedId(id), _storage(storage), _listDirectoryCallback(cb), @@ -58,7 +59,7 @@ void BoxListDirectoryByIdRequest::start() { void BoxListDirectoryByIdRequest::makeRequest(uint32 offset) { Common::String url = Common::String::format( - "https://api.box.com/2.0/folders/%s/items?offset=%u&limit=%u&fields=%s", + BOX_FOLDERS_API_LINK, _requestedId.c_str(), offset, BOX_LIST_DIRECTORY_LIMIT, diff --git a/backends/cloud/box/boxstorage.cpp b/backends/cloud/box/boxstorage.cpp index 2b2be70e55..401ec6a115 100644 --- a/backends/cloud/box/boxstorage.cpp +++ b/backends/cloud/box/boxstorage.cpp @@ -37,6 +37,11 @@ namespace Cloud { namespace Box { +#define BOX_OAUTH2_TOKEN "https://api.box.com/oauth2/token" +#define BOX_API_FOLDERS "https://api.box.com/2.0/folders" +#define BOX_API_FILES_CONTENT "https://api.box.com/2.0/files/%s/content" +#define BOX_API_USERS_ME "https://api.box.com/2.0/users/me" + char *BoxStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth char *BoxStorage::SECRET = nullptr; //TODO: hide these secrets somehow @@ -80,7 +85,7 @@ void BoxStorage::getAccessToken(BoolCallback callback, Networking::ErrorCallback if (errorCallback == nullptr) errorCallback = getErrorPrintingCallback(); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://api.box.com/oauth2/token"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, BOX_OAUTH2_TOKEN); if (codeFlow) { request->addPostField("grant_type=authorization_code"); request->addPostField("code=" + code); @@ -223,7 +228,7 @@ Networking::Request *BoxStorage::createDirectoryWithParentId(Common::String pare if (!errorCallback) errorCallback = getErrorPrintingCallback(); - Common::String url = "https://api.box.com/2.0/folders"; + Common::String url = BOX_API_FOLDERS; Networking::JsonCallback innerCallback = new Common::CallbackBridge<BoxStorage, BoolResponse, Networking::JsonResponse>(this, &BoxStorage::createDirectoryInnerCallback, callback); Networking::CurlJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, url.c_str()); request->addHeader("Authorization: Bearer " + accessToken()); @@ -263,7 +268,7 @@ bool BoxStorage::uploadStreamSupported() { Networking::Request *BoxStorage::streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) { if (callback) { - Common::String url = "https://api.box.com/2.0/files/" + id + "/content"; + Common::String url = Common::String::format(BOX_API_FILES_CONTENT, id.c_str()); Common::String header = "Authorization: Bearer " + _token; curl_slist *headersList = curl_slist_append(nullptr, header.c_str()); Networking::NetworkReadStream *stream = new Networking::NetworkReadStream(url.c_str(), headersList, ""); @@ -276,7 +281,7 @@ Networking::Request *BoxStorage::streamFileById(Common::String id, Networking::N Networking::Request *BoxStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) { Networking::JsonCallback innerCallback = new Common::CallbackBridge<BoxStorage, StorageInfoResponse, Networking::JsonResponse>(this, &BoxStorage::infoInnerCallback, callback); - Networking::CurlJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, "https://api.box.com/2.0/users/me"); + Networking::CurlJsonRequest *request = new BoxTokenRefresher(this, innerCallback, errorCallback, BOX_API_USERS_ME); request->addHeader("Authorization: Bearer " + _token); return addRequest(request); } diff --git a/backends/cloud/box/boxuploadrequest.cpp b/backends/cloud/box/boxuploadrequest.cpp index c308ddb429..29034c479f 100644 --- a/backends/cloud/box/boxuploadrequest.cpp +++ b/backends/cloud/box/boxuploadrequest.cpp @@ -33,6 +33,8 @@ namespace Cloud { namespace Box { +#define BOX_API_FILES "https://upload.box.com/api/2.0/files" + BoxUploadRequest::BoxUploadRequest(BoxStorage *storage, Common::String path, Common::String localPath, Storage::UploadCallback callback, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _localPath(localPath), _uploadCallback(callback), _workingRequest(nullptr), _ignoreCallback(false) { @@ -102,7 +104,7 @@ void BoxUploadRequest::upload() { } } - Common::String url = "https://upload.box.com/api/2.0/files"; + Common::String url = BOX_API_FILES; if (_resolvedId != "") url += "/" + _resolvedId; url += "/content"; diff --git a/backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp b/backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp index 968fb4550a..ad9b0fcc0d 100644 --- a/backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxcreatedirectoryrequest.cpp @@ -30,6 +30,8 @@ namespace Cloud { namespace Dropbox { +#define DROPBOX_API_CREATE_FOLDER "https://api.dropboxapi.com/2/files/create_folder" + DropboxCreateDirectoryRequest::DropboxCreateDirectoryRequest(Common::String token, Common::String path, Storage::BoolCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _token(token), _path(path), _boolCallback(cb), _workingRequest(nullptr), _ignoreCallback(false) { @@ -51,7 +53,7 @@ void DropboxCreateDirectoryRequest::start() { Networking::JsonCallback innerCallback = new Common::Callback<DropboxCreateDirectoryRequest, Networking::JsonResponse>(this, &DropboxCreateDirectoryRequest::responseCallback); Networking::ErrorCallback errorCallback = new Common::Callback<DropboxCreateDirectoryRequest, Networking::ErrorResponse>(this, &DropboxCreateDirectoryRequest::errorCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://api.dropboxapi.com/2/files/create_folder"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, DROPBOX_API_CREATE_FOLDER); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); diff --git a/backends/cloud/dropbox/dropboxinforequest.cpp b/backends/cloud/dropbox/dropboxinforequest.cpp index 37700eaf55..207c20271a 100644 --- a/backends/cloud/dropbox/dropboxinforequest.cpp +++ b/backends/cloud/dropbox/dropboxinforequest.cpp @@ -31,6 +31,9 @@ namespace Cloud { namespace Dropbox { +#define DROPBOX_API_GET_CURRENT_ACCOUNT "https://api.dropboxapi.com/2/users/get_current_account" +#define DROPBOX_API_GET_SPACE_USAGE "https://api.dropboxapi.com/2/users/get_space_usage" + DropboxInfoRequest::DropboxInfoRequest(Common::String token, Storage::StorageInfoCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _token(token), _infoCallback(cb), _workingRequest(nullptr), _ignoreCallback(false) { @@ -52,7 +55,7 @@ void DropboxInfoRequest::start() { Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, Networking::JsonResponse>(this, &DropboxInfoRequest::userResponseCallback); Networking::ErrorCallback errorCallback = new Common::Callback<DropboxInfoRequest, Networking::ErrorResponse>(this, &DropboxInfoRequest::errorCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://api.dropboxapi.com/2/users/get_current_account"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, DROPBOX_API_GET_CURRENT_ACCOUNT); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); request->addPostField("null"); //use POST @@ -90,7 +93,7 @@ void DropboxInfoRequest::userResponseCallback(Networking::JsonResponse response) Networking::JsonCallback innerCallback = new Common::Callback<DropboxInfoRequest, Networking::JsonResponse>(this, &DropboxInfoRequest::quotaResponseCallback); Networking::ErrorCallback errorCallback = new Common::Callback<DropboxInfoRequest, Networking::ErrorResponse>(this, &DropboxInfoRequest::errorCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://api.dropboxapi.com/2/users/get_space_usage"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, DROPBOX_API_GET_SPACE_USAGE); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); request->addPostField("null"); //use POST diff --git a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp index 2e1d61812a..8b00f7c2bf 100644 --- a/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp +++ b/backends/cloud/dropbox/dropboxlistdirectoryrequest.cpp @@ -31,6 +31,9 @@ namespace Cloud { namespace Dropbox { +#define DROPBOX_API_LIST_FOLDER "https://api.dropboxapi.com/2/files/list_folder" +#define DROPBOX_API_LIST_FOLDER_CONTINUE "https://api.dropboxapi.com/2/files/list_folder/continue" + DropboxListDirectoryRequest::DropboxListDirectoryRequest(Common::String token, Common::String path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive): Networking::Request(nullptr, ecb), _requestedPath(path), _requestedRecursive(recursive), _listDirectoryCallback(cb), _token(token), _workingRequest(nullptr), _ignoreCallback(false) { @@ -53,7 +56,7 @@ void DropboxListDirectoryRequest::start() { Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(this, &DropboxListDirectoryRequest::responseCallback); Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::ErrorResponse>(this, &DropboxListDirectoryRequest::errorCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, "https://api.dropboxapi.com/2/files/list_folder"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, DROPBOX_API_LIST_FOLDER); request->addHeader("Authorization: Bearer " + _token); request->addHeader("Content-Type: application/json"); @@ -71,6 +74,7 @@ void DropboxListDirectoryRequest::start() { void DropboxListDirectoryRequest::responseCallback(Networking::JsonResponse response) { _workingRequest = nullptr; + if (_ignoreCallback) { delete response.value; return; @@ -85,58 +89,109 @@ void DropboxListDirectoryRequest::responseCallback(Networking::JsonResponse resp error.httpResponseCode = rq->getNetworkReadStream()->httpResponseCode(); Common::JSONValue *json = response.value; - if (json) { - Common::JSONObject responseObjecct = json->asObject(); + if (json == nullptr) { + error.response = "Failed to parse JSON, null passed!"; + finishError(error); + return; + } + + if (!json->isObject()) { + error.response = "Passed JSON is not an object!"; + finishError(error); + delete json; + return; + } + + Common::JSONObject responseObject = json->asObject(); + + if (responseObject.contains("error") || responseObject.contains("error_summary")) { + if (responseObject.contains("error_summary") && responseObject.getVal("error_summary")->isString()) { + warning("Dropbox returned error: %s", responseObject.getVal("error_summary")->asString().c_str()); + } + error.failed = true; + error.response = json->stringify(); + finishError(error); + delete json; + return; + } - if (responseObjecct.contains("error") || responseObjecct.contains("error_summary")) { - warning("Dropbox returned error: %s", responseObjecct.getVal("error_summary")->asString().c_str()); - error.failed = true; - error.response = json->stringify(); + //check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults + if (responseObject.contains("entries")) { + if (!responseObject.getVal("entries")->isArray()) { + error.response = Common::String::format( + "\"entries\" found, but that's not an array!\n%s", + responseObject.getVal("entries")->stringify(true).c_str() + ); finishError(error); delete json; return; } - //TODO: check that ALL keys exist AND HAVE RIGHT TYPE to avoid segfaults - - if (responseObjecct.contains("entries")) { - Common::JSONArray items = responseObjecct.getVal("entries")->asArray(); - for (uint32 i = 0; i < items.size(); ++i) { - Common::JSONObject item = items[i]->asObject(); - Common::String path = item.getVal("path_lower")->asString(); - bool isDirectory = (item.getVal(".tag")->asString() == "folder"); - uint32 size = 0, timestamp = 0; - if (!isDirectory) { - size = item.getVal("size")->asIntegerNumber(); - timestamp = ISO8601::convertToTimestamp(item.getVal("server_modified")->asString()); - } - _files.push_back(StorageFile(path, size, timestamp, isDirectory)); + Common::JSONArray items = responseObject.getVal("entries")->asArray(); + for (uint32 i = 0; i < items.size(); ++i) { + if (!Networking::CurlJsonRequest::jsonIsObject(items[i], "DropboxListDirectoryRequest")) + continue; + + Common::JSONObject item = items[i]->asObject(); + + if (!Networking::CurlJsonRequest::jsonContainsString(item, "path_lower", "DropboxListDirectoryRequest")) + continue; + if (!Networking::CurlJsonRequest::jsonContainsString(item, ".tag", "DropboxListDirectoryRequest")) + continue; + + Common::String path = item.getVal("path_lower")->asString(); + bool isDirectory = (item.getVal(".tag")->asString() == "folder"); + uint32 size = 0, timestamp = 0; + if (!isDirectory) { + if (!Networking::CurlJsonRequest::jsonContainsString(item, "server_modified", "DropboxListDirectoryRequest")) + continue; + if (!Networking::CurlJsonRequest::jsonContainsIntegerNumber(item, "size", "DropboxListDirectoryRequest")) + continue; + + size = item.getVal("size")->asIntegerNumber(); + timestamp = ISO8601::convertToTimestamp(item.getVal("server_modified")->asString()); } + _files.push_back(StorageFile(path, size, timestamp, isDirectory)); } + } - bool hasMore = (responseObjecct.contains("has_more") && responseObjecct.getVal("has_more")->asBool()); + bool hasMore = false; + if (responseObject.contains("has_more")) { + if (!responseObject.getVal("has_more")->isBool()) { + warning("DropboxListDirectoryRequest: \"has_more\" is not a boolean"); + debug(9, "%s", responseObject.getVal("has_more")->stringify(true).c_str()); + error.response = "\"has_more\" is not a boolean!"; + finishError(error); + delete json; + return; + } - if (hasMore) { - Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(this, &DropboxListDirectoryRequest::responseCallback); - Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::ErrorResponse>(this, &DropboxListDirectoryRequest::errorCallback); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, "https://api.dropboxapi.com/2/files/list_folder/continue"); - request->addHeader("Authorization: Bearer " + _token); - request->addHeader("Content-Type: application/json"); + hasMore = responseObject.getVal("has_more")->asBool(); + } - Common::JSONObject jsonRequestParameters; - jsonRequestParameters.setVal("cursor", new Common::JSONValue(responseObjecct.getVal("cursor")->asString())); + if (hasMore) { + if (!Networking::CurlJsonRequest::jsonContainsString(responseObject, "cursor", "DropboxListDirectoryRequest")) { + error.response = "\"has_more\" found, but \"cursor\" is not (or it's not a string)!"; + finishError(error); + delete json; + return; + } - Common::JSONValue value(jsonRequestParameters); - request->addPostField(Common::JSON::stringify(&value)); + Networking::JsonCallback callback = new Common::Callback<DropboxListDirectoryRequest, Networking::JsonResponse>(this, &DropboxListDirectoryRequest::responseCallback); + Networking::ErrorCallback failureCallback = new Common::Callback<DropboxListDirectoryRequest, Networking::ErrorResponse>(this, &DropboxListDirectoryRequest::errorCallback); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, failureCallback, DROPBOX_API_LIST_FOLDER_CONTINUE); + request->addHeader("Authorization: Bearer " + _token); + request->addHeader("Content-Type: application/json"); - _workingRequest = ConnMan.addRequest(request); - } else { - finishListing(_files); - } + Common::JSONObject jsonRequestParameters; + jsonRequestParameters.setVal("cursor", new Common::JSONValue(responseObject.getVal("cursor")->asString())); + + Common::JSONValue value(jsonRequestParameters); + request->addPostField(Common::JSON::stringify(&value)); + + _workingRequest = ConnMan.addRequest(request); } else { - warning("null, not json"); - error.failed = true; - finishError(error); + finishListing(_files); } delete json; diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index cd1dff8afb..20f8a68327 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -37,6 +37,9 @@ namespace Cloud { namespace Dropbox { +#define DROPBOX_OAUTH2_TOKEN "https://api.dropboxapi.com/oauth2/token" +#define DROPBOX_API_FILES_DOWNLOAD "https://content.dropboxapi.com/2/files/download" + char *DropboxStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth char *DropboxStorage::SECRET = nullptr; //TODO: hide these secrets somehow @@ -65,7 +68,7 @@ void DropboxStorage::getAccessToken(Common::String code) { loadKeyAndSecret(); Networking::JsonCallback callback = new Common::Callback<DropboxStorage, Networking::JsonResponse>(this, &DropboxStorage::codeFlowComplete); Networking::ErrorCallback errorCallback = new Common::Callback<DropboxStorage, Networking::ErrorResponse>(this, &DropboxStorage::codeFlowFailed); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, "https://api.dropboxapi.com/oauth2/token"); + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, DROPBOX_OAUTH2_TOKEN); request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); request->addPostField("client_id=" + Common::String(KEY)); @@ -129,7 +132,7 @@ Networking::Request *DropboxStorage::streamFileById(Common::String path, Network jsonRequestParameters.setVal("path", new Common::JSONValue(path)); Common::JSONValue value(jsonRequestParameters); - Networking::CurlRequest *request = new Networking::CurlRequest(nullptr, nullptr, "https://content.dropboxapi.com/2/files/download"); //TODO: is it right? + Networking::CurlRequest *request = new Networking::CurlRequest(nullptr, nullptr, DROPBOX_API_FILES_DOWNLOAD); //TODO: is it OK to pass no callbacks? request->addHeader("Authorization: Bearer " + _token); request->addHeader("Dropbox-API-Arg: " + Common::JSON::stringify(&value)); request->addHeader("Content-Type: "); //required to be empty (as we do POST, it's usually app/form-url-encoded) diff --git a/backends/cloud/dropbox/dropboxuploadrequest.cpp b/backends/cloud/dropbox/dropboxuploadrequest.cpp index 03c3fbc244..f129eaa94d 100644 --- a/backends/cloud/dropbox/dropboxuploadrequest.cpp +++ b/backends/cloud/dropbox/dropboxuploadrequest.cpp @@ -32,6 +32,9 @@ namespace Cloud { namespace Dropbox { +#define DROPBOX_API_FILES_UPLOAD "https://content.dropboxapi.com/2/files/upload" +#define DROPBOX_API_FILES_UPLOAD_SESSION "https://content.dropboxapi.com/2/files/upload_session/" + DropboxUploadRequest::DropboxUploadRequest(Common::String token, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _token(token), _savePath(path), _contentsStream(contents), _uploadCallback(callback), _workingRequest(nullptr), _ignoreCallback(false) { @@ -68,12 +71,12 @@ void DropboxUploadRequest::start() { void DropboxUploadRequest::uploadNextPart() { const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024; - Common::String url = "https://content.dropboxapi.com/2/files/upload_session/"; + Common::String url = DROPBOX_API_FILES_UPLOAD_SESSION; Common::JSONObject jsonRequestParameters; if (_contentsStream->pos() == 0 || _sessionId == "") { if ((uint32)_contentsStream->size() <= UPLOAD_PER_ONE_REQUEST) { - url = "https://content.dropboxapi.com/2/files/upload"; + url = DROPBOX_API_FILES_UPLOAD; jsonRequestParameters.setVal("path", new Common::JSONValue(_savePath)); jsonRequestParameters.setVal("mode", new Common::JSONValue("overwrite")); jsonRequestParameters.setVal("autorename", new Common::JSONValue(false)); diff --git a/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp index 5597d78704..52611126a0 100644 --- a/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp +++ b/backends/cloud/googledrive/googledrivelistdirectorybyidrequest.cpp @@ -33,6 +33,9 @@ namespace Cloud { namespace GoogleDrive { +#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files?spaces=drive&fields=files%28id,mimeType,modifiedTime,name,size%29,nextPageToken&orderBy=folder,name" +//files(id,mimeType,modifiedTime,name,size),nextPageToken + GoogleDriveListDirectoryByIdRequest::GoogleDriveListDirectoryByIdRequest(GoogleDriveStorage *storage, Common::String id, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _requestedId(id), _storage(storage), _listDirectoryCallback(cb), _workingRequest(nullptr), _ignoreCallback(false) { @@ -57,8 +60,7 @@ void GoogleDriveListDirectoryByIdRequest::start() { } void GoogleDriveListDirectoryByIdRequest::makeRequest(Common::String pageToken) { - Common::String url = "https://www.googleapis.com/drive/v3/files?spaces=drive&fields=files%28id,mimeType,modifiedTime,name,size%29,nextPageToken&orderBy=folder,name"; - //files(id,mimeType,modifiedTime,name,size),nextPageToken + Common::String url = GOOGLEDRIVE_API_FILES; if (pageToken != "") url += "&pageToken=" + pageToken; url += "&q=%27" + _requestedId + "%27+in+parents"; diff --git a/backends/cloud/googledrive/googledrivestorage.cpp b/backends/cloud/googledrive/googledrivestorage.cpp index 22013210d3..af19019a1e 100644 --- a/backends/cloud/googledrive/googledrivestorage.cpp +++ b/backends/cloud/googledrive/googledrivestorage.cpp @@ -37,6 +37,11 @@ namespace Cloud { namespace GoogleDrive { +#define GOOGLEDRIVE_OAUTH2_TOKEN "https://accounts.google.com/o/oauth2/token" +#define GOOGLEDRIVE_API_FILES_ALT_MEDIA "https://www.googleapis.com/drive/v3/files/%s?alt=media" +#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/drive/v3/files" +#define GOOGLEDRIVE_API_ABOUT "https://www.googleapis.com/drive/v3/about?fields=storageQuota,user" + char *GoogleDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth char *GoogleDriveStorage::SECRET = nullptr; //TODO: hide these secrets somehow @@ -79,7 +84,7 @@ void GoogleDriveStorage::getAccessToken(BoolCallback callback, Networking::Error Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::tokenRefreshed, callback); if (errorCallback == nullptr) errorCallback = getErrorPrintingCallback(); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://accounts.google.com/o/oauth2/token"); //TODO + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, GOOGLEDRIVE_OAUTH2_TOKEN); //TODO if (codeFlow) { request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); @@ -224,7 +229,7 @@ Networking::Request *GoogleDriveStorage::upload(Common::String path, Common::See Networking::Request *GoogleDriveStorage::streamFileById(Common::String id, Networking::NetworkReadStreamCallback callback, Networking::ErrorCallback errorCallback) { if (callback) { - Common::String url = "https://www.googleapis.com/drive/v3/files/" + ConnMan.urlEncode(id) + "?alt=media"; + Common::String url = Common::String::format(GOOGLEDRIVE_API_FILES_ALT_MEDIA, ConnMan.urlEncode(id).c_str()); Common::String header = "Authorization: Bearer " + _token; curl_slist *headersList = curl_slist_append(nullptr, header.c_str()); Networking::NetworkReadStream *stream = new Networking::NetworkReadStream(url.c_str(), headersList, ""); @@ -246,7 +251,7 @@ Networking::Request *GoogleDriveStorage::createDirectoryWithParentId(Common::Str if (!errorCallback) errorCallback = getErrorPrintingCallback(); - Common::String url = "https://www.googleapis.com/drive/v3/files"; + Common::String url = GOOGLEDRIVE_API_FILES; Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, BoolResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::createDirectoryInnerCallback, callback); Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, url.c_str()); request->addHeader("Authorization: Bearer " + accessToken()); @@ -270,7 +275,7 @@ Networking::Request *GoogleDriveStorage::info(StorageInfoCallback callback, Netw if (!callback) callback = new Common::Callback<GoogleDriveStorage, StorageInfoResponse>(this, &GoogleDriveStorage::printInfo); Networking::JsonCallback innerCallback = new Common::CallbackBridge<GoogleDriveStorage, StorageInfoResponse, Networking::JsonResponse>(this, &GoogleDriveStorage::infoInnerCallback, callback); - Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, "https://www.googleapis.com/drive/v3/about?fields=storageQuota,user"); + Networking::CurlJsonRequest *request = new GoogleDriveTokenRefresher(this, innerCallback, errorCallback, GOOGLEDRIVE_API_ABOUT); request->addHeader("Authorization: Bearer " + _token); return addRequest(request); } diff --git a/backends/cloud/googledrive/googledriveuploadrequest.cpp b/backends/cloud/googledrive/googledriveuploadrequest.cpp index 3cdee26dbe..bd9d8790ed 100644 --- a/backends/cloud/googledrive/googledriveuploadrequest.cpp +++ b/backends/cloud/googledrive/googledriveuploadrequest.cpp @@ -33,6 +33,8 @@ namespace Cloud { namespace GoogleDrive { +#define GOOGLEDRIVE_API_FILES "https://www.googleapis.com/upload/drive/v3/files" + GoogleDriveUploadRequest::GoogleDriveUploadRequest(GoogleDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _contentsStream(contents), _uploadCallback(callback), _workingRequest(nullptr), _ignoreCallback(false) { @@ -111,7 +113,7 @@ void GoogleDriveUploadRequest::startUpload() { } } - Common::String url = "https://www.googleapis.com/upload/drive/v3/files"; + Common::String url = GOOGLEDRIVE_API_FILES; if (_resolvedId != "") url += "/" + ConnMan.urlEncode(_resolvedId); url += "?uploadType=resumable&fields=id,mimeType,modifiedTime,name,size"; diff --git a/backends/cloud/onedrive/onedrivecreatedirectoryrequest.cpp b/backends/cloud/onedrive/onedrivecreatedirectoryrequest.cpp index bd612d64a8..a31fd025ac 100644 --- a/backends/cloud/onedrive/onedrivecreatedirectoryrequest.cpp +++ b/backends/cloud/onedrive/onedrivecreatedirectoryrequest.cpp @@ -31,6 +31,8 @@ namespace Cloud { namespace OneDrive { +#define ONEDRIVE_API_SPECIAL_APPROOT "https://api.onedrive.com/v1.0/drive/special/approot" + OneDriveCreateDirectoryRequest::OneDriveCreateDirectoryRequest(OneDriveStorage *storage, Common::String path, Storage::BoolCallback cb, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _storage(storage), _path(path), _boolCallback(cb), _workingRequest(nullptr), _ignoreCallback(false) { @@ -65,7 +67,7 @@ void OneDriveCreateDirectoryRequest::start() { } } - Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot"; + Common::String url = ONEDRIVE_API_SPECIAL_APPROOT; if (parent != "") url += ":/" + ConnMan.urlEncode(parent) + ":"; url += "/children"; diff --git a/backends/cloud/onedrive/onedrivelistdirectoryrequest.cpp b/backends/cloud/onedrive/onedrivelistdirectoryrequest.cpp index 069d6fdfb1..a247a9f234 100644 --- a/backends/cloud/onedrive/onedrivelistdirectoryrequest.cpp +++ b/backends/cloud/onedrive/onedrivelistdirectoryrequest.cpp @@ -31,6 +31,8 @@ namespace Cloud { namespace OneDrive { +#define ONEDRIVE_API_SPECIAL_APPROOT_CHILDREN "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/children" + OneDriveListDirectoryRequest::OneDriveListDirectoryRequest(OneDriveStorage *storage, Common::String path, Storage::ListDirectoryCallback cb, Networking::ErrorCallback ecb, bool recursive): Networking::Request(nullptr, ecb), _requestedPath(path), _requestedRecursive(recursive), _storage(storage), _listDirectoryCallback(cb), @@ -74,8 +76,7 @@ void OneDriveListDirectoryRequest::listNextDirectory() { Common::String dir = _currentDirectory; dir.deleteLastChar(); - Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + ConnMan.urlEncode(dir); - url += ":/children"; + Common::String url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_CHILDREN, ConnMan.urlEncode(dir).c_str()); makeRequest(url); } diff --git a/backends/cloud/onedrive/onedrivestorage.cpp b/backends/cloud/onedrive/onedrivestorage.cpp index 5612cbfd16..af5d9f10ba 100644 --- a/backends/cloud/onedrive/onedrivestorage.cpp +++ b/backends/cloud/onedrive/onedrivestorage.cpp @@ -38,6 +38,10 @@ namespace Cloud { namespace OneDrive { +#define ONEDRIVE_OAUTH2_TOKEN "https://login.live.com/oauth20_token.srf" +#define ONEDRIVE_API_SPECIAL_APPROOT_ID "https://api.onedrive.com/v1.0/drive/special/approot:/" +#define ONEDRIVE_API_SPECIAL_APPROOT "https://api.onedrive.com/v1.0/drive/special/approot" + char *OneDriveStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth char *OneDriveStorage::SECRET = nullptr; //TODO: hide these secrets somehow @@ -81,7 +85,7 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Networking::ErrorCal Networking::JsonCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, BoolResponse, Networking::JsonResponse>(this, &OneDriveStorage::tokenRefreshed, callback); if (errorCallback == nullptr) errorCallback = getErrorPrintingCallback(); - Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, "https://login.live.com/oauth20_token.srf"); //TODO + Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(innerCallback, errorCallback, ONEDRIVE_OAUTH2_TOKEN); if (codeFlow) { request->addPostField("code=" + code); request->addPostField("grant_type=authorization_code"); @@ -227,7 +231,7 @@ Networking::Request *OneDriveStorage::upload(Common::String path, Common::Seekab } Networking::Request *OneDriveStorage::streamFileById(Common::String path, Networking::NetworkReadStreamCallback outerCallback, Networking::ErrorCallback errorCallback) { - Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + ConnMan.urlEncode(path); + Common::String url = ONEDRIVE_API_SPECIAL_APPROOT_ID + ConnMan.urlEncode(path); Networking::JsonCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, Networking::NetworkReadStreamResponse, Networking::JsonResponse>(this, &OneDriveStorage::fileInfoCallback, outerCallback); Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(this, innerCallback, errorCallback, url.c_str()); request->addHeader("Authorization: Bearer " + _token); @@ -242,7 +246,7 @@ Networking::Request *OneDriveStorage::createDirectory(Common::String path, BoolC Networking::Request *OneDriveStorage::info(StorageInfoCallback callback, Networking::ErrorCallback errorCallback) { Networking::JsonCallback innerCallback = new Common::CallbackBridge<OneDriveStorage, StorageInfoResponse, Networking::JsonResponse>(this, &OneDriveStorage::infoInnerCallback, callback); - Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(this, innerCallback, errorCallback, "https://api.onedrive.com/v1.0/drive/special/approot"); + Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(this, innerCallback, errorCallback, ONEDRIVE_API_SPECIAL_APPROOT); request->addHeader("Authorization: bearer " + _token); return addRequest(request); } diff --git a/backends/cloud/onedrive/onedriveuploadrequest.cpp b/backends/cloud/onedrive/onedriveuploadrequest.cpp index 331800a381..ef1507d1f2 100644 --- a/backends/cloud/onedrive/onedriveuploadrequest.cpp +++ b/backends/cloud/onedrive/onedriveuploadrequest.cpp @@ -33,6 +33,9 @@ namespace Cloud { namespace OneDrive { +#define ONEDRIVE_API_SPECIAL_APPROOT_UPLOAD "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/upload.createSession" +#define ONEDRIVE_API_SPECIAL_APPROOT_CONTENT "https://api.onedrive.com/v1.0/drive/special/approot:/%s:/content" + OneDriveUploadRequest::OneDriveUploadRequest(OneDriveStorage *storage, Common::String path, Common::SeekableReadStream *contents, Storage::UploadCallback callback, Networking::ErrorCallback ecb): Networking::Request(nullptr, ecb), _storage(storage), _savePath(path), _contentsStream(contents), _uploadCallback(callback), _workingRequest(nullptr), _ignoreCallback(false) { @@ -70,7 +73,7 @@ void OneDriveUploadRequest::uploadNextPart() { const uint32 UPLOAD_PER_ONE_REQUEST = 10 * 1024 * 1024; if (_uploadUrl == "" && (uint32)_contentsStream->size() > UPLOAD_PER_ONE_REQUEST) { - Common::String url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + ConnMan.urlEncode(_savePath) + ":/upload.createSession"; //folder must exist + Common::String url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_UPLOAD, ConnMan.urlEncode(_savePath).c_str()); //folder must exist Networking::JsonCallback callback = new Common::Callback<OneDriveUploadRequest, Networking::JsonResponse>(this, &OneDriveUploadRequest::partUploadedCallback); Networking::ErrorCallback failureCallback = new Common::Callback<OneDriveUploadRequest, Networking::ErrorResponse>(this, &OneDriveUploadRequest::partUploadedErrorCallback); Networking::CurlJsonRequest *request = new OneDriveTokenRefresher(_storage, callback, failureCallback, url.c_str()); @@ -82,7 +85,7 @@ void OneDriveUploadRequest::uploadNextPart() { Common::String url; if (_uploadUrl == "") { - url = "https://api.onedrive.com/v1.0/drive/special/approot:/" + ConnMan.urlEncode(_savePath) + ":/content"; + url = Common::String::format(ONEDRIVE_API_SPECIAL_APPROOT_CONTENT, ConnMan.urlEncode(_savePath).c_str()); } else { url = _uploadUrl; } |