2026-10-24 04:05:41 +02:00
|
|
|
#include "google/client.h"
|
|
|
|
|
|
|
|
|
|
#include "clients/transports/http_transport.h"
|
|
|
|
|
#include "google/credentials.h"
|
2026-10-24 04:12:52 +02:00
|
|
|
#include "google/stream_decoder.h"
|
2026-10-24 04:05:41 +02:00
|
|
|
|
|
|
|
|
namespace molo::google {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
clients::ClientConfig make_config(std::shared_ptr<clients::Auth> auth) {
|
|
|
|
|
clients::ClientConfig config;
|
|
|
|
|
config.auth = std::move(auth);
|
|
|
|
|
config.persist_cookies = false;
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
|
|
|
|
std::shared_ptr<clients::Auth> auth)
|
|
|
|
|
: client_(std::move(transport), make_config(auth)),
|
|
|
|
|
configured_(auth != nullptr) {}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<GoogleClient> GoogleClient::from_env() {
|
|
|
|
|
return std::make_shared<GoogleClient>(
|
|
|
|
|
std::make_unique<clients::transports::HttpTransport>(),
|
|
|
|
|
Credentials::from_env());
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 04:12:52 +02:00
|
|
|
clients::body::Json GoogleClient::request(clients::Method method,
|
|
|
|
|
const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path,
|
|
|
|
|
const clients::body::Json* body) {
|
2026-10-24 04:05:41 +02:00
|
|
|
if (!configured_) throw NotConfiguredError(service.name);
|
|
|
|
|
try {
|
2026-10-24 04:12:52 +02:00
|
|
|
clients::ClientRequest req(method,
|
|
|
|
|
clients::Url::parse(service.base_url + path));
|
|
|
|
|
if (body) req.json(*body);
|
|
|
|
|
return finish(service, client_.send_blocking(std::move(req)));
|
2026-10-24 04:05:41 +02:00
|
|
|
} catch (const GoogleError&) {
|
|
|
|
|
throw;
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
throw GoogleError(502, "UNAVAILABLE", e.what(), service.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 04:12:52 +02:00
|
|
|
clients::body::Json GoogleClient::post_json(const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path,
|
|
|
|
|
const clients::body::Json& body) {
|
|
|
|
|
return request(clients::Method::kPost, service, path, &body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clients::body::Json GoogleClient::patch_json(const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path,
|
|
|
|
|
const clients::body::Json& body) {
|
|
|
|
|
return request(clients::Method::kPatch, service, path, &body);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 04:05:41 +02:00
|
|
|
clients::body::Json GoogleClient::get_json(const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path) {
|
2026-10-24 04:12:52 +02:00
|
|
|
return request(clients::Method::kGet, service, path, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clients::body::Json GoogleClient::delete_json(const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path) {
|
|
|
|
|
return request(clients::Method::kDelete, service, path, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GoogleClient::stream_json(const ServiceEndpoint& service,
|
|
|
|
|
const std::string& path,
|
|
|
|
|
const clients::body::Json& body,
|
|
|
|
|
const EventSink& sink) {
|
2026-10-24 04:05:41 +02:00
|
|
|
if (!configured_) throw NotConfiguredError(service.name);
|
2026-10-24 04:12:52 +02:00
|
|
|
StreamDecoder decoder(service.name, sink);
|
2026-10-24 04:05:41 +02:00
|
|
|
try {
|
2026-10-24 04:12:52 +02:00
|
|
|
clients::ClientRequest req(
|
|
|
|
|
clients::Method::kPost, clients::Url::parse(service.base_url + path));
|
|
|
|
|
req.json(body);
|
|
|
|
|
client_.send_stream_blocking(
|
|
|
|
|
std::move(req),
|
|
|
|
|
[&](const std::string& chunk) { return decoder.feed(chunk); });
|
|
|
|
|
decoder.finish();
|
2026-10-24 04:05:41 +02:00
|
|
|
} catch (const GoogleError&) {
|
|
|
|
|
throw;
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
throw GoogleError(502, "UNAVAILABLE", e.what(), service.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clients::body::Json GoogleClient::finish(
|
|
|
|
|
const ServiceEndpoint& service, const clients::ClientResponse& response) {
|
|
|
|
|
if (!response.is_success()) throw error_from_response(service.name, response);
|
|
|
|
|
try {
|
|
|
|
|
return response.json();
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
throw GoogleError(502, "BAD_RESPONSE", e.what(), service.name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace molo::google
|