|
| 1 | +#include "client.hpp" |
| 2 | + |
| 3 | +#include <qjsengine.h> |
| 4 | +#include <qjsvalue.h> |
| 5 | +#include <qjsvalueiterator.h> |
| 6 | +#include <qnetworkaccessmanager.h> |
| 7 | +#include <qnetworkreply.h> |
| 8 | +#include <qnetworkrequest.h> |
| 9 | +#include <qobject.h> |
| 10 | +#include <qstringview.h> |
| 11 | +#include <qtimer.h> |
| 12 | +#include <qurl.h> |
| 13 | + |
| 14 | +#include "response.hpp" |
| 15 | + |
| 16 | +namespace qs::http { |
| 17 | + |
| 18 | +HttpClient::HttpClient(QObject* parent) |
| 19 | + : QObject(parent) |
| 20 | + , mManager(new QNetworkAccessManager(this)) {} |
| 21 | + |
| 22 | +QNetworkRequest HttpClient::createRequest(const QString& url, const QJSValue& headers) { |
| 23 | + QNetworkRequest req; |
| 24 | + |
| 25 | + req.setUrl(QUrl(url)); |
| 26 | + |
| 27 | + if (headers.isObject()) { |
| 28 | + QJSValueIterator iter(headers); |
| 29 | + while (iter.hasNext()) { |
| 30 | + iter.next(); |
| 31 | + req.setRawHeader(iter.name().toUtf8(), iter.value().toString().toUtf8()); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return req; |
| 36 | +} |
| 37 | + |
| 38 | +void HttpClient::connectReply(QNetworkReply* reply, const QJSValue& callback, int timeout) { |
| 39 | + if (timeout > 0) { |
| 40 | + auto* timer = new QTimer(reply); |
| 41 | + timer->setSingleShot(true); |
| 42 | + timer->setInterval(timeout); |
| 43 | + connect(timer, &QTimer::timeout, reply, &QNetworkReply::abort); |
| 44 | + timer->start(); |
| 45 | + } |
| 46 | + |
| 47 | + QJSEngine* engine = qjsEngine(this); |
| 48 | + |
| 49 | + if (!engine) { |
| 50 | + return; |
| 51 | + } |
| 52 | + if (callback.isCallable()) { |
| 53 | + connect(reply, &QNetworkReply::finished, this, [reply, engine, callback]() mutable { |
| 54 | + reply->deleteLater(); |
| 55 | + auto* resp = new HttpResponse(reply); |
| 56 | + auto jsResp = engine->newQObject(resp); |
| 57 | + QJSEngine::setObjectOwnership(resp, QJSEngine::JavaScriptOwnership); |
| 58 | + |
| 59 | + callback.call(QJSValueList() << jsResp); |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void HttpClient::request( |
| 65 | + const QString& url, |
| 66 | + const QString& verb, |
| 67 | + const QJSValue& options, |
| 68 | + const QJSValue& callback |
| 69 | +) { |
| 70 | + QJSValue headers; |
| 71 | + QByteArray body; |
| 72 | + int timeout = -1; |
| 73 | + |
| 74 | + if (options.isObject()) { |
| 75 | + if (options.hasOwnProperty("headers")) { |
| 76 | + headers = options.property("headers"); |
| 77 | + } |
| 78 | + if (options.hasOwnProperty("body")) { |
| 79 | + auto bodyProp = options.property("body"); |
| 80 | + body = bodyProp.toVariant().toByteArray(); |
| 81 | + } |
| 82 | + if (options.hasOwnProperty("timeout")) { |
| 83 | + auto timeoutProp = options.property("timeout"); |
| 84 | + if (timeoutProp.isNumber()) { |
| 85 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + auto req = this->createRequest(url, headers); |
| 91 | + auto* reply = this->mManager->sendCustomRequest(req, verb.toUtf8(), body); |
| 92 | + |
| 93 | + this->connectReply(reply, callback, timeout); |
| 94 | +} |
| 95 | + |
| 96 | +void HttpClient::get(const QString& url, const QJSValue& options, const QJSValue& callback) { |
| 97 | + QJSValue headers; |
| 98 | + int timeout = -1; |
| 99 | + |
| 100 | + if (options.isObject()) { |
| 101 | + if (options.hasOwnProperty("headers")) { |
| 102 | + headers = options.property("headers"); |
| 103 | + } |
| 104 | + if (options.hasOwnProperty("timeout")) { |
| 105 | + auto timeoutProp = options.property("timeout"); |
| 106 | + if (timeoutProp.isNumber()) { |
| 107 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + auto req = this->createRequest(url, headers); |
| 113 | + auto* reply = this->mManager->get(req); |
| 114 | + |
| 115 | + this->connectReply(reply, callback, timeout); |
| 116 | +} |
| 117 | + |
| 118 | +void HttpClient::post( |
| 119 | + const QString& url, |
| 120 | + const QVariant& body, |
| 121 | + const QJSValue& options, |
| 122 | + const QJSValue& callback |
| 123 | +) { |
| 124 | + auto bodyBytes = body.toByteArray(); |
| 125 | + QJSValue headers; |
| 126 | + int timeout = -1; |
| 127 | + |
| 128 | + if (options.isObject()) { |
| 129 | + if (options.hasOwnProperty("headers")) { |
| 130 | + headers = options.property("headers"); |
| 131 | + } |
| 132 | + if (options.hasOwnProperty("timeout")) { |
| 133 | + auto timeoutProp = options.property("timeout"); |
| 134 | + if (timeoutProp.isNumber()) { |
| 135 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + auto req = this->createRequest(url, headers); |
| 141 | + auto* reply = this->mManager->post(req, bodyBytes); |
| 142 | + |
| 143 | + this->connectReply(reply, callback, timeout); |
| 144 | +} |
| 145 | + |
| 146 | +void HttpClient::put( |
| 147 | + const QString& url, |
| 148 | + const QVariant& body, |
| 149 | + const QJSValue& options, |
| 150 | + const QJSValue& callback |
| 151 | +) { |
| 152 | + auto bodyBytes = body.toByteArray(); |
| 153 | + QJSValue headers; |
| 154 | + int timeout = -1; |
| 155 | + |
| 156 | + if (options.isObject()) { |
| 157 | + if (options.hasOwnProperty("headers")) { |
| 158 | + headers = options.property("headers"); |
| 159 | + } |
| 160 | + if (options.hasOwnProperty("timeout")) { |
| 161 | + auto timeoutProp = options.property("timeout"); |
| 162 | + if (timeoutProp.isNumber()) { |
| 163 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + auto req = this->createRequest(url, headers); |
| 169 | + auto* reply = this->mManager->put(req, bodyBytes); |
| 170 | + |
| 171 | + this->connectReply(reply, callback, timeout); |
| 172 | +} |
| 173 | + |
| 174 | +void HttpClient::del(const QString& url, const QJSValue& options, const QJSValue& callback) { |
| 175 | + QJSValue headers; |
| 176 | + int timeout = -1; |
| 177 | + |
| 178 | + if (options.isObject()) { |
| 179 | + if (options.hasOwnProperty("headers")) { |
| 180 | + headers = options.property("headers"); |
| 181 | + } |
| 182 | + if (options.hasOwnProperty("timeout")) { |
| 183 | + auto timeoutProp = options.property("timeout"); |
| 184 | + if (timeoutProp.isNumber()) { |
| 185 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + auto req = this->createRequest(url, headers); |
| 191 | + auto* reply = this->mManager->deleteResource(req); |
| 192 | + |
| 193 | + this->connectReply(reply, callback, timeout); |
| 194 | +} |
| 195 | + |
| 196 | +void HttpClient::head(const QString& url, const QJSValue& options, const QJSValue& callback) { |
| 197 | + QJSValue headers; |
| 198 | + int timeout = -1; |
| 199 | + |
| 200 | + if (options.isObject()) { |
| 201 | + if (options.hasOwnProperty("headers")) { |
| 202 | + headers = options.property("headers"); |
| 203 | + } |
| 204 | + if (options.hasOwnProperty("timeout")) { |
| 205 | + auto timeoutProp = options.property("timeout"); |
| 206 | + if (timeoutProp.isNumber()) { |
| 207 | + timeout = static_cast<int>(timeoutProp.toNumber()); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + auto req = this->createRequest(url, headers); |
| 213 | + auto* reply = this->mManager->head(req); |
| 214 | + |
| 215 | + this->connectReply(reply, callback, timeout); |
| 216 | +} |
| 217 | + |
| 218 | +} // namespace qs::http |
0 commit comments