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<>
|
||||
int convert(const QJsonValue& val);
|
||||
|
||||
template<>
|
||||
int64_t convert(const QJsonValue& val);
|
||||
|
||||
template<>
|
||||
std::string convert(const QJsonValue& val);
|
||||
|
||||
|
|
|
@ -9,9 +9,16 @@ public:
|
|||
RestClient(const std::string& base);
|
||||
virtual ~RestClient();
|
||||
|
||||
enum class Method {
|
||||
GET,
|
||||
POST,
|
||||
PUT,
|
||||
DELETE
|
||||
};
|
||||
|
||||
protected:
|
||||
QByteArray requestRaw(const std::string& url);
|
||||
QJsonDocument request(const std::string& url);
|
||||
QByteArray requestRaw(const std::string& url, Method m = Method::GET);
|
||||
QJsonDocument request(const std::string& url, Method m = Method::GET);
|
||||
|
||||
std::string baseurl;
|
||||
|
||||
|
|
|
@ -7,6 +7,13 @@ int convert(const QJsonValue& val) {
|
|||
return val.toInt();
|
||||
}
|
||||
|
||||
template<>
|
||||
int64_t convert(const QJsonValue& val) {
|
||||
if(val.isString())
|
||||
return val.toString().toLongLong();
|
||||
return (int64_t) val.toDouble();
|
||||
}
|
||||
|
||||
template<>
|
||||
std::string convert(const QJsonValue& val) {
|
||||
return val.toString().toStdString();
|
||||
|
|
|
@ -22,7 +22,7 @@ RestClient::~RestClient() {
|
|||
curl = nullptr;
|
||||
}
|
||||
|
||||
QByteArray RestClient::requestRaw(const std::string& url) {
|
||||
QByteArray RestClient::requestRaw(const std::string& url, Method m) {
|
||||
if(!curl) return {};
|
||||
|
||||
std::string requrl = baseurl + url;
|
||||
|
@ -40,6 +40,18 @@ QByteArray RestClient::requestRaw(const std::string& url) {
|
|||
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
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
|
@ -55,8 +67,8 @@ QByteArray RestClient::requestRaw(const std::string& url) {
|
|||
return ba;
|
||||
}
|
||||
|
||||
QJsonDocument RestClient::request(const std::string& url) {
|
||||
QByteArray arr = requestRaw(url);
|
||||
QJsonDocument RestClient::request(const std::string& url, Method m) {
|
||||
QByteArray arr = requestRaw(url, m);
|
||||
if(arr.isEmpty()) return {};
|
||||
|
||||
QJsonParseError err;
|
||||
|
|
Loading…
Reference in New Issue