start to move to qnetwork
This commit is contained in:
parent
b919a64ae6
commit
2ffa67ba10
|
@ -8,8 +8,9 @@
|
|||
#include "position.h"
|
||||
|
||||
class BlitzAPI : public RestClient {
|
||||
Q_OBJECT
|
||||
public:
|
||||
BlitzAPI();
|
||||
BlitzAPI(QObject* parent = nullptr);
|
||||
|
||||
struct ChampionInfo {
|
||||
std::vector<uint32_t> skillorder;
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "runestyle.h"
|
||||
|
||||
class ClientAPI : public RestClient {
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum class ReadyCheckState : uint32_t {
|
||||
INVALID = 0,
|
||||
|
@ -183,7 +184,7 @@ public:
|
|||
explicit Conversation(const QJsonObject& json);
|
||||
};
|
||||
|
||||
ClientAPI(const ClientAccess& access);
|
||||
ClientAPI(QObject* parent, const ClientAccess& access);
|
||||
~ClientAPI();
|
||||
|
||||
ReadyCheckState getReadyCheckState();
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
#include "restclient.h"
|
||||
|
||||
class DataDragon : public RestClient {
|
||||
Q_OBJECT
|
||||
public:
|
||||
using notifyImgfunc_t = std::function<void(QPixmap)>;
|
||||
|
||||
DataDragon(const QString& locale);
|
||||
DataDragon(QObject* parent, const QString& locale);
|
||||
~DataDragon();
|
||||
DataDragon(const DataDragon&) = delete;
|
||||
DataDragon& operator=(const DataDragon&) = delete;
|
||||
|
|
|
@ -54,7 +54,6 @@ private:
|
|||
Ui::MainWindow *ui;
|
||||
QTimer* saveTimer;
|
||||
QTimer* initDoneTimer;
|
||||
std::thread lolaathread;
|
||||
DataDragon dd;
|
||||
Config conf;
|
||||
LolAutoAccept lolaa;
|
||||
|
|
|
@ -1,17 +1,21 @@
|
|||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QJsonDocument>
|
||||
#include <curl/curl.h>
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#undef DELETE
|
||||
#endif
|
||||
|
||||
static const uint32_t CURLE_COULDNT_CONNECT = 0;
|
||||
|
||||
class RestClient {
|
||||
class RestClient : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
RestClient(const QString& base);
|
||||
|
||||
RestClient(QObject* parent, const QString& base);
|
||||
RestClient(const RestClient&) = delete;
|
||||
virtual ~RestClient();
|
||||
|
||||
|
@ -24,20 +28,22 @@ public:
|
|||
};
|
||||
|
||||
struct WebException {
|
||||
CURLcode curlresponse = CURLE_OK;
|
||||
int curlresponse = CURLE_COULDNT_CONNECT;
|
||||
|
||||
WebException(CURLcode c = CURLE_OK);
|
||||
WebException(int c = CURLE_COULDNT_CONNECT);
|
||||
};
|
||||
|
||||
protected:
|
||||
QByteArray requestRaw(const QString& url, Method m = Method::GET, const QString& data = {});
|
||||
QJsonDocument request(const QString& url, Method m = Method::GET, const QString& data = {});
|
||||
void enableDebugging(bool enabled = true);
|
||||
|
||||
QT_DEPRECATED
|
||||
QString escape(const QString& in) const;
|
||||
|
||||
QString baseurl;
|
||||
|
||||
CURL* curl = nullptr; // the curl (does curling)
|
||||
QNetworkAccessManager* net;
|
||||
|
||||
QString basicauth; // basic auth code (user:pw) or empty string to disable
|
||||
#ifdef WIN32
|
||||
|
@ -45,4 +51,5 @@ protected:
|
|||
#else
|
||||
bool disableCertCheck = false;
|
||||
#endif
|
||||
bool debugging = false;
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
QT += core gui
|
||||
QT += core gui network
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
static const QString POSITIONNAMES[] = {"INVALID", "TOP", "JUNGLE", "MIDDLE", "BOTTOM", "SUPPORT"};
|
||||
|
||||
BlitzAPI::BlitzAPI() : RestClient("https://league-champion-aggregate.iesdev.com/graphql?") {}
|
||||
BlitzAPI::BlitzAPI(QObject* parent) : RestClient(parent, "https://league-champion-aggregate.iesdev.com/graphql?") {}
|
||||
|
||||
BlitzAPI::ChampionInfo::ChampionInfo() {}
|
||||
BlitzAPI::ChampionInfo::ChampionInfo(const QJsonObject& json) {
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include "json.h"
|
||||
|
||||
ClientAPI::ClientAPI(const ClientAccess& ca) : RestClient(ca.getURL()), access(ca), memImageCache(40), imageCache("runes", "") {
|
||||
ClientAPI::ClientAPI(QObject* parent, const ClientAccess& ca) : RestClient(parent, ca.getURL()), access(ca), memImageCache(40), imageCache("runes", "") {
|
||||
basicauth = ca.getBasicAuth();
|
||||
disableCertCheck = true;
|
||||
// enableDebugging();
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include <ostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <curl/easy.h>
|
||||
#include <Log.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
|||
static const QString BASEURL = "https://ddragon.leagueoflegends.com/";
|
||||
const DataDragon::ChampData DataDragon::EMPTYCHAMP;
|
||||
|
||||
DataDragon::DataDragon(const QString& locale) : RestClient(BASEURL), locale(locale), cache({{"square", ".png"}, {"loading", "_0.jpg"}, {"splash", "_0.jpg"}}) {
|
||||
DataDragon::DataDragon(QObject* parent, const QString& locale) : RestClient(parent, BASEURL), locale(locale), cache({{"square", ".png"}, {"loading", "_0.jpg"}, {"splash", "_0.jpg"}}) {
|
||||
startThread();
|
||||
}
|
||||
|
||||
|
|
|
@ -54,7 +54,7 @@ bool LolAutoAccept::init() {
|
|||
|
||||
auto ca = ClientAccess::find();
|
||||
if(ca) {
|
||||
clientapi = std::make_shared<ClientAPI>(*ca.get());
|
||||
clientapi = std::make_shared<ClientAPI>(this, *ca.get());
|
||||
auto selfinfo = clientapi->getSelf();
|
||||
qInfo() << "selfinfo: gameName: " << selfinfo.gameName << " name: " << selfinfo.name << " summonerid: " << selfinfo.summonerid << " statusMessage: " << selfinfo.statusMessage << " puuid: " << selfinfo.puuid << " level: " << selfinfo.level;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include <Log.h>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(QLocale().name()),
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), loading(true), ui(new Ui::MainWindow), saveTimer(new QTimer(this)), dd(this, QLocale().name()),
|
||||
lolaa(conf.getConfig(), dd) {
|
||||
ui->setupUi(this);
|
||||
|
||||
|
|
|
@ -3,154 +3,79 @@
|
|||
#include <iomanip>
|
||||
#include <Log.h>
|
||||
|
||||
static size_t arrayWriteCallback(char* contents, size_t size, size_t nmemb, void* userdata) {
|
||||
if (userdata) {
|
||||
QByteArray* arr = (QByteArray*) userdata;
|
||||
arr->append(contents, size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QUrl>
|
||||
|
||||
static void dump(const char* text, FILE* stream, unsigned char* ptr, size_t size) {
|
||||
const unsigned int width = 0x40;
|
||||
|
||||
fprintf(stream, "%s, %10.10lu bytes (0x%8.8lx)\n", text, (unsigned long) size, (unsigned long) size);
|
||||
|
||||
for (size_t i = 0; i < size; i += width) {
|
||||
|
||||
fprintf(stream, "%4.4lx: ", (unsigned long) i);
|
||||
|
||||
for (size_t c = 0; (c < width) && (i + c < size); c++) {
|
||||
/* check for 0D0A; if found, skip past and start a new line of output */
|
||||
fprintf(stream, "%c", (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
|
||||
}
|
||||
fputc('\n', stream); /* newline */
|
||||
}
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
static int my_trace(CURL* handle, curl_infotype type, char* data, size_t size, void* userp) {
|
||||
const char* text;
|
||||
(void) handle; /* prevent compiler warning */
|
||||
(void) userp;
|
||||
|
||||
switch (type) {
|
||||
case CURLINFO_TEXT:
|
||||
fprintf(stderr, "== Info: %s", data);
|
||||
/* FALLTHROUGH */
|
||||
default: /* in case a new one is introduced to shock us */
|
||||
return 0;
|
||||
|
||||
case CURLINFO_HEADER_OUT:
|
||||
text = "=> Send header";
|
||||
break;
|
||||
case CURLINFO_DATA_OUT:
|
||||
text = "=> Send data";
|
||||
break;
|
||||
case CURLINFO_HEADER_IN:
|
||||
text = "<= Recv header";
|
||||
break;
|
||||
case CURLINFO_DATA_IN:
|
||||
text = "<= Recv data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_OUT:
|
||||
case CURLINFO_SSL_DATA_IN:
|
||||
return 0;
|
||||
}
|
||||
|
||||
dump(text, stderr, (unsigned char*) data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
RestClient::RestClient(const QString& base) : baseurl(base) {
|
||||
curl = curl_easy_init();
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, arrayWriteCallback);
|
||||
RestClient::RestClient(QObject* parent, const QString& base) : QObject(parent), baseurl(base), net( new QNetworkAccessManager(this) ) {
|
||||
net->setRedirectPolicy(QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy);
|
||||
}
|
||||
|
||||
RestClient::~RestClient() {
|
||||
curl_easy_cleanup(curl);
|
||||
curl = nullptr;
|
||||
}
|
||||
|
||||
RestClient::WebException::WebException(CURLcode c) : curlresponse(c) {
|
||||
RestClient::WebException::WebException(int c) : curlresponse(c) {
|
||||
}
|
||||
|
||||
QByteArray RestClient::requestRaw(const QString& url, Method m, const QString& data) {
|
||||
if (!curl) return {};
|
||||
|
||||
QString requrl = baseurl + url;
|
||||
curl_easy_setopt(curl, CURLOPT_URL, requrl.toLocal8Bit().data());
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_USERPWD, basicauth.toLocal8Bit().data());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||
if (disableCertCheck) {
|
||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
QNetworkRequest req( QUrl{requrl} );
|
||||
|
||||
if(!basicauth.isEmpty()) {
|
||||
req.setRawHeader("Authorization", "Basic " + QByteArray(basicauth.toLocal8Bit()).toBase64());
|
||||
}
|
||||
|
||||
// restore default HTTP Options
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, NULL);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL);
|
||||
if (disableCertCheck) {
|
||||
QSslConfiguration ssl = req.sslConfiguration();
|
||||
ssl.setPeerVerifyMode(QSslSocket::PeerVerifyMode::VerifyNone);
|
||||
req.setSslConfiguration(ssl);
|
||||
}
|
||||
|
||||
// curl header
|
||||
struct curl_slist* headerlist = NULL;
|
||||
|
||||
headerlist = curl_slist_append(headerlist, "Accept: application/json");
|
||||
req.setRawHeader("Accept", "application/json");
|
||||
|
||||
QNetworkReply* repl = nullptr;
|
||||
switch (m) {
|
||||
default:
|
||||
case Method::GET:
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); break;
|
||||
repl = net->get(req); break;
|
||||
case Method::POST:
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data());
|
||||
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
|
||||
req.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||
repl = net->post(req, data.toLocal8Bit());
|
||||
break;
|
||||
case Method::PUT:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT"); // to use the POSTFIELDS (do not use CURLOPT_PUT, it does not support POSTFIELDS)
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data());
|
||||
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
|
||||
req.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||
repl = net->put(req, data.toLocal8Bit());
|
||||
break;
|
||||
case Method::PATCH:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data());
|
||||
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
|
||||
req.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||
repl = net->sendCustomRequest(req, "PATCH", data.toLocal8Bit());
|
||||
break;
|
||||
case Method::DELETE:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.toLocal8Bit().data());
|
||||
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
|
||||
req.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||
repl = net->sendCustomRequest(req, "DELETE", data.toLocal8Bit());
|
||||
break;
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
|
||||
|
||||
QByteArray ba; //buffer
|
||||
// set callback data
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ba);
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (headerlist) {
|
||||
curl_slist_free_all(headerlist);
|
||||
if(!repl->waitForReadyRead(5000)) {
|
||||
qCritical() << "request timed out";
|
||||
return {};
|
||||
}
|
||||
|
||||
QByteArray arr = repl->readAll();
|
||||
|
||||
unsigned int res = repl->attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toUInt();
|
||||
|
||||
// Check for errors
|
||||
if (res != CURLE_OK) {
|
||||
if (res == CURLE_HTTP_RETURNED_ERROR) {
|
||||
long responsecode = -1;
|
||||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responsecode);
|
||||
qWarning() << "API request failed: " << baseurl << " " << url << " -> " << responsecode;
|
||||
} else {
|
||||
qWarning() << "API request failed: " << baseurl << " " << url << " " << curl_easy_strerror(res);
|
||||
if(res == CURLE_COULDNT_CONNECT) {
|
||||
throw WebException(res);
|
||||
}
|
||||
if (res != 200) {
|
||||
qWarning() << "API request failed: " << baseurl << " " << url << " -> " << res;
|
||||
if(res == 0) {
|
||||
throw WebException(res);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
return ba;
|
||||
return arr;
|
||||
}
|
||||
|
||||
QJsonDocument RestClient::request(const QString& url, Method m, const QString& data) {
|
||||
|
@ -168,19 +93,9 @@ QJsonDocument RestClient::request(const QString& url, Method m, const QString& d
|
|||
}
|
||||
|
||||
void RestClient::enableDebugging(bool enabled) {
|
||||
// enable debugging
|
||||
if(enabled) {
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
} else {
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, NULL);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
|
||||
}
|
||||
debugging = enabled;
|
||||
}
|
||||
|
||||
QString RestClient::escape(const QString& in) const {
|
||||
char* e = curl_easy_escape(curl, in.toLocal8Bit().data(), in.length());
|
||||
QString esc(e);
|
||||
curl_free(e);
|
||||
return esc;
|
||||
return QUrl::toPercentEncoding(in);
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ void RuneManager::loadRunes() {
|
|||
auto ca = ClientAccess::find();
|
||||
|
||||
if(ca) {
|
||||
client = std::make_shared<ClientAPI>(*ca.get());
|
||||
client = std::make_shared<ClientAPI>(this, *ca.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue