65 lines
2.1 KiB
C++
65 lines
2.1 KiB
C++
#include "google/client.h"
|
|||
|
|
|
||
|
|
#include "clients/transports/http_transport.h"
|
||
|
|
#include "google/credentials.h"
|
||
|
|
|
||
|
|
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());
|
||
|
|
}
|
||
|
|
|
||
|
|
clients::body::Json GoogleClient::post_json(const ServiceEndpoint& service,
|
||
|
|
const std::string& path,
|
||
|
|
const clients::body::Json& body) {
|
||
|
|
if (!configured_) throw NotConfiguredError(service.name);
|
||
|
|
try {
|
||
|
|
return finish(service, client_.post_json_blocking(
|
||
|
|
service.base_url + path, body));
|
||
|
|
} catch (const GoogleError&) {
|
||
|
|
throw;
|
||
|
|
} catch (const std::exception& e) {
|
||
|
|
throw GoogleError(502, "UNAVAILABLE", e.what(), service.name);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
clients::body::Json GoogleClient::get_json(const ServiceEndpoint& service,
|
||
|
|
const std::string& path) {
|
||
|
|
if (!configured_) throw NotConfiguredError(service.name);
|
||
|
|
try {
|
||
|
|
return finish(service, client_.get_blocking(service.base_url + path));
|
||
|
|
} 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
|