mirror of
https://gitlab.com/comunic/comunicterm
synced 2024-12-25 04:58:56 +00:00
First request to the API
This commit is contained in:
parent
458f12d0d1
commit
b7ef4ddf40
@ -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
|
||||
|
47
api_request.cpp
Normal file
47
api_request.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include <cpprest/http_client.h>
|
||||
#include <cpprest/json.h>
|
||||
|
||||
#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()));
|
||||
}
|
||||
}
|
24
api_request.h
Normal file
24
api_request.h
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Single API request
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#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;
|
||||
};
|
29
apiresponse.cpp
Normal file
29
apiresponse.cpp
Normal file
@ -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();
|
||||
}
|
27
apiresponse.h
Normal file
27
apiresponse.h
Normal file
@ -0,0 +1,27 @@
|
||||
/**
|
||||
* API Response
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cpprest/json.h>
|
||||
|
||||
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;
|
||||
};
|
11
config.h
Normal file
11
config.h
Normal file
@ -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"
|
9
main.cpp
9
main.cpp
@ -1,9 +1,18 @@
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user