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

Refactored DeleteItemRequest to use EitherError.

parent 5d705416
......@@ -80,7 +80,7 @@ ICloudProvider::GetItemDataRequest::Pointer MockProvider::getItemDataAsync(
ICloudProvider::DeleteItemRequest::Pointer MockProvider::deleteItemAsync(
IItem::Pointer, DeleteItemCallback callback) {
callback(false);
callback(Error{500, ""});
return util::make_unique<MockDeleteItemRequest>();
}
......
......@@ -112,7 +112,7 @@ class MockProvider : public ICloudProvider {
public:
void finish() override {}
void cancel() override {}
bool result() override { return false; }
EitherError<void> result() override { return Error{500, ""}; }
};
class MockCreateDirectoryRequest : public CreateDirectoryRequest {
......
......@@ -388,9 +388,9 @@ void Window::downloadFile(int item_id, QUrl path) {
void Window::deleteItem(int item_id) {
ItemModel* i = directory_model_.get(item_id);
delete_item_request_ =
cloud_provider_->deleteItemAsync(i->item(), [this](bool e) {
cloud_provider_->deleteItemAsync(i->item(), [this](EitherError<void> e) {
std::unique_lock<std::mutex> lock(stream_mutex());
if (e)
if (!e.left())
std::cerr << "[DIAG] Successfully deleted file\n";
else
std::cerr << "[FAIL] Failed to delete file.\n";
......
......@@ -225,10 +225,10 @@ ICloudProvider::CreateDirectoryRequest::Pointer AmazonS3::createDirectoryAsync(
ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
IItem::Pointer item, DeleteItemCallback callback) {
auto r = util::make_unique<Request<bool>>(shared_from_this());
r->set_resolver([=](Request<bool>* r) -> bool {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<void>>* r) -> EitherError<void> {
if (item->type() == IItem::FileType::Directory) {
Request<bool>::Semaphore semaphore(r);
Request<EitherError<void>>::Semaphore semaphore(r);
auto children_request = r->provider()->listDirectoryAsync(
item, [&semaphore](EitherError<std::vector<IItem::Pointer>>) {
semaphore.notify();
......@@ -236,21 +236,24 @@ ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
semaphore.wait();
if (r->is_cancelled()) {
children_request->cancel();
callback(false);
return false;
Error e{IHttpRequest::Aborted, ""};
callback(e);
return e;
}
for (auto child : *children_request->result().right()) {
auto delete_request =
deleteItemAsync(child, [&](bool) { semaphore.notify(); });
auto delete_request = deleteItemAsync(
child, [&](EitherError<void>) { semaphore.notify(); });
semaphore.wait();
if (r->is_cancelled()) {
delete_request->cancel();
callback(false);
return false;
Error e{IHttpRequest::Aborted, ""};
callback(e);
return e;
}
}
}
std::stringstream output;
Error error;
int code = r->sendRequest(
[=](std::ostream&) {
auto data = split(item->id());
......@@ -259,13 +262,13 @@ ICloudProvider::DeleteItemRequest::Pointer AmazonS3::deleteItemAsync(
escapePath(http(), data.second),
"DELETE");
},
output);
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback(false);
return false;
callback(error);
return error;
}
callback(true);
return true;
callback(nullptr);
return nullptr;
});
return std::move(r);
}
......
......@@ -465,25 +465,28 @@ ICloudProvider::DownloadFileRequest::Pointer MegaNz::getThumbnailAsync(
ICloudProvider::DeleteItemRequest::Pointer MegaNz::deleteItemAsync(
IItem::Pointer item, DeleteItemCallback callback) {
auto r = util::make_unique<Request<bool>>(shared_from_this());
r->set_resolver([this, item, callback](Request<bool>* r) {
auto r = util::make_unique<Request<EitherError<void>>>(shared_from_this());
r->set_resolver([this, item, callback](
Request<EitherError<void>>* r) -> EitherError<void> {
if (!ensureAuthorized(r)) {
callback(false);
return false;
Error e{401, "authrization failed"};
callback(e);
return e;
}
std::unique_ptr<mega::MegaNode> node(
mega_->getNodeByPath(item->id().c_str()));
Request<bool>::Semaphore semaphore(r);
Request<EitherError<void>>::Semaphore semaphore(r);
RequestListener listener(&semaphore);
mega_->remove(node.get(), &listener);
semaphore.wait();
mega_->removeRequestListener(&listener);
if (listener.status_ == Listener::SUCCESS) {
callback(true);
return true;
callback(nullptr);
return nullptr;
} else {
callback(false);
return false;
Error e{500, ""};
callback(e);
return e;
}
});
return std::move(r);
......
......@@ -50,7 +50,7 @@ class ICloudProvider {
using DownloadFileRequest = IRequest<void>;
using UploadFileRequest = IRequest<void>;
using GetItemDataRequest = IRequest<EitherError<IItem>>;
using DeleteItemRequest = IRequest<bool>;
using DeleteItemRequest = IRequest<EitherError<void>>;
using CreateDirectoryRequest = IRequest<EitherError<IItem>>;
using MoveItemRequest = IRequest<bool>;
using RenameItemRequest = IRequest<bool>;
......@@ -302,7 +302,8 @@ class ICloudProvider {
* @return object representing the pending request
*/
virtual DeleteItemRequest::Pointer deleteItemAsync(
IItem::Pointer item, DeleteItemCallback callback = [](bool) {}) = 0;
IItem::Pointer item,
DeleteItemCallback callback = [](EitherError<void>) {}) = 0;
/**
* Creates directory in cloud provider.
......
......@@ -194,13 +194,26 @@ class Either {
std::shared_ptr<Right> right_;
};
template <class Left>
class Either<Left, void> {
public:
Either(std::nullptr_t) {}
Either(const Left& left) : left_(std::make_shared<Left>(left)) {}
Either(std::shared_ptr<Left> left) : left_(left) {}
std::shared_ptr<Left> left() const { return left_; }
private:
std::shared_ptr<Left> left_;
};
template <class T>
using EitherError = Either<Error, T>;
using ExchangeCodeCallback = std::function<void(EitherError<std::string>)>;
using GetItemCallback = std::function<void(EitherError<IItem>)>;
using GetItemDataCallback = std::function<void(EitherError<IItem>)>;
using DeleteItemCallback = std::function<void(bool)>;
using DeleteItemCallback = std::function<void(EitherError<void>)>;
using CreateDirectoryCallback = std::function<void(EitherError<IItem>)>;
using MoveItemCallback = std::function<void(bool)>;
using RenameItemCallback = std::function<void(bool)>;
......
......@@ -31,19 +31,20 @@ DeleteItemRequest::DeleteItemRequest(std::shared_ptr<CloudProvider> p,
IItem::Pointer item,
DeleteItemCallback callback)
: Request(p), item_(item), callback_(callback) {
set_resolver([this](Request<bool>* r) {
set_resolver([this](Request* r) -> EitherError<void> {
std::stringstream output;
Error error;
int code = r->sendRequest(
[this](std::ostream& stream) {
return provider()->deleteItemRequest(*item_, stream);
},
output);
output, &error);
if (IHttpRequest::isSuccess(code)) {
callback_(true);
return true;
callback_(nullptr);
return nullptr;
} else {
callback_(false);
return false;
callback_(error);
return error;
}
});
}
......
......@@ -28,7 +28,7 @@
namespace cloudstorage {
class DeleteItemRequest : public Request<bool> {
class DeleteItemRequest : public Request<EitherError<void>> {
public:
DeleteItemRequest(std::shared_ptr<CloudProvider>, IItem::Pointer item,
DeleteItemCallback);
......
......@@ -225,5 +225,6 @@ template class Request<std::string>;
template class Request<EitherError<std::string>>;
template class Request<EitherError<IItem>>;
template class Request<EitherError<std::vector<IItem::Pointer>>>;
template class Request<EitherError<void>>;
} // 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