feat: Support custom quota_project in request options

This commit is contained in:
Daniel Azuma 2020-06-03 13:21:09 -07:00 committed by GitHub
parent ddf80e2dc9
commit 197286a8a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View File

@ -52,12 +52,9 @@ module Google
# #
# @return [void] # @return [void]
def prepare! def prepare!
set_xgac set_api_client_header
if options&.authorization.respond_to? :quota_project_id set_user_project_header
quota_project_id = options.authorization.quota_project_id if options&.api_format_version
header['X-Goog-User-Project'] = quota_project_id if quota_project_id
end
if options && options.api_format_version
header['X-Goog-Api-Format-Version'] = options.api_format_version.to_s header['X-Goog-Api-Format-Version'] = options.api_format_version.to_s
end end
query[FIELDS_PARAM] = normalize_fields_param(query[FIELDS_PARAM]) if query.key?(FIELDS_PARAM) query[FIELDS_PARAM] = normalize_fields_param(query[FIELDS_PARAM]) if query.key?(FIELDS_PARAM)
@ -130,7 +127,7 @@ module Google
private private
def set_xgac def set_api_client_header
old_xgac = header old_xgac = header
.find_all { |k, v| k.downcase == 'x-goog-api-client' } .find_all { |k, v| k.downcase == 'x-goog-api-client' }
.map { |(a, b)| b } .map { |(a, b)| b }
@ -144,6 +141,14 @@ module Google
header['X-Goog-Api-Client'] = xgac header['X-Goog-Api-Client'] = xgac
end end
def set_user_project_header
quota_project_id = options.quota_project
if !quota_project_id && options&.authorization.respond_to?(:quota_project_id)
quota_project_id = options.authorization.quota_project_id
end
header['X-Goog-User-Project'] = quota_project_id if quota_project_id
end
# Attempt to parse a JSON error message # Attempt to parse a JSON error message
# @param [String] body # @param [String] body
# HTTP response body # HTTP response body

View File

@ -33,7 +33,8 @@ module Google
:skip_serialization, :skip_serialization,
:skip_deserialization, :skip_deserialization,
:api_format_version, :api_format_version,
:use_opencensus) :use_opencensus,
:quota_project)
# General client options # General client options
class ClientOptions class ClientOptions
@ -76,6 +77,8 @@ module Google
# @return [Fixnum] Version of the error format to request/expect. # @return [Fixnum] Version of the error format to request/expect.
# @!attribute [rw] use_opencensus # @!attribute [rw] use_opencensus
# @return [Boolean] Whether OpenCensus spans should be generated for requests. Default is true. # @return [Boolean] Whether OpenCensus spans should be generated for requests. Default is true.
# @!attribute [rw] quota_project
# @return [String] Project ID to charge quota, or `nil` to default to the credentials-specified project.
# Get the default options # Get the default options
# @return [Google::Apis::RequestOptions] # @return [Google::Apis::RequestOptions]
@ -105,5 +108,6 @@ module Google
RequestOptions.default.skip_deserialization = false RequestOptions.default.skip_deserialization = false
RequestOptions.default.api_format_version = nil RequestOptions.default.api_format_version = nil
RequestOptions.default.use_opencensus = true RequestOptions.default.use_opencensus = true
RequestOptions.default.quota_project = nil
end end
end end

View File

@ -71,11 +71,18 @@ RSpec.describe Google::Apis::Core::ApiCommand do
expect(command.header['X-Goog-User-Project']).to be_nil expect(command.header['X-Goog-User-Project']).to be_nil
end end
it 'should set the X-Goog-User-Project to a given quota_project' do it 'should set the X-Goog-User-Project to the credentials quota_project' do
command.options.authorization = OpenStruct.new quota_project_id: "b_project_id" command.options.authorization = OpenStruct.new quota_project_id: "b_project_id"
command.prepare! command.prepare!
expect(command.header['X-Goog-User-Project']).to eql "b_project_id" expect(command.header['X-Goog-User-Project']).to eql "b_project_id"
end end
it 'should set the X-Goog-User-Project to a custom quota_project in preference to credentials' do
command.options.authorization = OpenStruct.new quota_project_id: "b_project_id"
command.options.quota_project = "c_project_id"
command.prepare!
expect(command.header['X-Goog-User-Project']).to eql "c_project_id"
end
end end
context('with a request body') do context('with a request body') do