Commit f4c2667a authored by Paweł Wegner's avatar Paweł Wegner

Refactored listDirectory to use EitherError.

parent bdd78271
......@@ -57,7 +57,9 @@ class MockProvider : public ICloudProvider {
cancelled_ = true;
result_.wait();
}
std::vector<IItem::Pointer> result() override { return result_.get(); }
EitherError<std::vector<IItem::Pointer>> result() override {
return result_.get();
}
private:
IListDirectoryCallback::Pointer callback_;
......
......@@ -47,7 +47,7 @@ class Callback : public cloudstorage::ICloudProvider::ICallback {
IItem::Pointer getChild(ICloudProvider::Pointer provider, IItem::Pointer item,
const std::string& filename) {
for (auto i : provider->listDirectoryAsync(item)->result())
for (auto i : *provider->listDirectoryAsync(item)->result().right())
if (i->filename() == filename) return i;
return nullptr;
}
......@@ -69,8 +69,10 @@ int main(int, char**) {
for (auto p : ICloudStorage::create()->providers())
std::cout << p->name() << "\n";
} else {
for (auto item : current_provider->listDirectoryAsync(current_directory)
->result()) {
for (auto item :
*current_provider->listDirectoryAsync(current_directory)
->result()
.right()) {
std::cout << item->filename() << "\n";
}
}
......
......@@ -77,7 +77,7 @@ bool rename(Request<bool>* r, std::string dest_id, std::string source_id,
if (!directory) return false;
Request<bool>::Semaphore semaphore(r);
auto children_request = r->provider()->listDirectoryAsync(
directory, [&semaphore](const std::vector<IItem::Pointer>&) {
directory, [&semaphore](EitherError<std::vector<IItem::Pointer>>) {
semaphore.notify();
});
semaphore.wait();
......@@ -85,7 +85,7 @@ bool rename(Request<bool>* r, std::string dest_id, std::string source_id,
children_request->cancel();
return false;
}
for (auto item : children_request->result())
for (auto item : *children_request->result().right())
if (!rename(r, dest_id + "/" + item->filename(), item->id(), region,
http))
return false;
......@@ -230,7 +230,7 @@ ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
if (item->type() == IItem::FileType::Directory) {
Request<bool>::Semaphore semaphore(r);
auto children_request = r->provider()->listDirectoryAsync(
item, [&semaphore](const std::vector<IItem::Pointer>&) {
item, [&semaphore](EitherError<std::vector<IItem::Pointer>>) {
semaphore.notify();
});
semaphore.wait();
......@@ -239,7 +239,7 @@ ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
callback(false);
return false;
}
for (auto child : children_request->result()) {
for (auto child : *children_request->result().right()) {
auto delete_request =
deleteItemAsync(child, [&](bool) { semaphore.notify(); });
semaphore.wait();
......
......@@ -75,7 +75,9 @@ class ListDirectoryCallback : public cloudstorage::IListDirectoryCallback {
callback_(result);
}
void error(const std::string&) override { callback_({}); }
void error(const std::string& d) override {
callback_(cloudstorage::Error{0, d});
}
private:
cloudstorage::ListDirectoryCallback callback_;
......
......@@ -345,14 +345,18 @@ ICloudProvider::GetItemDataRequest::Pointer MegaNz::getItemDataAsync(
ICloudProvider::ListDirectoryRequest::Pointer MegaNz::listDirectoryAsync(
IItem::Pointer item, IListDirectoryCallback::Pointer callback) {
auto r = util::make_unique<Request<std::vector<IItem::Pointer>>>(
auto r = util::make_unique<Request<EitherError<std::vector<IItem::Pointer>>>>(
shared_from_this());
r->set_resolver([this, item,
callback](Request<std::vector<IItem::Pointer>>* r)
-> std::vector<IItem::Pointer> {
r->set_resolver([this, item, callback](
Request<EitherError<std::vector<IItem::Pointer>>>* r)
-> EitherError<std::vector<IItem::Pointer>> {
if (!ensureAuthorized(r)) {
if (!r->is_cancelled()) callback->error("Authorization failed.");
return {};
Error e = {401, "authorization failed"};
if (!r->is_cancelled()) {
e = {IHttpRequest::Aborted, ""};
callback->error("Authorization failed.");
}
return e;
}
std::unique_ptr<mega::MegaNode> node(
mega_->getNodeByPath(item->id().c_str()));
......
......@@ -62,20 +62,20 @@ std::string YouTube::endpoint() const { return "https://www.googleapis.com"; }
ICloudProvider::ListDirectoryRequest::Pointer YouTube::listDirectoryAsync(
IItem::Pointer item, IListDirectoryCallback::Pointer callback) {
auto r = util::make_unique<Request<std::vector<IItem::Pointer>>>(
auto r = util::make_unique<Request<EitherError<std::vector<IItem::Pointer>>>>(
shared_from_this());
auto is_fine = [](int code) { return code == 404; };
r->set_error_callback(
[callback, is_fine](Request<std::vector<IItem::Pointer>>* r, int code,
const std::string& error) {
[callback, is_fine](Request<EitherError<std::vector<IItem::Pointer>>>* r,
int code, const std::string& error) {
if (!is_fine(code)) callback->error(r->error_string(code, error));
});
r->set_resolver([item, callback, is_fine,
this](Request<std::vector<IItem::Pointer>>* r)
-> std::vector<IItem::Pointer> {
this](Request<EitherError<std::vector<IItem::Pointer>>>* r)
-> EitherError<std::vector<IItem::Pointer>> {
if (item->type() != IItem::FileType::Directory) {
callback->error("Trying to list non-directory.");
return {};
return Error{403, "trying to list non-directory"};
}
std::string page_token;
std::vector<IItem::Pointer> result;
......
......@@ -44,7 +44,8 @@ class ICloudProvider {
using Hints = std::unordered_map<std::string, std::string>;
using ExchangeCodeRequest = IRequest<EitherError<std::string>>;
using ListDirectoryRequest = IRequest<std::vector<IItem::Pointer>>;
using ListDirectoryRequest =
IRequest<EitherError<std::vector<IItem::Pointer>>>;
using GetItemRequest = IRequest<EitherError<IItem>>;
using DownloadFileRequest = IRequest<void>;
using UploadFileRequest = IRequest<void>;
......@@ -361,8 +362,8 @@ class ICloudProvider {
*/
virtual ListDirectoryRequest::Pointer listDirectoryAsync(
IItem::Pointer item,
ListDirectoryCallback callback = [](const std::vector<IItem::Pointer>&) {
}) = 0;
ListDirectoryCallback callback =
[](EitherError<std::vector<IItem::Pointer>>) {}) = 0;
/**
* Simplified version of downloadFileAsync.
......
......@@ -205,7 +205,7 @@ using CreateDirectoryCallback = std::function<void(IItem::Pointer)>;
using MoveItemCallback = std::function<void(bool)>;
using RenameItemCallback = std::function<void(bool)>;
using ListDirectoryCallback =
std::function<void(const std::vector<IItem::Pointer>&)>;
std::function<void(EitherError<std::vector<IItem::Pointer>>)>;
using DownloadFileCallback = std::function<void(bool)>;
using UploadFileCallback = std::function<void(bool)>;
using GetThumbnailCallback = std::function<void(bool)>;
......
......@@ -51,9 +51,9 @@ GetItemRequest::GetItemRequest(std::shared_ptr<CloudProvider> p,
return e;
}
current_request_ = provider()->listDirectoryAsync(
std::move(node), [](const std::vector<IItem::Pointer>&) {});
std::move(node), [](EitherError<std::vector<IItem::Pointer>>) {});
}
node = getItem(current_request_->result(), token);
node = getItem(*current_request_->result().right(), token);
}
callback_(node);
return node;
......
......@@ -29,7 +29,8 @@
namespace cloudstorage {
class ListDirectoryRequest : public Request<std::vector<IItem::Pointer>> {
class ListDirectoryRequest
: public Request<EitherError<std::vector<IItem::Pointer>>> {
public:
using ICallback = IListDirectoryCallback;
......
......@@ -224,5 +224,6 @@ template class Request<std::vector<char>>;
template class Request<std::string>;
template class Request<EitherError<std::string>>;
template class Request<EitherError<IItem>>;
template class Request<EitherError<std::vector<IItem::Pointer>>>;
} // namespace cloudstorage
......@@ -58,7 +58,7 @@ void traverse_drive(cloudstorage::ICloudProvider& drive,
std::cout << path << "\n";
if (f->type() != cloudstorage::IItem::FileType::Directory) return;
for (cloudstorage::IItem::Pointer& t :
drive.listDirectoryAsync(f)->result()) {
*drive.listDirectoryAsync(f)->result().right()) {
traverse_drive(
drive, std::move(t),
path + t->filename() +
......
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