Commit 95b87805 authored by Paweł Wegner's avatar Paweł Wegner

ICloudProvider: introduced ExchangeCodeRequest.

parent 082692bd
......@@ -47,6 +47,11 @@ IItem::Pointer MockProvider::rootDirectory() const {
return std::make_shared<MockItem>("root", IItem::FileType::Directory);
}
ICloudProvider::ExchangeCodeRequest::Pointer MockProvider::exchangeCodeAsync(
const std::string&, ExchangeCodeCallback) {
return nullptr;
}
ICloudProvider::ListDirectoryRequest::Pointer MockProvider::listDirectoryAsync(
IItem::Pointer directory, IListDirectoryCallback::Pointer callback) {
return util::make_unique<MockListDirectoryRequest>(directory,
......
......@@ -139,6 +139,8 @@ class MockProvider : public ICloudProvider {
std::string authorizeLibraryUrl() const override;
IItem::Pointer rootDirectory() const override;
ExchangeCodeRequest::Pointer exchangeCodeAsync(const std::string&,
ExchangeCodeCallback) override;
ListDirectoryRequest::Pointer listDirectoryAsync(
IItem::Pointer, IListDirectoryCallback::Pointer) override;
GetItemRequest::Pointer getItemAsync(const std::string& absolute_path,
......
......@@ -34,6 +34,7 @@
#include "Request/CreateDirectoryRequest.h"
#include "Request/DeleteItemRequest.h"
#include "Request/DownloadFileRequest.h"
#include "Request/ExchangeCodeRequest.h"
#include "Request/GetItemDataRequest.h"
#include "Request/GetItemRequest.h"
#include "Request/ListDirectoryRequest.h"
......@@ -248,6 +249,12 @@ IHttpServerFactory* CloudProvider::http_server() const {
return http_server_.get();
}
ExchangeCodeRequest::Pointer CloudProvider::exchangeCodeAsync(
const std::string& code, ExchangeCodeCallback callback) {
return util::make_unique<cloudstorage::ExchangeCodeRequest>(
shared_from_this(), code, callback);
}
ICloudProvider::ListDirectoryRequest::Pointer CloudProvider::listDirectoryAsync(
IItem::Pointer item, IListDirectoryCallback::Pointer callback) {
return util::make_unique<cloudstorage::ListDirectoryRequest>(
......
......@@ -60,6 +60,8 @@ class CloudProvider : public ICloudProvider,
virtual AuthorizeRequest::Pointer authorizeAsync();
ExchangeCodeRequest::Pointer exchangeCodeAsync(const std::string&,
ExchangeCodeCallback) override;
ListDirectoryRequest::Pointer listDirectoryAsync(
IItem::Pointer, IListDirectoryCallback::Pointer) override;
GetItemRequest::Pointer getItemAsync(const std::string& absolute_path,
......
......@@ -43,6 +43,7 @@ class ICloudProvider {
using Pointer = std::shared_ptr<ICloudProvider>;
using Hints = std::unordered_map<std::string, std::string>;
using ExchangeCodeRequest = IRequest<std::string>;
using ListDirectoryRequest = IRequest<std::vector<IItem::Pointer>>;
using GetItemRequest = IRequest<IItem::Pointer>;
using DownloadFileRequest = IRequest<void>;
......@@ -205,6 +206,16 @@ class ICloudProvider {
*/
virtual IItem::Pointer rootDirectory() const = 0;
/**
* Exchanges authorization code which was sent to redirect_uri() by cloud
* provider for a token (refresh_token).
*
* @return object representing the pending request
*/
virtual ExchangeCodeRequest::Pointer exchangeCodeAsync(
const std::string& code,
ExchangeCodeCallback = [](const std::string&) {}) = 0;
/**
* Lists directory.
*
......
......@@ -24,9 +24,9 @@
#ifndef IREQUEST_H
#define IREQUEST_H
#include <functional>
#include <memory>
#include <vector>
#include <functional>
#include "IItem.h"
......@@ -170,6 +170,7 @@ class IUploadFileCallback {
virtual void progress(uint32_t total, uint32_t now) = 0;
};
using ExchangeCodeCallback = std::function<void(const std::string&)>;
using GetItemCallback = std::function<void(IItem::Pointer)>;
using GetItemDataCallback = std::function<void(IItem::Pointer)>;
using DeleteItemCallback = std::function<void(bool)>;
......
......@@ -36,7 +36,8 @@ libcloudstorage_la_SOURCES = \
Request/DeleteItemRequest.cpp \
Request/CreateDirectoryRequest.cpp \
Request/MoveItemRequest.cpp \
Request/RenameItemRequest.cpp
Request/RenameItemRequest.cpp \
Request/ExchangeCodeRequest.cpp
noinst_HEADERS = \
IAuth.h \
......@@ -65,7 +66,8 @@ noinst_HEADERS = \
Request/DeleteItemRequest.h \
Request/CreateDirectoryRequest.h \
Request/MoveItemRequest.h \
Request/RenameItemRequest.h
Request/RenameItemRequest.h \
Request/ExchangeCodeRequest.h
libcloudstorage_la_HEADERS = \
IItem.h \
......
/*****************************************************************************
* ExchangeCodeRequest.cpp
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "ExchangeCodeRequest.h"
#include "CloudProvider/CloudProvider.h"
namespace cloudstorage {
ExchangeCodeRequest::ExchangeCodeRequest(std::shared_ptr<CloudProvider> p,
const std::string& authorization_code,
ExchangeCodeCallback callback)
: Request(p) {
set_resolver([this, authorization_code,
callback](Request<std::string>* r) -> std::string {
std::stringstream stream;
if (!provider()->auth()->exchangeAuthorizationCodeRequest(stream)) {
callback(authorization_code);
return authorization_code;
}
std::stringstream input, output;
IHttpRequest::Pointer request;
{
std::lock_guard<std::mutex> lock(provider()->auth_mutex());
auto previous_code = provider()->auth()->authorization_code();
provider()->auth()->set_authorization_code(authorization_code);
request = provider()->auth()->exchangeAuthorizationCodeRequest(input);
provider()->auth()->set_authorization_code(previous_code);
}
if (IHttpRequest::isSuccess(
r->send(request.get(), input, output, nullptr))) {
std::lock_guard<std::mutex> lock(provider()->auth_mutex());
auto token = provider()
->auth()
->exchangeAuthorizationCodeResponse(output)
->refresh_token_;
callback(token);
return token;
} else {
callback("");
return "";
}
});
}
ExchangeCodeRequest::~ExchangeCodeRequest() { cancel(); }
} // namespace cloudstorage
/*****************************************************************************
* ExchangeCodeRequest.h
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef EXCHANGE_CODE_REQUEST_H
#define EXCHANGE_CODE_REQUEST_H
#include "Request.h"
namespace cloudstorage {
class ExchangeCodeRequest : public Request<std::string> {
public:
ExchangeCodeRequest(std::shared_ptr<CloudProvider>, const std::string& code,
ExchangeCodeCallback);
~ExchangeCodeRequest();
};
} // namespace cloudstorage;
#endif // EXCHANGE_CODE_REQUEST_H
......@@ -219,5 +219,6 @@ template class Request<bool>;
template class Request<std::shared_ptr<cloudstorage::IItem>>;
template class Request<std::vector<std::shared_ptr<cloudstorage::IItem>>>;
template class Request<std::vector<char>>;
template class Request<std::string>;
} // namespace cloudstorage
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment