1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-17 11:26:26 +02:00

172 lines
5.0 KiB
C++
Raw Normal View History

2016-12-27 21:25:07 +01:00
#include <stable.h>
#include "webapi.h"
using namespace jop;
2017-01-11 00:28:51 +01:00
WebApi::WebApi() {
baseUrl_ = "";
2016-12-27 21:25:07 +01:00
sessionId_ = "";
connect(&manager_, SIGNAL(finished(QNetworkReply*)), this, SLOT(request_finished(QNetworkReply*)));
}
QString WebApi::baseUrl() const {
return baseUrl_;
}
2017-01-03 19:42:01 +01:00
void WebApi::execRequest(HttpMethod method, const QString &path, const QUrlQuery &query, const QUrlQuery &data, const QString& tag) {
2017-01-11 00:28:51 +01:00
if (baseUrl() == "") {
qCritical() << "Trying to execute request before base URL has been set";
QJsonObject obj;
obj["error"] = "Trying to execute request before base URL has been set";
emit requestDone(obj, tag);
return;
}
2016-12-27 21:25:07 +01:00
QueuedRequest r;
r.method = method;
r.path = path;
r.query = query;
r.data = data;
r.tag = tag;
2017-01-03 19:42:01 +01:00
r.buffer = NULL;
r.reply = NULL;
2016-12-27 21:25:07 +01:00
queuedRequests_ << r;
processQueue();
}
2017-01-03 19:42:01 +01:00
void WebApi::post(const QString& path,const QUrlQuery& query, const QUrlQuery& data, const QString& tag) { execRequest(HttpMethod::POST, path, query, data, tag); }
void WebApi::get(const QString& path,const QUrlQuery& query, const QUrlQuery& data, const QString& tag) { execRequest(HttpMethod::GET, path, query, data, tag); }
void WebApi::put(const QString& path,const QUrlQuery& query, const QUrlQuery& data, const QString& tag) { execRequest(HttpMethod::PUT, path, query, data, tag); }
void WebApi::del(const QString &path, const QUrlQuery &query, const QUrlQuery &data, const QString &tag) { execRequest(HttpMethod::DEL, path, query, data, tag); }
void WebApi::patch(const QString &path, const QUrlQuery &query, const QUrlQuery &data, const QString &tag) { execRequest(HttpMethod::PATCH, path, query, data, tag); }
2016-12-27 21:25:07 +01:00
void WebApi::setSessionId(const QString &v) {
sessionId_ = v;
}
void WebApi::abortAll() {
for (int i = 0; i < inProgressRequests_.size(); i++) {
QueuedRequest r = inProgressRequests_[i];
if (r.reply) {
r.reply->abort();
// TODO: Delete r.reply?
}
}
for (int i = 0; i < queuedRequests_.size(); i++) {
QueuedRequest r = queuedRequests_[i];
if (r.reply) {
r.reply->abort();
// TODO: Delete r.reply?
}
}
queuedRequests_.size();
}
2016-12-27 21:25:07 +01:00
void WebApi::processQueue() {
if (!queuedRequests_.size() || inProgressRequests_.size() >= 50) return;
2017-01-10 18:05:58 +01:00
QueuedRequest r = queuedRequests_.takeFirst();
2016-12-27 21:25:07 +01:00
QString url = baseUrl_ + "/" + r.path;
2016-12-31 10:48:18 +01:00
QUrlQuery query = r.query;
if (sessionId_ != "") {
query.addQueryItem("session", sessionId_);
}
url += "?" + query.toString(QUrl::FullyEncoded);
2016-12-27 21:25:07 +01:00
QNetworkRequest* request = new QNetworkRequest(url);
request->setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkReply* reply = NULL;
2017-01-03 19:42:01 +01:00
if (r.method == jop::PATCH) {
// TODO: Delete buffer when done
QBuffer* buffer = new QBuffer();
buffer->open(QBuffer::ReadWrite);
buffer->write(r.data.toString(QUrl::FullyEncoded).toUtf8());
buffer->seek(0);
r.buffer = buffer;
reply = manager_.sendCustomRequest(*request, "PATCH", buffer);
2016-12-27 21:25:07 +01:00
}
2017-01-03 19:42:01 +01:00
if (r.method == jop::GET) {
reply = manager_.get(*request);
}
if (r.method == jop::POST) {
2016-12-27 21:25:07 +01:00
reply = manager_.post(*request, r.data.toString(QUrl::FullyEncoded).toUtf8());
}
2017-01-03 19:42:01 +01:00
if (r.method == jop::PUT) {
2016-12-27 21:25:07 +01:00
reply = manager_.put(*request, r.data.toString(QUrl::FullyEncoded).toUtf8());
}
2017-01-03 19:42:01 +01:00
if (r.method == jop::DEL) {
reply = manager_.deleteResource(*request);
}
2016-12-27 21:25:07 +01:00
if (!reply) {
qWarning() << "WebApi::processQueue(): reply object was not created - invalid request method";
return;
}
r.reply = reply;
r.request = request;
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(request_error(QNetworkReply::NetworkError)));
QStringList cmd;
cmd << "curl";
2017-01-03 19:42:01 +01:00
if (r.method == jop::PUT) cmd << "-X" << "PUT";
if (r.method == jop::PATCH) cmd << "-X" << "PATCH";
if (r.method == jop::DEL) cmd << "-X" << "DELETE";
2016-12-27 21:25:07 +01:00
2017-01-03 19:42:01 +01:00
if (r.method != jop::GET && r.method != jop::DEL) {
cmd << "--data" << "'" + r.data.toString(QUrl::FullyEncoded) + "'";
}
2017-01-05 18:59:01 +01:00
cmd << "'" + url + "'";
2016-12-31 10:48:18 +01:00
qDebug().noquote() << cmd.join(" ");
2016-12-27 21:25:07 +01:00
inProgressRequests_.push_back(r);
}
void WebApi::request_finished(QNetworkReply *reply) {
QByteArray responseBodyBA = reply->readAll();
QJsonObject response;
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(responseBodyBA, &err);
if (err.error != QJsonParseError::NoError) {
2017-01-11 15:18:56 +01:00
QString errorMessage = "Could not parse JSON: " + err.errorString() + "\n" + QString(responseBodyBA);
qWarning().noquote() << errorMessage;
response["error"] = errorMessage;
2016-12-27 21:25:07 +01:00
} else {
response = doc.object();
2017-01-05 18:59:01 +01:00
if (response.contains("error") && !response["error"].isNull()) {
2017-01-03 19:42:01 +01:00
qWarning().noquote() << "API error:" << QString(responseBodyBA);
}
2016-12-27 21:25:07 +01:00
}
2016-12-29 21:51:10 +01:00
for (int i = 0; i < inProgressRequests_.size(); i++) {
2016-12-27 21:25:07 +01:00
QueuedRequest r = inProgressRequests_[i];
if (r.reply == reply) {
inProgressRequests_.erase(inProgressRequests_.begin() + i);
emit requestDone(response, r.tag);
break;
}
}
processQueue();
}
void WebApi::request_error(QNetworkReply::NetworkError e) {
2017-01-12 18:04:21 +01:00
qWarning() << "Network error" << e;
2016-12-27 21:25:07 +01:00
}
2017-01-11 00:28:51 +01:00
void jop::WebApi::setBaseUrl(const QString &v) {
baseUrl_ = v;
}