Commit ecb0d2bf authored by Paweł Wegner's avatar Paweł Wegner
Browse files

GetItemDataRequest api cleanup.

parent 88b43b2c
......@@ -30,7 +30,7 @@
#include "Utility/Item.h"
#include "Utility/Utility.h"
#include "Request/GetItemDataRequest.h"
#include "Request/Request.h"
namespace cloudstorage {
......@@ -46,9 +46,9 @@ bool Dropbox::reauthorize(int code) const { return code == 400 || code == 401; }
ICloudProvider::GetItemDataRequest::Pointer Dropbox::getItemDataAsync(
const std::string& id, GetItemDataCallback callback) {
return make_unique<cloudstorage::GetItemDataRequest>(
shared_from_this(), id, callback,
[id](cloudstorage::GetItemDataRequest* r) -> IItem::Pointer {
auto f = make_unique<Request<IItem::Pointer>>(shared_from_this());
f->set_resolver(
[this, id, callback](Request<IItem::Pointer>* r) -> IItem::Pointer {
auto item_data = [r, id](std::ostream& input) {
auto request = make_unique<HttpRequest>(
"https://api.dropboxapi.com/2/files/get_metadata",
......@@ -61,12 +61,17 @@ ICloudProvider::GetItemDataRequest::Pointer Dropbox::getItemDataAsync(
return request;
};
std::stringstream output;
if (!HttpRequest::isSuccess(r->sendRequest(item_data, output)))
if (!HttpRequest::isSuccess(r->sendRequest(item_data, output))) {
callback(nullptr);
return nullptr;
}
Json::Value response;
output >> response;
auto item = toItem(response);
if (item->type() == IItem::FileType::Directory) return item;
if (item->type() == IItem::FileType::Directory) {
callback(item);
return item;
}
auto temporary_link = [r, id](std::ostream& input) {
auto request = make_unique<HttpRequest>(
"https://api.dropboxapi.com/2/files/get_temporary_link",
......@@ -78,12 +83,16 @@ ICloudProvider::GetItemDataRequest::Pointer Dropbox::getItemDataAsync(
input << Json::FastWriter().write(parameter);
return request;
};
if (!HttpRequest::isSuccess(r->sendRequest(temporary_link, output)))
if (!HttpRequest::isSuccess(r->sendRequest(temporary_link, output))) {
callback(item);
return item;
}
output >> response;
static_cast<Item*>(item.get())->set_url(response["link"].asString());
callback(item);
return item;
});
return f;
}
HttpRequest::Pointer Dropbox::getItemDataRequest(const std::string&,
......
......@@ -29,17 +29,23 @@
namespace cloudstorage {
GetItemDataRequest::GetItemDataRequest(std::shared_ptr<CloudProvider> p,
const std::string& id, Callback callback,
Factory f)
const std::string& id, Callback callback)
: Request(p), id_(id), callback_(callback) {
if (f)
set_resolver([this, f](Request*) {
auto result = f(this);
callback_(result);
return result;
});
else
set_resolver([this](Request*) { return resolve(this); });
set_resolver([this](Request*) -> IItem::Pointer {
std::stringstream response_stream;
int code = sendRequest(
[this](std::ostream& input) {
return provider()->getItemDataRequest(id_, input);
},
response_stream);
if (HttpRequest::isSuccess(code)) {
auto i = provider()->getItemDataResponse(response_stream);
callback_(i);
return i;
}
callback_(nullptr);
return nullptr;
});
}
GetItemDataRequest::~GetItemDataRequest() { cancel(); }
......@@ -48,20 +54,4 @@ GetItemDataRequest::Callback GetItemDataRequest::callback() const {
return callback_;
}
IItem::Pointer GetItemDataRequest::resolve(GetItemDataRequest* t) {
std::stringstream response_stream;
int code = t->sendRequest(
[t](std::ostream& input) {
return t->provider()->getItemDataRequest(t->id_, input);
},
response_stream);
if (HttpRequest::isSuccess(code)) {
auto i = t->provider()->getItemDataResponse(response_stream);
t->callback_(i);
return i;
}
t->callback_(nullptr);
return nullptr;
}
} // namespace cloudstorage
......@@ -33,17 +33,13 @@ namespace cloudstorage {
class GetItemDataRequest : public Request<IItem::Pointer> {
public:
using Callback = GetItemDataCallback;
using Factory = std::function<IItem::Pointer(GetItemDataRequest*)>;
GetItemDataRequest(std::shared_ptr<CloudProvider>, const std::string& id,
Callback, Factory = nullptr);
Callback);
~GetItemDataRequest();
Callback callback() const;
protected:
static IItem::Pointer resolve(GetItemDataRequest*);
private:
std::string id_;
Callback callback_;
......
Supports Markdown
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