Skip to content

Commit 1a3b35d

Browse files
committed
feat(rest): add faraday_config option to client configuration
Addresses issue #57 by exposing the internal Faraday::Connection customization hook to the public client layer. This enables advanced HTTP setup, such as mTLS client certificate configuration. Instead of forwarding blocks through the client constructors (which collides with the existing configuration blocks), we introduced a config.faraday_config attribute. This approach isolates transport specific details, allows clean automated propagation to internal subclients (LROs and mixins), and uses a standard respond_to? check to preserve perfect backward compatibility with legacy external gems. ```ruby client = Google::Cloud::Language::V1::LanguageService::Rest::Client.new do |config| config.faraday_config = ->(conn) do conn.ssl.client_cert = OpenSSL::X509::Certificate.new cert_pem conn.ssl.client_key = OpenSSL::PKey::RSA.new key_pem end end ```
1 parent 9c6f951 commit 1a3b35d

87 files changed

Lines changed: 1212 additions & 296 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

gapic-generator-cloud/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ gem "grpc-tools", ">= 1.60.0", "< 1.75.1"
1313
gem "minitest", "~> 5.16"
1414
gem "minitest-autotest", "~> 1.0"
1515
gem "minitest-focus", "~> 1.0"
16+
gem "ostruct", "~> 0.6.3"
1617
gem "pry", ">= 0.14"
1718
gem "redcarpet", "~> 3.0"
1819
gem "yard", "~> 0.9"

gapic-generator/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ gem "grpc-tools", ">= 1.60.0", "< 1.75.1"
1111
gem "minitest", "~> 5.16"
1212
gem "minitest-autotest", "~> 1.0"
1313
gem "minitest-focus", "~> 1.0"
14+
gem "ostruct", "~> 0.6.3"
1415
gem "pry", ">= 0.14"
1516
gem "redcarpet", "~> 3.0"
1617
gem "yard", "~> 0.9"

gapic-generator/templates/default/service/rest/client/_client.text.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class <%= service.rest.client_name %>
137137
<%- if subclient.respond_to?(:bindings_override) && !subclient.bindings_override.empty? -%>
138138
config.bindings_override = @config.bindings_override
139139
<%- end -%>
140+
config.faraday_config = @config.faraday_config if config.respond_to? :faraday_config=
140141
end
141142

142143
<%- end -%>
@@ -145,7 +146,8 @@ class <%= service.rest.client_name %>
145146
endpoint_template: DEFAULT_ENDPOINT_TEMPLATE,
146147
universe_domain: @config.universe_domain,
147148
credentials: credentials,
148-
logger: @config.logger
149+
logger: @config.logger,
150+
faraday_config: @config.faraday_config
149151
)
150152

151153
@<%= service.stub_name %>.logger(stub: true)&.info do |entry|
@@ -168,6 +170,7 @@ class <%= service.rest.client_name %>
168170
config.bindings_override = @config.bindings_override
169171
<%- end -%>
170172
config.logger = @<%= service.stub_name %>.logger if config.respond_to? :logger=
173+
config.faraday_config = @config.faraday_config if config.respond_to? :faraday_config=
171174
end
172175
<%- end -%>
173176
end

gapic-generator/templates/default/service/rest/client/_config.text.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
# `:default` (the default) to construct a default logger, or `nil` to
9090
# explicitly disable logging.
9191
# @return [::Logger,:default,nil]
92+
# @!attribute [rw] faraday_config
93+
# A Proc to configure the underlying Faraday connection object.
94+
# @return [::Proc,nil]
9295
#
9396
class Configuration
9497
extend ::Gapic::Config
@@ -120,6 +123,7 @@ class Configuration
120123
config_attr :bindings_override, {}, ::Hash, nil
121124
<%- end -%>
122125
config_attr :logger, :default, ::Logger, nil, :default
126+
config_attr :faraday_config, nil, ::Proc, nil
123127

124128
# @private
125129
def initialize parent_config = nil

gapic-generator/templates/default/service/rest/client/_operations.text.erb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class <%= service.operations_name %>
8080
endpoint: @config.endpoint,
8181
endpoint_template: DEFAULT_ENDPOINT_TEMPLATE,
8282
universe_domain: @config.universe_domain,
83-
credentials: credentials
83+
credentials: credentials,
84+
faraday_config: @config.faraday_config
8485
)
8586

8687
# Used by an LRO wrapper for some methods of this service
@@ -102,15 +103,18 @@ end
102103
# Service stub contains baseline method implementations
103104
# including transcoding, making the REST call, and deserialing the response.
104105
class <%= service.operations_stub_name %>
105-
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:
106+
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:, faraday_config: nil
106107
# These require statements are intentionally placed here to initialize
107108
# the REST modules only when it's required.
108109
require "gapic/rest"
109110

110-
@client_stub = ::Gapic::Rest::ClientStub.new endpoint: endpoint,
111-
endpoint_template: endpoint_template,
112-
universe_domain: universe_domain,
113-
credentials: credentials
111+
@client_stub = ::Gapic::Rest::ClientStub.new(
112+
endpoint: endpoint,
113+
endpoint_template: endpoint_template,
114+
universe_domain: universe_domain,
115+
credentials: credentials,
116+
&faraday_config
117+
)
114118
end
115119

116120
<%- service.lro_service.rest.methods.each do |method| -%>

gapic-generator/templates/default/service/rest/service_stub/_service_stub.text.erb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ require "<%= service.proto_service_require %>"
1010
<%= render partial: "proto_docs/deprecated", locals: { presenter: service } -%>
1111
class <%= service.rest.service_stub_name %>
1212
# @private
13-
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:, logger:
13+
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:, logger:, faraday_config: nil
1414
# These require statements are intentionally placed here to initialize
1515
# the REST modules only when it's required.
1616
require "gapic/rest"
1717

18-
@client_stub = ::Gapic::Rest::ClientStub.new endpoint: endpoint,
19-
endpoint_template: endpoint_template,
20-
universe_domain: universe_domain,
21-
credentials: credentials,
22-
numeric_enums: <%= service.rest.numeric_enums? %>,
23-
service_name: self.class,
24-
raise_faraday_errors: false,
25-
logger: logger
18+
@client_stub = ::Gapic::Rest::ClientStub.new(
19+
endpoint: endpoint,
20+
endpoint_template: endpoint_template,
21+
universe_domain: universe_domain,
22+
credentials: credentials,
23+
numeric_enums: <%= service.rest.numeric_enums? %>,
24+
service_name: self.class,
25+
raise_faraday_errors: false,
26+
logger: logger,
27+
&faraday_config
28+
)
2629
end
2730

2831
##

gapic-generator/templates/default/service/rest/test/method/_configure.text.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@ def test_configure
1818
assert_same block_config, config
1919
assert_kind_of <%= full_client_name %>::Configuration, config
2020
end
21+
22+
def test_faraday_config
23+
# Create test objects.
24+
credentials_token = :dummy_value
25+
captured_blocks = []
26+
27+
faraday_config_proc = ->(conn) { conn.ssl.client_cert = "cert" }
28+
29+
stub_proc = ->(**kwargs, &block) do
30+
captured_blocks << block
31+
ClientStub.new nil
32+
end
33+
34+
# Create client with faraday_config option.
35+
Gapic::Rest::ClientStub.stub :new, stub_proc do
36+
<%= full_client_name %>.new do |config|
37+
config.credentials = credentials_token
38+
config.faraday_config = faraday_config_proc
39+
end
40+
end
41+
42+
# Verify faraday_config block passthrough.
43+
assert_includes captured_blocks, faraday_config_proc
44+
end
45+

shared/output/cloud/compute_small/lib/google/cloud/compute/v1/addresses/rest/client.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,16 @@ def initialize
150150
config.quota_project = @quota_project_id
151151
config.endpoint = @config.endpoint
152152
config.universe_domain = @config.universe_domain
153+
config.faraday_config = @config.faraday_config if config.respond_to? :faraday_config=
153154
end
154155

155156
@addresses_stub = ::Google::Cloud::Compute::V1::Addresses::Rest::ServiceStub.new(
156157
endpoint: @config.endpoint,
157158
endpoint_template: DEFAULT_ENDPOINT_TEMPLATE,
158159
universe_domain: @config.universe_domain,
159160
credentials: credentials,
160-
logger: @config.logger
161+
logger: @config.logger,
162+
faraday_config: @config.faraday_config
161163
)
162164

163165
@addresses_stub.logger(stub: true)&.info do |entry|
@@ -758,6 +760,9 @@ def list request, options = nil
758760
# `:default` (the default) to construct a default logger, or `nil` to
759761
# explicitly disable logging.
760762
# @return [::Logger,:default,nil]
763+
# @!attribute [rw] faraday_config
764+
# A Proc to configure the underlying Faraday connection object.
765+
# @return [::Proc,nil]
761766
#
762767
class Configuration
763768
extend ::Gapic::Config
@@ -780,6 +785,7 @@ class Configuration
780785
config_attr :quota_project, nil, ::String, nil
781786
config_attr :universe_domain, nil, ::String, nil
782787
config_attr :logger, :default, ::Logger, nil, :default
788+
config_attr :faraday_config, nil, ::Proc, nil
783789

784790
# @private
785791
def initialize parent_config = nil

shared/output/cloud/compute_small/lib/google/cloud/compute/v1/addresses/rest/service_stub.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ module Rest
3131
#
3232
class ServiceStub
3333
# @private
34-
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:, logger:
34+
def initialize endpoint:, endpoint_template:, universe_domain:, credentials:, logger:, faraday_config: nil
3535
# These require statements are intentionally placed here to initialize
3636
# the REST modules only when it's required.
3737
require "gapic/rest"
3838

39-
@client_stub = ::Gapic::Rest::ClientStub.new endpoint: endpoint,
40-
endpoint_template: endpoint_template,
41-
universe_domain: universe_domain,
42-
credentials: credentials,
43-
numeric_enums: false,
44-
service_name: self.class,
45-
raise_faraday_errors: false,
46-
logger: logger
39+
@client_stub = ::Gapic::Rest::ClientStub.new(
40+
endpoint: endpoint,
41+
endpoint_template: endpoint_template,
42+
universe_domain: universe_domain,
43+
credentials: credentials,
44+
numeric_enums: false,
45+
service_name: self.class,
46+
raise_faraday_errors: false,
47+
logger: logger,
48+
&faraday_config
49+
)
4750
end
4851

4952
##

shared/output/cloud/compute_small/lib/google/cloud/compute/v1/global_operations/rest/client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def initialize
149149
endpoint_template: DEFAULT_ENDPOINT_TEMPLATE,
150150
universe_domain: @config.universe_domain,
151151
credentials: credentials,
152-
logger: @config.logger
152+
logger: @config.logger,
153+
faraday_config: @config.faraday_config
153154
)
154155

155156
@global_operations_stub.logger(stub: true)&.info do |entry|
@@ -421,6 +422,9 @@ def get request, options = nil
421422
# `:default` (the default) to construct a default logger, or `nil` to
422423
# explicitly disable logging.
423424
# @return [::Logger,:default,nil]
425+
# @!attribute [rw] faraday_config
426+
# A Proc to configure the underlying Faraday connection object.
427+
# @return [::Proc,nil]
424428
#
425429
class Configuration
426430
extend ::Gapic::Config
@@ -443,6 +447,7 @@ class Configuration
443447
config_attr :quota_project, nil, ::String, nil
444448
config_attr :universe_domain, nil, ::String, nil
445449
config_attr :logger, :default, ::Logger, nil, :default
450+
config_attr :faraday_config, nil, ::Proc, nil
446451

447452
# @private
448453
def initialize parent_config = nil

0 commit comments

Comments
 (0)