This commit is contained in:
2025-01-24 13:29:38 -06:00
parent bfe7b8915d
commit ebee5ec857
2 changed files with 155 additions and 80 deletions

View File

@ -31,18 +31,18 @@ public:
private: private:
const rpc_host_info host_info_; const rpc_host_info host_info_;
std::atomic<std::uint32_t> request_id_; std::atomic<std::uint32_t> request_id_{0U};
public: public:
[[nodiscard]] auto get_drive_information() -> rpc_response; [[nodiscard]] auto get_drive_information() -> rpc_response;
[[nodiscard]] auto get_config() -> rpc_response; [[nodiscard]] auto get_config() -> rpc_response;
[[nodiscard]] auto [[nodiscard]] auto get_config_value_by_name(const std::string &name)
get_config_value_by_name(const std::string &name) -> rpc_response; -> rpc_response;
[[nodiscard]] auto [[nodiscard]] auto get_directory_items(const std::string &api_path)
get_directory_items(const std::string &api_path) -> rpc_response; -> rpc_response;
[[nodiscard]] auto get_open_files() -> rpc_response; [[nodiscard]] auto get_open_files() -> rpc_response;
@ -52,9 +52,9 @@ public:
[[nodiscard]] auto pinned_status(const std::string &api_file) -> rpc_response; [[nodiscard]] auto pinned_status(const std::string &api_file) -> rpc_response;
[[nodiscard]] auto [[nodiscard]] auto set_config_value_by_name(const std::string &name,
set_config_value_by_name(const std::string &name, const std::string &value)
const std::string &value) -> rpc_response; -> rpc_response;
[[nodiscard]] auto unmount() -> rpc_response; [[nodiscard]] auto unmount() -> rpc_response;

View File

@ -26,9 +26,7 @@
#include "utils/utils.hpp" #include "utils/utils.hpp"
namespace repertory { namespace repertory {
client::client(rpc_host_info host_info) : host_info_(std::move(host_info)) { client::client(rpc_host_info host_info) : host_info_(std::move(host_info)) {}
request_id_ = 0u;
}
auto client::get_drive_information() -> rpc_response { auto client::get_drive_information() -> rpc_response {
const auto base_url = const auto base_url =
@ -39,15 +37,22 @@ auto client::get_drive_information() -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::get_drive_information); auto resp = cli.Get("/api/v1/" + rpc_method::get_drive_information);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::get_config() -> rpc_response { auto client::get_config() -> rpc_response {
@ -59,15 +64,22 @@ auto client::get_config() -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::get_config); auto resp = cli.Get("/api/v1/" + rpc_method::get_config);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::get_config_value_by_name(const std::string &name) -> rpc_response { auto client::get_config_value_by_name(const std::string &name) -> rpc_response {
@ -81,15 +93,22 @@ auto client::get_config_value_by_name(const std::string &name) -> rpc_response {
auto resp = auto resp =
cli.Get("/api/v1/" + rpc_method::get_config_value_by_name, params, {}); cli.Get("/api/v1/" + rpc_method::get_config_value_by_name, params, {});
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::get_directory_items(const std::string &api_path) -> rpc_response { auto client::get_directory_items(const std::string &api_path) -> rpc_response {
@ -102,15 +121,22 @@ auto client::get_directory_items(const std::string &api_path) -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::get_directory_items, params, {}); auto resp = cli.Get("/api/v1/" + rpc_method::get_directory_items, params, {});
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::get_open_files() -> rpc_response { auto client::get_open_files() -> rpc_response {
@ -122,15 +148,22 @@ auto client::get_open_files() -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::get_open_files); auto resp = cli.Get("/api/v1/" + rpc_method::get_open_files);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::get_pinned_files() -> rpc_response { auto client::get_pinned_files() -> rpc_response {
@ -142,15 +175,22 @@ auto client::get_pinned_files() -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::get_pinned_files); auto resp = cli.Get("/api/v1/" + rpc_method::get_pinned_files);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::pin_file(const std::string &api_path) -> rpc_response { auto client::pin_file(const std::string &api_path) -> rpc_response {
@ -163,15 +203,22 @@ auto client::pin_file(const std::string &api_path) -> rpc_response {
auto resp = cli.Post("/api/v1/" + rpc_method::pin_file, params); auto resp = cli.Post("/api/v1/" + rpc_method::pin_file, params);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, {}}; return rpc_response{
rpc_response_type::success,
{},
};
} }
auto client::pinned_status(const std::string &api_path) -> rpc_response { auto client::pinned_status(const std::string &api_path) -> rpc_response {
@ -184,19 +231,27 @@ auto client::pinned_status(const std::string &api_path) -> rpc_response {
auto resp = cli.Get("/api/v1/" + rpc_method::pinned_status, params, {}); auto resp = cli.Get("/api/v1/" + rpc_method::pinned_status, params, {});
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, json::parse(resp->body)}; return rpc_response{
rpc_response_type::success,
json::parse(resp->body),
};
} }
auto client::set_config_value_by_name( auto client::set_config_value_by_name(const std::string &name,
const std::string &name, const std::string &value) -> rpc_response { const std::string &value)
-> rpc_response {
const auto base_url = const auto base_url =
"http://" + host_info_.host + ":" + std::to_string(host_info_.port); "http://" + host_info_.host + ":" + std::to_string(host_info_.port);
@ -211,16 +266,22 @@ auto client::set_config_value_by_name(
auto resp = auto resp =
cli.Post("/api/v1/" + rpc_method::set_config_value_by_name, params); cli.Post("/api/v1/" + rpc_method::set_config_value_by_name, params);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
}; };
return rpc_response{rpc_response_type::success, return rpc_response{
nlohmann::json::parse(resp->body)}; rpc_response_type::success,
nlohmann::json::parse(resp->body),
};
} }
auto client::unmount() -> rpc_response { auto client::unmount() -> rpc_response {
@ -232,15 +293,22 @@ auto client::unmount() -> rpc_response {
auto resp = cli.Post("/api/v1/" + rpc_method::unmount); auto resp = cli.Post("/api/v1/" + rpc_method::unmount);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, {}}; return rpc_response{
rpc_response_type::success,
{},
};
} }
auto client::unpin_file(const std::string &api_path) -> rpc_response { auto client::unpin_file(const std::string &api_path) -> rpc_response {
@ -253,14 +321,21 @@ auto client::unpin_file(const std::string &api_path) -> rpc_response {
auto resp = cli.Post("/api/v1/" + rpc_method::unpin_file, params); auto resp = cli.Post("/api/v1/" + rpc_method::unpin_file, params);
if (resp.error() != httplib::Error::Success) { if (resp.error() != httplib::Error::Success) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", httplib::to_string(resp.error())}}}; rpc_response_type::http_error,
{{"error", httplib::to_string(resp.error())}},
};
} }
if (resp->status != 200) { if (resp->status != http_error_codes::ok) {
return rpc_response{rpc_response_type::http_error, return rpc_response{
{{"error", std::to_string(resp->status)}}}; rpc_response_type::http_error,
{{"error", std::to_string(resp->status)}},
};
} }
return rpc_response{rpc_response_type::success, {}}; return rpc_response{
rpc_response_type::success,
{},
};
} }
} // namespace repertory } // namespace repertory