add new impl client
This commit is contained in:
parent
cf5e79f6bb
commit
af5fffc9d0
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ subprojects/*
|
|||||||
!subprojects/jwt-cpp.wrap
|
!subprojects/jwt-cpp.wrap
|
||||||
!subprojects/nlohmann_json.wrap
|
!subprojects/nlohmann_json.wrap
|
||||||
!subprojects/pgxx.wrap
|
!subprojects/pgxx.wrap
|
||||||
|
!subprojects/cpr.wrap
|
||||||
|
@ -32,9 +32,11 @@ inc = []
|
|||||||
deps = [
|
deps = [
|
||||||
dependency('uuid'),
|
dependency('uuid'),
|
||||||
dependency('libpqxx'),
|
dependency('libpqxx'),
|
||||||
|
declare_dependency(link_args: ['-lcurl']),
|
||||||
subproject('hack').get_variable('hack_dep'),
|
subproject('hack').get_variable('hack_dep'),
|
||||||
subproject('pgxx').get_variable('pgxx_dep'),
|
subproject('pgxx').get_variable('pgxx_dep'),
|
||||||
subproject('nlohmann_json').get_variable('nlohmann_json_dep'),
|
subproject('nlohmann_json').get_variable('nlohmann_json_dep'),
|
||||||
|
subproject('cpr').get_variable('cpr_dep'),
|
||||||
subproject('jwt-cpp').get_variable('jwt_dep'),
|
subproject('jwt-cpp').get_variable('jwt_dep'),
|
||||||
subproject('httplib').get_variable('cpp_httplib_dep'),
|
subproject('httplib').get_variable('cpp_httplib_dep'),
|
||||||
]
|
]
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
|
#include "cpr/cpr.h"
|
||||||
#include "hack/logger/logger.hpp"
|
#include "hack/logger/logger.hpp"
|
||||||
#include "hack/exception/exception.hpp"
|
#include "hack/exception/exception.hpp"
|
||||||
#include "pgxx/pgxx.hpp"
|
#include "pgxx/pgxx.hpp"
|
||||||
@ -125,39 +127,42 @@ namespace trs
|
|||||||
class client
|
class client
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
client() = delete;
|
client() = default;
|
||||||
client(std::string url) : m_cli { url } {}
|
client(std::string url) : m_url { url } {}
|
||||||
client(client&& c) : m_cli{ std::move(c.m_cli) }, m_token { std::move(c.m_token) } {}
|
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;
|
~client() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void set_token(std::string t) { m_token = t; }
|
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)
|
auto post(std::string func_name, JSON&& data)
|
||||||
{
|
{
|
||||||
httplib::Headers headers = {
|
auto r = cpr::Post(cpr::Url { m_url },
|
||||||
{ var::HEADER_TOKEN, m_token },
|
cpr::Body { data.dump() },
|
||||||
{ var::HEADER_FUNCTION, func_name },
|
cpr::Header {
|
||||||
{ "Content-Type", var::HEADER_FLAG_JSON },
|
{ "Content-Type", var::HEADER_FLAG_JSON },
|
||||||
{ "Cookie", "unione_lang=ru" }
|
{ var::HEADER_TOKEN, m_token },
|
||||||
};
|
{ var::HEADER_FUNCTION, func_name }
|
||||||
|
});
|
||||||
return m_cli.Post("/", headers, data.dump(), var::HEADER_FLAG_JSON );
|
return r;
|
||||||
}
|
|
||||||
|
|
||||||
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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
httplib::Client m_cli;
|
std::string m_url;
|
||||||
|
// httplib::Client m_cli;
|
||||||
std::string m_token;
|
std::string m_token;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
13
subprojects/cpr.wrap
Normal file
13
subprojects/cpr.wrap
Normal file
@ -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
|
@ -42,11 +42,11 @@ auto main(int argc, char* args[]) -> int
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trs::client cli {"localhost:5000"};
|
||||||
// trs::client cli {"localhost:5000"};
|
cli.set_token("token");
|
||||||
// cli.set_token("token");
|
trs::client cli_2 { std::move(cli) };
|
||||||
// trs::client cli_2 { std::move(cli) };
|
auto r = cli_2.post("func_name", trs::JSON{{ "key", "test message" }});
|
||||||
// cli_2.post("func_name",trs::JSON{{ "key", "test message" }});
|
hack::log()(r.text, r.status_code);
|
||||||
|
|
||||||
trs::server srv;
|
trs::server srv;
|
||||||
srv.init("test service");
|
srv.init("test service");
|
||||||
|
Loading…
Reference in New Issue
Block a user