forked from MrBesen/lolautoaccept
restclient improved
This commit is contained in:
parent
00f6cc4a7d
commit
07ee1df44e
|
@ -12,6 +12,9 @@ T convert(const QJsonValue& val) {
|
||||||
template<>
|
template<>
|
||||||
int convert(const QJsonValue& val);
|
int convert(const QJsonValue& val);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
int64_t convert(const QJsonValue& val);
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
std::string convert(const QJsonValue& val);
|
std::string convert(const QJsonValue& val);
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,16 @@ public:
|
||||||
RestClient(const std::string& base);
|
RestClient(const std::string& base);
|
||||||
virtual ~RestClient();
|
virtual ~RestClient();
|
||||||
|
|
||||||
|
enum class Method {
|
||||||
|
GET,
|
||||||
|
POST,
|
||||||
|
PUT,
|
||||||
|
DELETE
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
QByteArray requestRaw(const std::string& url);
|
QByteArray requestRaw(const std::string& url, Method m = Method::GET);
|
||||||
QJsonDocument request(const std::string& url);
|
QJsonDocument request(const std::string& url, Method m = Method::GET);
|
||||||
|
|
||||||
std::string baseurl;
|
std::string baseurl;
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,13 @@ int convert(const QJsonValue& val) {
|
||||||
return val.toInt();
|
return val.toInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
int64_t convert(const QJsonValue& val) {
|
||||||
|
if(val.isString())
|
||||||
|
return val.toString().toLongLong();
|
||||||
|
return (int64_t) val.toDouble();
|
||||||
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
std::string convert(const QJsonValue& val) {
|
std::string convert(const QJsonValue& val) {
|
||||||
return val.toString().toStdString();
|
return val.toString().toStdString();
|
||||||
|
|
|
@ -22,7 +22,7 @@ RestClient::~RestClient() {
|
||||||
curl = nullptr;
|
curl = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray RestClient::requestRaw(const std::string& url) {
|
QByteArray RestClient::requestRaw(const std::string& url, Method m) {
|
||||||
if(!curl) return {};
|
if(!curl) return {};
|
||||||
|
|
||||||
std::string requrl = baseurl + url;
|
std::string requrl = baseurl + url;
|
||||||
|
@ -40,6 +40,18 @@ QByteArray RestClient::requestRaw(const std::string& url) {
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (m) {
|
||||||
|
default:
|
||||||
|
case Method::GET:
|
||||||
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); break;
|
||||||
|
case Method::POST:
|
||||||
|
curl_easy_setopt(curl, CURLOPT_POST, 1L); break;
|
||||||
|
case Method::PUT:
|
||||||
|
curl_easy_setopt(curl, CURLOPT_PUT, 1L); break;
|
||||||
|
case Method::DELETE:
|
||||||
|
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); break;
|
||||||
|
}
|
||||||
|
|
||||||
// Check for errors
|
// Check for errors
|
||||||
CURLcode res = curl_easy_perform(curl);
|
CURLcode res = curl_easy_perform(curl);
|
||||||
if (res != CURLE_OK) {
|
if (res != CURLE_OK) {
|
||||||
|
@ -55,8 +67,8 @@ QByteArray RestClient::requestRaw(const std::string& url) {
|
||||||
return ba;
|
return ba;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonDocument RestClient::request(const std::string& url) {
|
QJsonDocument RestClient::request(const std::string& url, Method m) {
|
||||||
QByteArray arr = requestRaw(url);
|
QByteArray arr = requestRaw(url, m);
|
||||||
if(arr.isEmpty()) return {};
|
if(arr.isEmpty()) return {};
|
||||||
|
|
||||||
QJsonParseError err;
|
QJsonParseError err;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user