Commit 5d705416 authored by Paweł Wegner's avatar Paweł Wegner

Refactored createDirectoryRequest to use EitherError.

parent f4c2667a
......@@ -194,7 +194,7 @@ MockProvider::MockDownloadFileRequest::MockDownloadFileRequest(
MockProvider::MockCreateDirectoryRequest::MockCreateDirectoryRequest(
IItem::Pointer, const std::string&, CreateDirectoryCallback callback) {
callback(nullptr);
callback(Error{500, "unimplemented"});
}
} // namespace cloudstorage
......@@ -121,7 +121,7 @@ class MockProvider : public ICloudProvider {
CreateDirectoryCallback);
void finish() override {}
void cancel() override {}
IItem::Pointer result() override { return nullptr; }
EitherError<IItem> result() override { return Error{500, "unimplemented"}; }
};
class MockMoveItemRequest : public MoveItemRequest {
......
......@@ -399,9 +399,9 @@ void Window::deleteItem(int item_id) {
void Window::createDirectory(QString name) {
create_directory_request_ = cloud_provider_->createDirectoryAsync(
current_directory_, name.toStdString(), [this](IItem::Pointer item) {
current_directory_, name.toStdString(), [this](EitherError<IItem> item) {
std::unique_lock<std::mutex> lock(stream_mutex());
if (item)
if (item.right())
std::cerr << "[DIAG] Successfully created directory\n";
else
std::cerr << "[FAIL] Failed to create directory\n";
......
......@@ -195,9 +195,10 @@ ICloudProvider::RenameItemRequest::Pointer AmazonS3::renameItemAsync(
ICloudProvider::CreateDirectoryRequest::Pointer AmazonS3::createDirectoryAsync(
IItem::Pointer parent, const std::string& name,
CreateDirectoryCallback callback) {
auto r = util::make_unique<Request<IItem::Pointer>>(shared_from_this());
r->set_resolver([=](Request<IItem::Pointer>* r) -> IItem::Pointer {
auto r = util::make_unique<Request<EitherError<IItem>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<IItem>>* r) -> EitherError<IItem> {
std::stringstream output;
Error error;
int code = r->sendRequest(
[=](std::ostream&) {
auto data = split(parent->id());
......@@ -206,19 +207,18 @@ ICloudProvider::CreateDirectoryRequest::Pointer AmazonS3::createDirectoryAsync(
escapePath(http(), data.second + name + "/"),
"PUT");
},
output);
output, &error);
if (!IHttpRequest::isSuccess(code)) {
callback(nullptr);
return nullptr;
callback(error);
return error;
} else {
auto path = getPath(parent->id());
if (!split(path).second.empty()) path += "/";
auto item = std::make_shared<Item>(name, path + name + "/",
IItem::FileType::Directory);
callback(item);
return item;
callback(EitherError<IItem>(item));
return EitherError<IItem>(item);
}
});
return std::move(r);
}
......
......@@ -492,19 +492,21 @@ ICloudProvider::DeleteItemRequest::Pointer MegaNz::deleteItemAsync(
ICloudProvider::CreateDirectoryRequest::Pointer MegaNz::createDirectoryAsync(
IItem::Pointer parent, const std::string& name,
CreateDirectoryCallback callback) {
auto r = util::make_unique<Request<IItem::Pointer>>(shared_from_this());
r->set_resolver([=](Request<IItem::Pointer>* r) -> IItem::Pointer {
auto r = util::make_unique<Request<EitherError<IItem>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<IItem>>* r) -> EitherError<IItem> {
if (!ensureAuthorized(r)) {
callback(nullptr);
return nullptr;
Error e{401, "authorization error"};
callback(e);
return e;
}
std::unique_ptr<mega::MegaNode> parent_node(
mega_->getNodeByPath(parent->id().c_str()));
if (!parent_node) {
callback(nullptr);
return nullptr;
Error e{404, "parent not found"};
callback(e);
return e;
}
Request<IItem::Pointer>::Semaphore semaphore(r);
Request<EitherError<IItem>>::Semaphore semaphore(r);
RequestListener listener(&semaphore);
mega_->createFolder(name.c_str(), parent_node.get(), &listener);
semaphore.wait();
......@@ -516,8 +518,9 @@ ICloudProvider::CreateDirectoryRequest::Pointer MegaNz::createDirectoryAsync(
callback(item);
return item;
} else {
callback(nullptr);
return nullptr;
Error e{500, "couldn't create folder"};
callback(e);
return e;
}
});
return std::move(r);
......
......@@ -63,24 +63,25 @@ AuthorizeRequest::Pointer OwnCloud::authorizeAsync() {
ICloudProvider::CreateDirectoryRequest::Pointer OwnCloud::createDirectoryAsync(
IItem::Pointer parent, const std::string& name,
CreateDirectoryCallback callback) {
auto r = util::make_unique<Request<IItem::Pointer>>(shared_from_this());
r->set_resolver([=](Request<IItem::Pointer>* r) -> IItem::Pointer {
auto r = util::make_unique<Request<EitherError<IItem>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<IItem>>* r) -> EitherError<IItem> {
std::stringstream response;
Error error;
int code = r->sendRequest(
[=](std::ostream&) {
return http()->create(
api_url() + "/remote.php/webdav" + parent->id() + name + "/",
"MKCOL");
},
response);
response, &error);
if (IHttpRequest::isSuccess(code)) {
IItem::Pointer item = util::make_unique<Item>(
name, parent->id() + name + "/", IItem::FileType::Directory);
callback(item);
return item;
} else {
callback(nullptr);
return nullptr;
callback(error);
return error;
}
});
return std::move(r);
......
......@@ -174,9 +174,10 @@ ICloudProvider::UploadFileRequest::Pointer YandexDisk::uploadFileAsync(
ICloudProvider::CreateDirectoryRequest::Pointer
YandexDisk::createDirectoryAsync(IItem::Pointer parent, const std::string& name,
CreateDirectoryCallback callback) {
auto r = util::make_unique<Request<IItem::Pointer>>(shared_from_this());
r->set_resolver([=](Request<IItem::Pointer>* r) -> IItem::Pointer {
auto r = util::make_unique<Request<EitherError<IItem>>>(shared_from_this());
r->set_resolver([=](Request<EitherError<IItem>>* r) -> EitherError<IItem> {
std::stringstream output;
Error error;
int code = r->sendRequest(
[=](std::ostream&) {
auto request =
......@@ -186,7 +187,7 @@ YandexDisk::createDirectoryAsync(IItem::Pointer parent, const std::string& name,
parent->id() + (parent->id().back() == '/' ? "" : "/") + name);
return request;
},
output);
output, &error);
if (IHttpRequest::isSuccess(code)) {
Json::Value json;
output >> json;
......@@ -195,7 +196,7 @@ YandexDisk::createDirectoryAsync(IItem::Pointer parent, const std::string& name,
auto request = http()->create(json["href"].asString(), "GET");
return request;
},
output);
output, &error);
if (IHttpRequest::isSuccess(code)) {
output >> json;
auto item = toItem(json);
......@@ -203,8 +204,8 @@ YandexDisk::createDirectoryAsync(IItem::Pointer parent, const std::string& name,
return item;
}
}
callback(nullptr);
return nullptr;
callback(error);
return error;
});
return std::move(r);
}
......
......@@ -51,7 +51,7 @@ class ICloudProvider {
using UploadFileRequest = IRequest<void>;
using GetItemDataRequest = IRequest<EitherError<IItem>>;
using DeleteItemRequest = IRequest<bool>;
using CreateDirectoryRequest = IRequest<IItem::Pointer>;
using CreateDirectoryRequest = IRequest<EitherError<IItem>>;
using MoveItemRequest = IRequest<bool>;
using RenameItemRequest = IRequest<bool>;
......@@ -318,7 +318,7 @@ class ICloudProvider {
*/
virtual CreateDirectoryRequest::Pointer createDirectoryAsync(
IItem::Pointer parent, const std::string& name,
CreateDirectoryCallback callback = [](IItem::Pointer) {}) = 0;
CreateDirectoryCallback callback = [](EitherError<IItem>) {}) = 0;
/**
* Moves item.
......
......@@ -201,7 +201,7 @@ 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 CreateDirectoryCallback = std::function<void(IItem::Pointer)>;
using CreateDirectoryCallback = std::function<void(EitherError<IItem>)>;
using MoveItemCallback = std::function<void(bool)>;
using RenameItemCallback = std::function<void(bool)>;
using ListDirectoryCallback =
......
......@@ -32,24 +32,26 @@ CreateDirectoryRequest::CreateDirectoryRequest(std::shared_ptr<CloudProvider> p,
const std::string& name,
CreateDirectoryCallback callback)
: Request(p), parent_(parent), name_(name), callback_(callback) {
set_resolver([this](Request* r) -> IItem::Pointer {
set_resolver([this](Request* r) -> EitherError<IItem> {
if (parent_->type() != IItem::FileType::Directory) {
callback_(nullptr);
return nullptr;
Error e{403, "parent not a directory"};
callback_(e);
return e;
}
std::stringstream output;
Error error;
int code = r->sendRequest(
[this](std::ostream& stream) {
return provider()->createDirectoryRequest(*parent_, name_, stream);
},
output);
output, &error);
if (IHttpRequest::isSuccess(code)) {
auto i = provider()->createDirectoryResponse(output);
callback_(i);
return i;
} else {
callback_(nullptr);
return nullptr;
callback_(error);
return error;
}
});
}
......
......@@ -28,7 +28,7 @@
namespace cloudstorage {
class CreateDirectoryRequest : public Request<IItem::Pointer> {
class CreateDirectoryRequest : public Request<EitherError<IItem>> {
public:
CreateDirectoryRequest(std::shared_ptr<CloudProvider>, IItem::Pointer,
const std::string&, CreateDirectoryCallback);
......
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