common_http_client

This commit is contained in:
Xuan Son Nguyen
2026-07-28 23:51:35 +02:00
parent e9fa0781f1
commit 72d0d8de4e
6 changed files with 44 additions and 26 deletions
+8 -8
View File
@@ -27,9 +27,9 @@ static std::string join_path(const common_http_url & parts, const std::string &
std::string cli_client::get(const std::string & path) {
auto [cli, parts] = common_http_client(server_base);
cli.set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
cli->set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
auto path_with_model = path + (model.empty() ? "" : ("?model=" + model));
auto res = cli.Get(join_path(parts, path_with_model));
auto res = cli->Get(join_path(parts, path_with_model));
if (!res) {
throw std::runtime_error("failed to connect to " + server_base + ": " + httplib::to_string(res.error()));
}
@@ -41,8 +41,8 @@ std::string cli_client::get(const std::string & path) {
std::string cli_client::post(const std::string & path, const std::string & body) {
auto [cli, parts] = common_http_client(server_base);
cli.set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
auto res = cli.Post(join_path(parts, path), body, "application/json");
cli->set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
auto res = cli->Post(join_path(parts, path), body, "application/json");
if (!res) {
throw std::runtime_error("failed to connect to " + server_base + ": " + httplib::to_string(res.error()));
}
@@ -57,7 +57,7 @@ std::string cli_client::post_sse(const std::string & path,
const std::function<bool()> & should_stop,
const std::function<void(const std::string &)> & on_data) {
auto [cli, parts] = common_http_client(server_base);
cli.set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
cli->set_read_timeout(CLI_HTTP_READ_TIMEOUT_SEC, 0);
std::string pending; // buffer for incomplete SSE lines
std::string raw_body; // accumulated body, used only for error reporting
@@ -90,7 +90,7 @@ std::string cli_client::post_sse(const std::string & path,
};
httplib::Headers headers = {{"Accept", "text/event-stream"}};
auto res = cli.Post(join_path(parts, path), headers, body, "application/json", receiver);
auto res = cli->Post(join_path(parts, path), headers, body, "application/json", receiver);
if (!res) {
if (res.error() == httplib::Error::Canceled && should_stop()) {
@@ -111,8 +111,8 @@ bool cli_client::wait_health(const std::function<bool()> & is_aborted) {
int connect_attempts = 0;
while (!is_aborted()) {
auto [cli, parts] = common_http_client(server_base);
cli.set_connection_timeout(1, 0);
auto res = cli.Get(join_path(parts, "/health"));
cli->set_connection_timeout(1, 0);
auto res = cli->Get(join_path(parts, "/health"));
if (res) {
if (res->status == 200) {
return true;