Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest): cache credentials #9620

Merged
merged 1 commit into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions google/cloud/internal/curl_rest_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,19 @@ CurlRestClient::CurlRestClient(std::string endpoint_address,
handle_factory_(std::move(factory)),
x_goog_api_client_header_("x-goog-api-client: " +
google::cloud::internal::ApiClientHeader()),
options_(std::move(options)) {}
options_(std::move(options)) {
if (options_.has<UnifiedCredentialsOption>()) {
credentials_ = MapCredentials(options_.get<UnifiedCredentialsOption>());
}
}

StatusOr<std::unique_ptr<CurlImpl>> CurlRestClient::CreateCurlImpl(
RestRequest const& request) {
auto handle = CurlHandle::MakeFromPool(*handle_factory_);
auto impl =
absl::make_unique<CurlImpl>(std::move(handle), handle_factory_, options_);
if (options_.has<UnifiedCredentialsOption>()) {
auto credentials = MapCredentials(options_.get<UnifiedCredentialsOption>());
auto auth_header = credentials->AuthorizationHeader();
if (credentials_) {
auto auth_header = credentials_->AuthorizationHeader();
if (!auth_header.ok()) return std::move(auth_header).status();
impl->SetHeader(auth_header.value());
}
Expand Down
4 changes: 3 additions & 1 deletion google/cloud/internal/curl_rest_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_REST_CLIENT_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_INTERNAL_CURL_REST_CLIENT_H

#include "google/cloud/internal/oauth2_credentials.h"
#include "google/cloud/internal/rest_client.h"
#include "google/cloud/internal/rest_options.h"
#include "google/cloud/internal/rest_request.h"
Expand Down Expand Up @@ -71,7 +72,7 @@ class CurlRestClient : public RestClient {
friend std::unique_ptr<RestClient> MakeDefaultRestClient(
std::string endpoint_address, Options options);

friend class std::unique_ptr<RestClient> MakePooledRestClient(
friend std::unique_ptr<RestClient> MakePooledRestClient(
std::string endpoint_address, Options options);

CurlRestClient(std::string endpoint_address,
Expand All @@ -82,6 +83,7 @@ class CurlRestClient : public RestClient {
std::string endpoint_address_;
std::shared_ptr<CurlHandleFactory> handle_factory_;
std::string x_goog_api_client_header_;
std::shared_ptr<oauth2_internal::Credentials> credentials_;
Options options_;
};

Expand Down