mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
added NetworkRequest builder functions
This commit is contained in:
parent
d3224e7a4e
commit
4f6c1a8519
3 changed files with 170 additions and 57 deletions
|
@ -44,59 +44,62 @@ NetworkRequest::~NetworkRequest()
|
|||
// assert(this->executed_);
|
||||
}
|
||||
|
||||
void NetworkRequest::setRequestType(NetworkRequestType newRequestType)
|
||||
// old
|
||||
void NetworkRequest::type(NetworkRequestType newRequestType) &
|
||||
{
|
||||
this->data->requestType_ = newRequestType;
|
||||
}
|
||||
|
||||
void NetworkRequest::setCaller(const QObject *caller)
|
||||
void NetworkRequest::setCaller(const QObject *caller) &
|
||||
{
|
||||
this->data->caller_ = caller;
|
||||
}
|
||||
|
||||
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb)
|
||||
void NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb) &
|
||||
{
|
||||
this->data->onReplyCreated_ = cb;
|
||||
}
|
||||
|
||||
void NetworkRequest::onError(NetworkErrorCallback cb)
|
||||
void NetworkRequest::onError(NetworkErrorCallback cb) &
|
||||
{
|
||||
this->data->onError_ = cb;
|
||||
}
|
||||
|
||||
void NetworkRequest::onSuccess(NetworkSuccessCallback cb)
|
||||
void NetworkRequest::onSuccess(NetworkSuccessCallback cb) &
|
||||
{
|
||||
this->data->onSuccess_ = cb;
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const char *value) &
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName,
|
||||
const QByteArray &value)
|
||||
const QByteArray &value) &
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value);
|
||||
}
|
||||
|
||||
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
|
||||
void NetworkRequest::setRawHeader(const char *headerName,
|
||||
const QString &value) &
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
||||
}
|
||||
|
||||
void NetworkRequest::setTimeout(int ms)
|
||||
void NetworkRequest::setTimeout(int ms) &
|
||||
{
|
||||
this->timer->timeoutMS_ = ms;
|
||||
}
|
||||
|
||||
void NetworkRequest::setExecuteConcurrently(bool value)
|
||||
void NetworkRequest::setExecuteConcurrently(bool value) &
|
||||
{
|
||||
this->data->executeConcurrently = value;
|
||||
}
|
||||
|
||||
// TODO: rename to "authorizeTwitchV5"?
|
||||
void NetworkRequest::makeAuthorizedV5(const QString &clientID,
|
||||
const QString &oauthToken)
|
||||
const QString &oauthToken) &
|
||||
{
|
||||
this->setRawHeader("Client-ID", clientID);
|
||||
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||
|
@ -106,16 +109,105 @@ void NetworkRequest::makeAuthorizedV5(const QString &clientID,
|
|||
}
|
||||
}
|
||||
|
||||
void NetworkRequest::setPayload(const QByteArray &payload)
|
||||
void NetworkRequest::setPayload(const QByteArray &payload) &
|
||||
{
|
||||
this->data->payload_ = payload;
|
||||
}
|
||||
|
||||
void NetworkRequest::setUseQuickLoadCache(bool value)
|
||||
void NetworkRequest::setUseQuickLoadCache(bool value) &
|
||||
{
|
||||
this->data->useQuickLoadCache_ = value;
|
||||
}
|
||||
|
||||
// new
|
||||
NetworkRequest NetworkRequest::type(NetworkRequestType newRequestType) &&
|
||||
{
|
||||
this->data->requestType_ = newRequestType;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::caller(const QObject *caller) &&
|
||||
{
|
||||
this->data->caller_ = caller;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::onReplyCreated(NetworkReplyCreatedCallback cb) &&
|
||||
{
|
||||
this->data->onReplyCreated_ = cb;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::onError(NetworkErrorCallback cb) &&
|
||||
{
|
||||
this->data->onError_ = cb;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::onSuccess(NetworkSuccessCallback cb) &&
|
||||
{
|
||||
this->data->onSuccess_ = cb;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::header(const char *headerName,
|
||||
const char *value) &&
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::header(const char *headerName,
|
||||
const QByteArray &value) &&
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::header(const char *headerName,
|
||||
const QString &value) &&
|
||||
{
|
||||
this->data->request_.setRawHeader(headerName, value.toUtf8());
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::timeout(int ms) &&
|
||||
{
|
||||
this->timer->timeoutMS_ = ms;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::concurrent() &&
|
||||
{
|
||||
this->data->executeConcurrently = true;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
// TODO: rename to "authorizeTwitchV5"?
|
||||
NetworkRequest NetworkRequest::authorizeTwitchV5(const QString &clientID,
|
||||
const QString &oauthToken) &&
|
||||
{
|
||||
this->setRawHeader("Client-ID", clientID);
|
||||
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
|
||||
if (!oauthToken.isEmpty())
|
||||
{
|
||||
this->setRawHeader("Authorization", "OAuth " + oauthToken);
|
||||
}
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::payload(const QByteArray &payload) &&
|
||||
{
|
||||
this->data->payload_ = payload;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
NetworkRequest NetworkRequest::quickLoad() &&
|
||||
{
|
||||
this->data->useQuickLoadCache_ = true;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
void NetworkRequest::execute()
|
||||
{
|
||||
this->executed_ = true;
|
||||
|
|
|
@ -35,27 +35,51 @@ public:
|
|||
explicit NetworkRequest(
|
||||
QUrl url, NetworkRequestType requestType = NetworkRequestType::Get);
|
||||
|
||||
// Enable move
|
||||
NetworkRequest(NetworkRequest &&other) = default;
|
||||
NetworkRequest &operator=(NetworkRequest &&other) = default;
|
||||
|
||||
// Disable copy
|
||||
NetworkRequest(const NetworkRequest &other) = delete;
|
||||
NetworkRequest &operator=(const NetworkRequest &other) = delete;
|
||||
|
||||
~NetworkRequest();
|
||||
|
||||
void setRequestType(NetworkRequestType newRequestType);
|
||||
// old
|
||||
void type(NetworkRequestType newRequestType) &;
|
||||
|
||||
void onReplyCreated(NetworkReplyCreatedCallback cb);
|
||||
void onError(NetworkErrorCallback cb);
|
||||
void onSuccess(NetworkSuccessCallback cb);
|
||||
void onReplyCreated(NetworkReplyCreatedCallback cb) &;
|
||||
void onError(NetworkErrorCallback cb) &;
|
||||
void onSuccess(NetworkSuccessCallback cb) &;
|
||||
|
||||
void setPayload(const QByteArray &payload);
|
||||
void setUseQuickLoadCache(bool value);
|
||||
void setCaller(const QObject *caller);
|
||||
void setRawHeader(const char *headerName, const char *value);
|
||||
void setRawHeader(const char *headerName, const QByteArray &value);
|
||||
void setRawHeader(const char *headerName, const QString &value);
|
||||
void setTimeout(int ms);
|
||||
void setExecuteConcurrently(bool value);
|
||||
void setPayload(const QByteArray &payload) &;
|
||||
void setUseQuickLoadCache(bool value) &;
|
||||
void setCaller(const QObject *caller) &;
|
||||
void setRawHeader(const char *headerName, const char *value) &;
|
||||
void setRawHeader(const char *headerName, const QByteArray &value) &;
|
||||
void setRawHeader(const char *headerName, const QString &value) &;
|
||||
void setTimeout(int ms) &;
|
||||
void setExecuteConcurrently(bool value) &;
|
||||
void makeAuthorizedV5(const QString &clientID,
|
||||
const QString &oauthToken = QString());
|
||||
const QString &oauthToken = QString()) &;
|
||||
|
||||
// new
|
||||
NetworkRequest type(NetworkRequestType newRequestType) &&;
|
||||
|
||||
NetworkRequest onReplyCreated(NetworkReplyCreatedCallback cb) &&;
|
||||
NetworkRequest onError(NetworkErrorCallback cb) &&;
|
||||
NetworkRequest onSuccess(NetworkSuccessCallback cb) &&;
|
||||
|
||||
NetworkRequest payload(const QByteArray &payload) &&;
|
||||
NetworkRequest quickLoad() &&;
|
||||
NetworkRequest caller(const QObject *caller) &&;
|
||||
NetworkRequest header(const char *headerName, const char *value) &&;
|
||||
NetworkRequest header(const char *headerName, const QByteArray &value) &&;
|
||||
NetworkRequest header(const char *headerName, const QString &value) &&;
|
||||
NetworkRequest timeout(int ms) &&;
|
||||
NetworkRequest concurrent() &&;
|
||||
NetworkRequest authorizeTwitchV5(const QString &clientID,
|
||||
const QString &oauthToken = QString()) &&;
|
||||
|
||||
void execute();
|
||||
|
||||
|
|
|
@ -334,12 +334,11 @@ int Image::height() const
|
|||
|
||||
void Image::load()
|
||||
{
|
||||
NetworkRequest req(this->url().string);
|
||||
req.setExecuteConcurrently(true);
|
||||
req.setCaller(&this->object_);
|
||||
req.setUseQuickLoadCache(true);
|
||||
|
||||
req.onSuccess([that = this, weak = weakOf(this)](auto result) -> Outcome {
|
||||
NetworkRequest(this->url().string)
|
||||
.concurrent()
|
||||
.caller(&this->object_)
|
||||
.quickLoad()
|
||||
.onSuccess([that = this, weak = weakOf(this)](auto result) -> Outcome {
|
||||
auto shared = weak.lock();
|
||||
if (!shared)
|
||||
return Failure;
|
||||
|
@ -358,9 +357,8 @@ void Image::load()
|
|||
}));
|
||||
|
||||
return Success;
|
||||
});
|
||||
|
||||
req.onError([weak = weakOf(this)](auto result) -> bool {
|
||||
})
|
||||
.onError([weak = weakOf(this)](auto /*result*/) -> bool {
|
||||
auto shared = weak.lock();
|
||||
if (!shared)
|
||||
return false;
|
||||
|
@ -368,9 +366,8 @@ void Image::load()
|
|||
shared->empty_ = true;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
req.execute();
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
bool Image::operator==(const Image &other) const
|
||||
|
|
Loading…
Reference in a new issue