diff --git a/include/json.h b/include/json.h index aec566a..338981e 100644 --- a/include/json.h +++ b/include/json.h @@ -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); diff --git a/include/restclient.h b/include/restclient.h index 915c434..eb323b7 100644 --- a/include/restclient.h +++ b/include/restclient.h @@ -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; diff --git a/src/json.cpp b/src/json.cpp index a1fd50b..0f9ee18 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -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(); diff --git a/src/restclient.cpp b/src/restclient.cpp index ff02e57..fccc163 100644 --- a/src/restclient.cpp +++ b/src/restclient.cpp @@ -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;