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

Updated to the new IHttp.

parent 1b138e99
......@@ -122,10 +122,10 @@ AuthorizeRequest::Pointer AmazonDrive::authorizeAsync() {
output = std::make_shared<std::stringstream>(),
error = std::make_shared<std::stringstream>();
r->send(request.get(),
[=](int code, util::Output, util::Output) {
[=](IHttpRequest::Response response) {
(void)r;
if (!IHttpRequest::isSuccess(code))
return complete(Error{code, error->str()});
if (!IHttpRequest::isSuccess(response.http_code_))
return complete(Error{response.http_code_, error->str()});
try {
Json::Value response;
*output >> response;
......
......@@ -100,7 +100,7 @@ ICloudProvider::GetItemDataRequest::Pointer YouTube::getItemDataAsync(
callback(e.left());
return r->done(e.left());
}
auto i = getItemDataResponse(
auto item = getItemDataResponse(
*response_stream, id.find(AUDIO_ID_PREFIX) != std::string::npos);
auto stream = std::make_shared<std::stringstream>();
auto request = [=](std::string format) {
......@@ -123,29 +123,42 @@ ICloudProvider::GetItemDataRequest::Pointer YouTube::getItemDataAsync(
try {
Json::Value response;
*stream >> response;
auto item = i;
for (auto v : response["info"]["formats"]) {
if (v["format_id"] == response["info"]["format_id"]) {
auto nitem = static_cast<Item*>(item.get());
auto size = v["filesize"].isNull() ? IItem::UnknownSize
: v["filesize"].asUInt64();
auto nitem = std::make_shared<Item>(
i->filename() + "." + v["ext"].asString(), i->id(), size,
i->type());
nitem->set_thumbnail_url(
static_cast<Item*>(i.get())->thumbnail_url());
nitem->set_size(size);
nitem->set_filename(item->filename() + "." +
v["ext"].asString());
nitem->set_url(v["url"].asString());
item = nitem;
}
}
callback(item);
r->done(item);
if (item->size() == IItem::UnknownSize) {
auto request = http()->create(item->url(), "HEAD");
auto input = std::make_shared<std::stringstream>();
auto output = std::make_shared<std::stringstream>();
auto error = std::make_shared<std::stringstream>();
r->send(request.get(),
[=](IHttpRequest::Response response) {
if (IHttpRequest::isSuccess(response.http_code_))
static_cast<Item*>(item.get())
->set_size(response.content_length_);
callback(item);
r->done(item);
},
input, output, error);
} else {
callback(item);
r->done(item);
}
} catch (std::exception) {
Error err{IHttpRequest::Failure, stream->str()};
callback(err);
r->done(err);
}
};
if (i->type() == IItem::FileType::Audio)
if (item->type() == IItem::FileType::Audio)
r->sendRequest(request("bestaudio"), response, stream);
else
r->sendRequest(request("best"), response, stream);
......
......@@ -117,8 +117,8 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
auto auth = provider()->auth();
auto auth_callback = provider()->auth_callback();
send(r.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) {
[=](IHttpRequest::Response response) {
if (IHttpRequest::isSuccess(response.http_code_)) {
try {
auto lock = provider()->auth_lock();
auth->set_access_token(auth->refreshTokenResponse(*output));
......@@ -128,8 +128,8 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
Error err{IHttpRequest::Failure, output->str()};
return complete(err);
}
} else if (!IHttpRequest::isClientError(code) && r) {
return complete(Error{code, error_stream->str()});
} else if (!IHttpRequest::isClientError(response.http_code_) && r) {
return complete(Error{response.http_code_, error_stream->str()});
}
if (auth_callback->userConsentRequired(*provider()) ==
ICloudProvider::IAuthCallback::Status::WaitForAuthorizationCode) {
......@@ -142,8 +142,8 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
error_stream = std::make_shared<std::stringstream>();
auto r = auth->exchangeAuthorizationCodeRequest(*input);
send(r.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) {
[=](IHttpRequest::Response response) {
if (IHttpRequest::isSuccess(response.http_code_)) {
try {
auto lock = provider()->auth_lock();
auth->set_access_token(
......@@ -154,14 +154,14 @@ void AuthorizeRequest::oauth2Authorization(AuthorizeCompleted complete) {
complete(Error{IHttpRequest::Failure, output->str()});
}
} else {
complete(Error{code, error_stream->str()});
complete(Error{response.http_code_, error_stream->str()});
}
},
input, output, error_stream);
};
set_server(auth->requestAuthorizationCode(code));
} else {
complete(Error{code, error_stream->str()});
complete(Error{response.http_code_, error_stream->str()});
}
},
input, output, error_stream);
......
......@@ -50,8 +50,8 @@ ExchangeCodeRequest::ExchangeCodeRequest(std::shared_ptr<CloudProvider> p,
provider()->auth()->set_authorization_code(previous_code);
}
r->send(request.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) {
[=](IHttpRequest::Response response) {
if (IHttpRequest::isSuccess(response.http_code_)) {
try {
auto lock = provider()->auth_lock();
auto auth_token =
......@@ -67,7 +67,7 @@ ExchangeCodeRequest::ExchangeCodeRequest(std::shared_ptr<CloudProvider> p,
r->done(err);
}
} else {
Error e{code, error->str()};
Error e{response.http_code_, error->str()};
callback(e);
r->done(e);
}
......
......@@ -205,32 +205,35 @@ void Request<T>::sendRequest(RequestFactory factory, RequestCompleted complete,
auto r = factory(input);
authorize(r);
send(r.get(),
[=](int code, util::Output, util::Output) {
if (IHttpRequest::isSuccess(code)) return complete(output);
if (this->reauthorize(code)) {
[=](IHttpRequest::Response response) {
if (IHttpRequest::isSuccess(response.http_code_))
return complete(output);
if (this->reauthorize(response.http_code_)) {
this->reauthorize([=](EitherError<void> e) {
if (e.left()) {
if (e.left()->code_ != IHttpRequest::Aborted)
return complete(e.left());
else
return complete(Error{code, error_stream->str()});
return complete(
Error{response.http_code_, error_stream->str()});
}
auto input = std::make_shared<std::stringstream>(),
error_stream = std::make_shared<std::stringstream>();
auto r = factory(input);
authorize(r);
this->send(r.get(),
[=](int code, util::Output output, util::Output) {
(void)request;
if (IHttpRequest::isSuccess(code))
complete(output);
else
complete(Error{code, error_stream->str()});
},
input, output, error_stream, download, upload);
this->send(
r.get(),
[=](IHttpRequest::Response response) {
(void)request;
if (IHttpRequest::isSuccess(response.http_code_))
complete(output);
else
complete(Error{response.http_code_, error_stream->str()});
},
input, output, error_stream, download, upload);
});
} else {
complete(Error{code, error_stream->str()});
complete(Error{response.http_code_, error_stream->str()});
}
},
input, output, error_stream, download, upload);
......@@ -247,7 +250,7 @@ void Request<T>::send(IHttpRequest* request,
request->send(complete, input, output, error,
httpCallback(download, upload));
else
complete(IHttpRequest::Aborted, output, error);
complete({IHttpRequest::Aborted, 0, output, error});
}
template <class T>
......
......@@ -64,16 +64,20 @@ Item::Item(std::string filename, std::string id, size_t size, FileType type)
std::string Item::filename() const { return filename_; }
void Item::set_filename(std::string filename) { filename_ = filename; }
std::string Item::extension() const {
return filename_.substr(filename_.find_last_of('.') + 1, std::string::npos);
}
std::string Item::id() const { return id_; }
std::string Item::url() const { return url_; }
size_t Item::size() const { return size_; }
void Item::set_size(size_t size) { size_ = size; }
std::string Item::url() const { return url_; }
void Item::set_url(std::string url) { url_ = url; }
std::string Item::thumbnail_url() const { return thumbnail_url_; }
......
......@@ -38,9 +38,13 @@ class Item : public IItem {
Item(std::string filename, std::string id, size_t size, FileType);
std::string filename() const override;
void set_filename(std::string);
std::string extension() const override;
std::string id() const override;
size_t size() const override;
void set_size(size_t);
std::string url() const override;
void set_url(std::string);
......
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