Skip to content

Commit

Permalink
fix(rest): cache credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
scotthart committed Aug 2, 2022
1 parent e8cc9a9 commit 41279e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit 41279e8

Please sign in to comment.