From af5fffc9d01d89ea37602e8de92a16b9de900c33 Mon Sep 17 00:00:00 2001 From: chatlanin Date: Sun, 21 Jul 2024 11:51:07 +0300 Subject: [PATCH] add new impl client --- .gitignore | 1 + meson.build | 2 ++ src/trs/trs.hpp | 53 ++++++++++++++++++++++++-------------------- subprojects/cpr.wrap | 13 +++++++++++ tests/main.cpp | 10 ++++----- 5 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 subprojects/cpr.wrap diff --git a/.gitignore b/.gitignore index b71c06c..554e040 100755 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ subprojects/* !subprojects/jwt-cpp.wrap !subprojects/nlohmann_json.wrap !subprojects/pgxx.wrap +!subprojects/cpr.wrap diff --git a/meson.build b/meson.build index 4471fca..070a7b7 100755 --- a/meson.build +++ b/meson.build @@ -32,9 +32,11 @@ inc = [] deps = [ dependency('uuid'), dependency('libpqxx'), + declare_dependency(link_args: ['-lcurl']), subproject('hack').get_variable('hack_dep'), subproject('pgxx').get_variable('pgxx_dep'), subproject('nlohmann_json').get_variable('nlohmann_json_dep'), + subproject('cpr').get_variable('cpr_dep'), subproject('jwt-cpp').get_variable('jwt_dep'), subproject('httplib').get_variable('cpp_httplib_dep'), ] diff --git a/src/trs/trs.hpp b/src/trs/trs.hpp index 7b6d4c6..8a06e0b 100644 --- a/src/trs/trs.hpp +++ b/src/trs/trs.hpp @@ -2,8 +2,10 @@ #include #include +#include #include "httplib.h" +#include "cpr/cpr.h" #include "hack/logger/logger.hpp" #include "hack/exception/exception.hpp" #include "pgxx/pgxx.hpp" @@ -125,39 +127,42 @@ namespace trs class client { public: - client() = delete; - client(std::string url) : m_cli { url } {} - client(client&& c) : m_cli{ std::move(c.m_cli) }, m_token { std::move(c.m_token) } {} + client() = default; + client(std::string url) : m_url { url } {} + client(client&& c) : m_url { std::move(c.m_url) }, m_token { std::move(c.m_token) } {} + // client(std::string url) : m_url { url }, m_cli { url } {} + // client(client&& c) : m_cli{ std::move(c.m_cli) }, m_token { std::move(c.m_token) } {} ~client() = default; public: void set_token(std::string t) { m_token = t; } + // auto post(std::string func_name, JSON&& data) + // { + // httplib::Headers headers = { + // { var::HEADER_TOKEN, m_token }, + // { var::HEADER_FUNCTION, func_name }, + // { "Content-Type", var::HEADER_FLAG_JSON }, + // { "Cookie", "unione_lang=ru" } + // }; + // + // return m_cli.Post("/", headers, data.dump(), var::HEADER_FLAG_JSON ); + // } + auto post(std::string func_name, JSON&& data) { - httplib::Headers headers = { - { var::HEADER_TOKEN, m_token }, - { var::HEADER_FUNCTION, func_name }, - { "Content-Type", var::HEADER_FLAG_JSON }, - { "Cookie", "unione_lang=ru" } - }; - - return m_cli.Post("/", headers, data.dump(), var::HEADER_FLAG_JSON ); - } - - auto post(std::string func_name, JSON& data) - { - httplib::Headers headers = { - { var::HEADER_TOKEN, m_token }, - { var::HEADER_FUNCTION, func_name }, - { "Content-Type", var::HEADER_FLAG_JSON }, - { "Cookie", "unione_lang=ru" } - }; - - return m_cli.Post("/", headers, data.dump(), var::HEADER_FLAG_JSON ); + auto r = cpr::Post(cpr::Url { m_url }, + cpr::Body { data.dump() }, + cpr::Header { + { "Content-Type", var::HEADER_FLAG_JSON }, + { var::HEADER_TOKEN, m_token }, + { var::HEADER_FUNCTION, func_name } + }); + return r; } private: - httplib::Client m_cli; + std::string m_url; + // httplib::Client m_cli; std::string m_token; }; } diff --git a/subprojects/cpr.wrap b/subprojects/cpr.wrap new file mode 100644 index 0000000..601df20 --- /dev/null +++ b/subprojects/cpr.wrap @@ -0,0 +1,13 @@ +[wrap-file] +directory = cpr-1.10.5 +source_url = https://github.com/libcpr/cpr/archive/1.10.5.tar.gz +source_filename = cpr-1.10.5.tar.gz +source_hash = c8590568996cea918d7cf7ec6845d954b9b95ab2c4980b365f582a665dea08d8 +patch_filename = cpr_1.10.5-1_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/cpr_1.10.5-1/get_patch +patch_hash = 01e31e1bd47bc55dabc8e3b0633451abefc37532ba57b3ba3f0a304592ea5ac5 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/cpr_1.10.5-1/cpr-1.10.5.tar.gz +wrapdb_version = 1.10.5-1 + +[provide] +cpr = cpr_dep diff --git a/tests/main.cpp b/tests/main.cpp index 0683106..8c2399c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -42,11 +42,11 @@ auto main(int argc, char* args[]) -> int throw; } - - // trs::client cli {"localhost:5000"}; - // cli.set_token("token"); - // trs::client cli_2 { std::move(cli) }; - // cli_2.post("func_name",trs::JSON{{ "key", "test message" }}); + trs::client cli {"localhost:5000"}; + cli.set_token("token"); + trs::client cli_2 { std::move(cli) }; + auto r = cli_2.post("func_name", trs::JSON{{ "key", "test message" }}); + hack::log()(r.text, r.status_code); trs::server srv; srv.init("test service");