From b7ef4ddf408bc469742bc1ad3fcafa4f3c0c6b51 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 7 Jan 2020 21:53:27 +0100 Subject: [PATCH] First request to the API --- ComunicTerm.pro | 11 ++++++++++- api_request.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ api_request.h | 24 ++++++++++++++++++++++++ apiresponse.cpp | 29 +++++++++++++++++++++++++++++ apiresponse.h | 27 +++++++++++++++++++++++++++ config.h | 11 +++++++++++ main.cpp | 9 +++++++++ 7 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 api_request.cpp create mode 100644 api_request.h create mode 100644 apiresponse.cpp create mode 100644 apiresponse.h create mode 100644 config.h diff --git a/ComunicTerm.pro b/ComunicTerm.pro index 7f95685..9dc0493 100644 --- a/ComunicTerm.pro +++ b/ComunicTerm.pro @@ -3,5 +3,14 @@ CONFIG += console c++11 CONFIG -= app_bundle CONFIG -= qt +LIBS += -lboost_system -lcrypto -lssl -lcpprest + SOURCES += \ - main.cpp + main.cpp \ + api_request.cpp \ + apiresponse.cpp + +HEADERS += \ + config.h \ + api_request.h \ + apiresponse.h diff --git a/api_request.cpp b/api_request.cpp new file mode 100644 index 0000000..fa54221 --- /dev/null +++ b/api_request.cpp @@ -0,0 +1,47 @@ +#include +#include + +#include "api_request.h" +#include "config.h" + +using namespace std; +using namespace web; +using namespace web::http; +using namespace web::json; + +ApiRequest::ApiRequest(const std::string &uri, bool needLogin) + : mURI(uri) +{ + this->addArg("serviceName", API_CLIENT_NAME); + this->addArg("serviceToken", API_CLIENT_TOKEN); +} + +void ApiRequest::addArg(const std::string &name, const std::string &value) +{ + if(mReqBody.length() > 0) + mReqBody += "&"; + + mReqBody += uri::encode_data_string(name) + "=" + uri::encode_data_string(value); +} + +ApiResponse ApiRequest::exec() +{ + try { + http_request req(methods::POST); + req.headers().add("Content-Type", "application/x-www-form-urlencoded"); + req.set_body(this->mReqBody); + + + web::http::client::http_client c(API_URL + this->mURI); + auto task = c.request(req); + + task.wait(); + auto res = task.get(); + + return ApiResponse(res.status_code(), res.extract_json().get()); + + } catch(exception &e) { + cerr << "Error on request!" << e.what() << endl; + return ApiResponse(-1, web::json::value(e.what())); + } +} diff --git a/api_request.h b/api_request.h new file mode 100644 index 0000000..38d0ff7 --- /dev/null +++ b/api_request.h @@ -0,0 +1,24 @@ +/** + * Single API request + * + * @author Pierre HUBERT + */ +#pragma once + +#include + +#include "apiresponse.h" + +class ApiRequest +{ +public: + ApiRequest(const std::string &uri, bool needLogin = true); + + void addArg(const std::string &name, const std::string &value); + + ApiResponse exec(); + +private: + std::string mURI; + std::string mReqBody; +}; diff --git a/apiresponse.cpp b/apiresponse.cpp new file mode 100644 index 0000000..ca4f6c2 --- /dev/null +++ b/apiresponse.cpp @@ -0,0 +1,29 @@ +#include "apiresponse.h" + +using namespace web; + +ApiResponse::ApiResponse(int code, json::value content) + : mCode(code), mContent(content) +{ + +} + +json::value ApiResponse::content() const +{ + return mContent; +} + +int ApiResponse::code() const +{ + return mCode; +} + +json::object ApiResponse::object() const +{ + return mContent.as_object(); +} + +json::array ApiResponse::array() const +{ + return mContent.as_array(); +} diff --git a/apiresponse.h b/apiresponse.h new file mode 100644 index 0000000..0c2fad8 --- /dev/null +++ b/apiresponse.h @@ -0,0 +1,27 @@ +/** + * API Response + * + * @author Pierre HUBERT + */ + +#pragma once + +#include + +class ApiResponse +{ +public: + ApiResponse(int code, web::json::value content); + + web::json::value content() const; + + int code() const; + + web::json::object object() const; + + web::json::array array() const; + +private: + int mCode; + web::json::value mContent; +}; diff --git a/config.h b/config.h new file mode 100644 index 0000000..634b8c5 --- /dev/null +++ b/config.h @@ -0,0 +1,11 @@ +/** + * Project configuration + * + * @author Pierre HUBERT + */ + +#pragma once + +#define API_URL "https://api.communiquons.org/" +#define API_CLIENT_NAME "ComunicFlutter" +#define API_CLIENT_TOKEN "9KfSwmB76U9UUwjXngDG7PeYccNfy" diff --git a/main.cpp b/main.cpp index d709c12..bb8d080 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,18 @@ #include +#include "api_request.h" using namespace std; int main() { cout << "Comunic Term (c) Pierre HUBERT" << endl; + + auto req = ApiRequest("user/getInfosMultiple"); + req.addArg("usersID", "1"); + auto res = req.exec(); + cout << "code: " << res.code() << endl; + cout << "content" << res.content().serialize() << endl; + cout << "First name found: " << res.object().at("1").as_object().at(U("firstName")).as_string() << endl; + return 0; }