Commit 7525b245 authored by Paweł Wegner's avatar Paweł Wegner

ExchangeCodeRequest: refactored to use EitherError.

parent b9a13b50
......@@ -282,8 +282,9 @@ ICloudProvider::Hints MegaNz::hints() const {
ICloudProvider::ExchangeCodeRequest::Pointer MegaNz::exchangeCodeAsync(
const std::string& code, ExchangeCodeCallback callback) {
auto r = util::make_unique<Request<std::string>>(shared_from_this());
r->set_resolver([this, code, callback](Request<std::string>*) {
auto r =
util::make_unique<Request<EitherError<std::string>>>(shared_from_this());
r->set_resolver([this, code, callback](Request<EitherError<std::string>>*) {
auto token = authorizationCodeToToken(code);
callback(token->token_);
return token->token_;
......
......@@ -43,7 +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 ExchangeCodeRequest = IRequest<EitherError<std::string>>;
using ListDirectoryRequest = IRequest<std::vector<IItem::Pointer>>;
using GetItemRequest = IRequest<IItem::Pointer>;
using DownloadFileRequest = IRequest<void>;
......@@ -215,7 +215,7 @@ class ICloudProvider {
*/
virtual ExchangeCodeRequest::Pointer exchangeCodeAsync(
const std::string& code,
ExchangeCodeCallback = [](const std::string&) {}) = 0;
ExchangeCodeCallback = [](EitherError<std::string>) {}) = 0;
/**
* Lists directory.
......
......@@ -195,7 +195,7 @@ class Either {
template <class T>
using EitherError = Either<Error, T>;
using ExchangeCodeCallback = std::function<void(const std::string&)>;
using ExchangeCodeCallback = std::function<void(EitherError<std::string>)>;
using GetItemCallback = std::function<void(IItem::Pointer)>;
using GetItemDataCallback = std::function<void(IItem::Pointer)>;
using DeleteItemCallback = std::function<void(bool)>;
......
......@@ -31,36 +31,38 @@ 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 "";
}
});
set_resolver(
[this, authorization_code, callback](
Request<EitherError<std::string>>* r) -> EitherError<std::string> {
std::stringstream stream;
if (!provider()->auth()->exchangeAuthorizationCodeRequest(stream)) {
callback(authorization_code);
return authorization_code;
}
std::stringstream input, output, error;
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);
}
int code = r->send(request.get(), input, output, &error);
if (IHttpRequest::isSuccess(code)) {
std::lock_guard<std::mutex> lock(provider()->auth_mutex());
auto token = provider()
->auth()
->exchangeAuthorizationCodeResponse(output)
->refresh_token_;
callback(token);
return token;
} else {
Error e{code, error.str()};
callback(e);
return e;
}
});
}
ExchangeCodeRequest::~ExchangeCodeRequest() { cancel(); }
......
......@@ -28,7 +28,7 @@
namespace cloudstorage {
class ExchangeCodeRequest : public Request<std::string> {
class ExchangeCodeRequest : public Request<EitherError<std::string>> {
public:
ExchangeCodeRequest(std::shared_ptr<CloudProvider>, const std::string& code,
ExchangeCodeCallback);
......
......@@ -220,5 +220,6 @@ 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>;
template class Request<EitherError<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