Files
moloapp/src/google/client.cpp
T

115 lines
4.3 KiB
C++
Raw Normal View History

#include "google/client.h"
#include "clients/transports/http_transport.h"
#include "google/credentials.h"
#include "google/stream_decoder.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)
: GoogleClient(std::move(transport), std::move(auth), nullptr) {}
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
std::shared_ptr<clients::Auth> key,
std::shared_ptr<clients::Auth> oauth)
: client_(std::move(transport), make_config(nullptr)),
key_auth_(std::move(key)),
oauth_auth_(std::move(oauth)),
configured_(key_auth_ != nullptr || oauth_auth_ != nullptr) {}
std::shared_ptr<clients::Auth> GoogleClient::pick_auth(
const ServiceEndpoint& service) const {
if (oauth_auth_ && !service.prefer_key) return oauth_auth_;
return key_auth_ ? key_auth_ : oauth_auth_;
}
std::shared_ptr<GoogleClient> GoogleClient::from_env() {
return std::make_shared<GoogleClient>(
std::make_unique<clients::transports::HttpTransport>(),
Credentials::api_key_from_env(), Credentials::oauth_from_env());
}
clients::body::Json GoogleClient::request(clients::Method method,
const ServiceEndpoint& service,
const std::string& path,
const clients::body::Json* body) {
if (!configured_) throw NotConfiguredError(service.name);
try {
clients::ClientRequest req(method,
clients::Url::parse(service.base_url + path));
if (body) req.json(*body);
pick_auth(service)->apply(req);
return finish(service, client_.send_blocking(std::move(req)));
} catch (const GoogleError&) {
throw;
} catch (const std::exception& e) {
throw GoogleError(502, "UNAVAILABLE", e.what(), service.name);
}
}
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);
}
clients::body::Json GoogleClient::get_json(const ServiceEndpoint& service,
const std::string& path) {
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) {
if (!configured_) throw NotConfiguredError(service.name);
StreamDecoder decoder(service.name, sink);
try {
clients::ClientRequest req(
clients::Method::kPost, clients::Url::parse(service.base_url + path));
req.json(body);
pick_auth(service)->apply(req);
client_.send_stream_blocking(
std::move(req),
[&](const std::string& chunk) { return decoder.feed(chunk); });
decoder.finish();
} 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