google-cloud-translate-v2 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/google/cloud/translate/v2/api.rb +19 -0
- data/lib/google/cloud/translate/v2/service.rb +52 -8
- data/lib/google/cloud/translate/v2/version.rb +1 -1
- data/lib/google/cloud/translate/v2.rb +48 -9
- metadata +5 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fef51471c5a0d897f8f59371c1c747d02ed0b5947957eecee0d4800c040e0c4e
|
|
4
|
+
data.tar.gz: cff4165fe95482f93e10cd9848887a608e8738b8c38fbd33c2cd28f345708c7f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b8ef36acc39982279a5c2c3c0a35f7f588e87cde48e9457bc8e872def63190a7b54c1b8061f27879a87311ecdff4a4e62075aacf3eb01eba1e42ca787c1623a5
|
|
7
|
+
data.tar.gz: 419eba42cd617700c107d196aafc5812690c3bea957427e04a89ffe1f80cbbcdd313c8dcfcd7d5b72bca4c15532a3785617db19ca823ce42d53b487ea9347544
|
data/CHANGELOG.md
CHANGED
|
@@ -77,6 +77,25 @@ module Google
|
|
|
77
77
|
end
|
|
78
78
|
alias project project_id
|
|
79
79
|
|
|
80
|
+
##
|
|
81
|
+
# The effective universe domain connected to.
|
|
82
|
+
#
|
|
83
|
+
# @return [String]
|
|
84
|
+
#
|
|
85
|
+
# @example
|
|
86
|
+
# require "google/cloud/translate/v2"
|
|
87
|
+
#
|
|
88
|
+
# translate = Google::Cloud::Translate::V2.new(
|
|
89
|
+
# project_id: "my-todo-project",
|
|
90
|
+
# credentials: "/path/to/keyfile.json"
|
|
91
|
+
# )
|
|
92
|
+
#
|
|
93
|
+
# translate.universe_domain #=> "googleapis.com"
|
|
94
|
+
#
|
|
95
|
+
def universe_domain
|
|
96
|
+
service.universe_domain
|
|
97
|
+
end
|
|
98
|
+
|
|
80
99
|
##
|
|
81
100
|
# Returns text translations from one language to another.
|
|
82
101
|
#
|
|
@@ -28,20 +28,36 @@ module Google
|
|
|
28
28
|
# Represents the Translation API REST service, exposing the API calls.
|
|
29
29
|
class Service # :nodoc:
|
|
30
30
|
API_VERSION = "v2".freeze
|
|
31
|
+
DEFAULT_UNIVERSE_DOMAIN = "googleapis.com".freeze
|
|
31
32
|
API_HOST = "translate.googleapis.com".freeze
|
|
32
33
|
|
|
33
34
|
# @private
|
|
34
35
|
attr_accessor :project_id, :credentials, :retries, :timeout, :key
|
|
35
36
|
|
|
37
|
+
# @private
|
|
38
|
+
# @return [String] The effective universe domain.
|
|
39
|
+
attr_reader :universe_domain
|
|
40
|
+
|
|
36
41
|
##
|
|
37
42
|
# Creates a new Service instance.
|
|
38
|
-
def initialize project_id, credentials, retries: nil, timeout: nil, key: nil, host: nil
|
|
43
|
+
def initialize project_id, credentials, retries: nil, timeout: nil, key: nil, host: nil, universe_domain: nil
|
|
39
44
|
@project_id = project_id
|
|
40
45
|
@credentials = credentials
|
|
41
46
|
@retries = retries
|
|
42
47
|
@timeout = timeout
|
|
43
48
|
@key = key
|
|
44
|
-
|
|
49
|
+
|
|
50
|
+
# Reuse `V2.resolve_universe_domain` so direct `Service.new` callers
|
|
51
|
+
# and `V2.new` share identical config/ENV precedence and validation,
|
|
52
|
+
# then verify that OAuth/JWT credentials match the resolved universe.
|
|
53
|
+
@universe_domain = V2.resolve_universe_domain universe_domain
|
|
54
|
+
verify_universe_domain! unless @key
|
|
55
|
+
|
|
56
|
+
# Default the REST endpoint to `translate.<universe_domain>` unless an
|
|
57
|
+
# explicit host override (either bare hostname or full URL) was given.
|
|
58
|
+
target_host = host || "translate.#{@universe_domain}"
|
|
59
|
+
@url = target_host.sub %r{/\z}, ""
|
|
60
|
+
@url = "https://#{@url}" unless @url.start_with? "http://", "https://"
|
|
45
61
|
end
|
|
46
62
|
|
|
47
63
|
##
|
|
@@ -135,16 +151,44 @@ module Google
|
|
|
135
151
|
end
|
|
136
152
|
|
|
137
153
|
##
|
|
138
|
-
#
|
|
154
|
+
# Signs outgoing HTTP requests using `googleauth`'s `#apply!` contract
|
|
155
|
+
# instead of calling `fetch_access_token!` directly. Using `#apply!`
|
|
156
|
+
# allows self-signed JWT credentials to sign locally for the target
|
|
157
|
+
# universe without exchanging assertions at GDU token endpoints, while
|
|
158
|
+
# still refreshing OAuth2 bearer tokens when needed.
|
|
139
159
|
def sign_http_request! request
|
|
140
|
-
client = credentials
|
|
141
|
-
return if client.nil?
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
160
|
+
client = credentials&.client
|
|
161
|
+
return request if client.nil?
|
|
162
|
+
|
|
163
|
+
# Seed `:jwt_aud_uri` with the universe-scoped service audience so
|
|
164
|
+
# scope-less ServiceAccountCredentials can mint an audience-bound JWT,
|
|
165
|
+
# then strip the helper key so it is never sent as an HTTP header.
|
|
166
|
+
# `Faraday::Utils::Headers#merge!` normalizes `:authorization` to `"Authorization"`.
|
|
167
|
+
auth_headers = { jwt_aud_uri: "https://translate.#{@universe_domain}/" }
|
|
168
|
+
client.apply! auth_headers
|
|
169
|
+
auth_headers.delete :jwt_aud_uri
|
|
170
|
+
request.headers.merge! auth_headers
|
|
145
171
|
request
|
|
146
172
|
end
|
|
147
173
|
|
|
174
|
+
##
|
|
175
|
+
# Verifies that the credentials' universe domain matches the service's
|
|
176
|
+
# effective universe domain unless the credential explicitly opts out
|
|
177
|
+
# (e.g. GCE metadata credentials prior to universe domain discovery).
|
|
178
|
+
# Credentials with a `nil` universe domain are only accepted for the
|
|
179
|
+
# default universe (`googleapis.com`); non-default universes fail closed.
|
|
180
|
+
def verify_universe_domain!
|
|
181
|
+
return unless @credentials.is_a? Google::Auth::Credentials
|
|
182
|
+
return if @credentials.disable_universe_domain_check
|
|
183
|
+
|
|
184
|
+
creds_universe = @credentials.universe_domain
|
|
185
|
+
return if creds_universe == @universe_domain
|
|
186
|
+
return if creds_universe.nil? && @universe_domain == DEFAULT_UNIVERSE_DOMAIN
|
|
187
|
+
|
|
188
|
+
raise Google::Cloud::Error,
|
|
189
|
+
"Universe domain is #{@universe_domain} but credentials are in #{creds_universe || 'nil'}"
|
|
190
|
+
end
|
|
191
|
+
|
|
148
192
|
##
|
|
149
193
|
# @private Backoff
|
|
150
194
|
class Backoff
|
|
@@ -65,6 +65,8 @@ module Google
|
|
|
65
65
|
# endpoint.
|
|
66
66
|
# @param [Boolean] enable_self_signed_jwt Whether to enable self-signed JWT. Optional. If the param is nil,
|
|
67
67
|
# it is computed based on the endpoint.
|
|
68
|
+
# @param [String] universe_domain Override of the universe domain. Optional. If the param is nil, uses the
|
|
69
|
+
# configured `universe_domain`, `ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"]`, or `"googleapis.com"`.
|
|
68
70
|
#
|
|
69
71
|
# @return [Google::Cloud::Translate::V2::Api]
|
|
70
72
|
#
|
|
@@ -100,19 +102,34 @@ module Google
|
|
|
100
102
|
# translation = translate.translate "Hello world!", to: "la"
|
|
101
103
|
# translation.text #=> "Salve mundi!"
|
|
102
104
|
#
|
|
103
|
-
def self.new project_id: nil,
|
|
105
|
+
def self.new project_id: nil,
|
|
106
|
+
credentials: nil,
|
|
107
|
+
key: nil,
|
|
108
|
+
scope: nil,
|
|
109
|
+
retries: nil,
|
|
110
|
+
timeout: nil,
|
|
111
|
+
endpoint: nil,
|
|
112
|
+
enable_self_signed_jwt: nil,
|
|
113
|
+
universe_domain: nil
|
|
104
114
|
project_id ||= default_project_id
|
|
105
115
|
|
|
116
|
+
# Reuse the shared `Google::Cloud.configure.translate` subconfig when
|
|
117
|
+
# `google-cloud-translate` is loaded; otherwise `translation_config` is nil.
|
|
106
118
|
configuration = translation_config
|
|
107
119
|
key ||= configuration&.key || ENV["TRANSLATE_KEY"] || ENV["GOOGLE_CLOUD_KEY"]
|
|
108
120
|
retries ||= configuration&.retries
|
|
109
121
|
timeout ||= configuration&.timeout
|
|
110
122
|
endpoint ||= configuration&.endpoint
|
|
123
|
+
universe_domain ||= configuration&.universe_domain
|
|
111
124
|
|
|
112
125
|
if key
|
|
126
|
+
# API-key requests bypass OAuth/JWT credentials (and credential
|
|
127
|
+
# universe matching), so forward `universe_domain` here as well to
|
|
128
|
+
# route key-authenticated calls to `translate.<universe_domain>`.
|
|
113
129
|
return Google::Cloud::Translate::V2::Api.new(
|
|
114
130
|
Google::Cloud::Translate::V2::Service.new(
|
|
115
|
-
project_id.to_s, nil,
|
|
131
|
+
project_id.to_s, nil,
|
|
132
|
+
retries: retries, timeout: timeout, key: key, host: endpoint, universe_domain: universe_domain
|
|
116
133
|
)
|
|
117
134
|
)
|
|
118
135
|
end
|
|
@@ -120,16 +137,19 @@ module Google
|
|
|
120
137
|
scope ||= configuration&.scope
|
|
121
138
|
|
|
122
139
|
if enable_self_signed_jwt.nil?
|
|
123
|
-
#
|
|
124
|
-
#
|
|
125
|
-
#
|
|
126
|
-
|
|
140
|
+
# Enable self-signed JWTs when using the default endpoint for the
|
|
141
|
+
# effective universe (`translate.<universe_domain>`), rather than
|
|
142
|
+
# comparing against a hardcoded `translate.googleapis.com` host.
|
|
143
|
+
effective_universe = resolve_universe_domain universe_domain
|
|
144
|
+
enable_self_signed_jwt = endpoint.nil? || endpoint == "translate.#{effective_universe}"
|
|
127
145
|
end
|
|
128
146
|
|
|
129
147
|
credentials ||= default_credentials scope: scope, enable_self_signed_jwt: enable_self_signed_jwt
|
|
130
148
|
|
|
131
149
|
unless credentials.is_a? Google::Auth::Credentials
|
|
132
|
-
credentials = Google::Cloud::Translate::V2::Credentials.new
|
|
150
|
+
credentials = Google::Cloud::Translate::V2::Credentials.new(
|
|
151
|
+
credentials, { scope: scope, enable_self_signed_jwt: enable_self_signed_jwt }
|
|
152
|
+
)
|
|
133
153
|
end
|
|
134
154
|
|
|
135
155
|
project_id = resolve_project_id project_id, credentials
|
|
@@ -137,11 +157,28 @@ module Google
|
|
|
137
157
|
|
|
138
158
|
Google::Cloud::Translate::V2::Api.new(
|
|
139
159
|
Google::Cloud::Translate::V2::Service.new(
|
|
140
|
-
project_id, credentials,
|
|
160
|
+
project_id, credentials,
|
|
161
|
+
retries: retries, timeout: timeout, host: endpoint, universe_domain: universe_domain
|
|
141
162
|
)
|
|
142
163
|
)
|
|
143
164
|
end
|
|
144
165
|
|
|
166
|
+
##
|
|
167
|
+
# @private Resolves the effective universe domain using standard Google
|
|
168
|
+
# Cloud client precedence: explicit argument -> `Translate.configure.universe_domain`
|
|
169
|
+
# -> non-blank `ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"]` -> `"googleapis.com"`.
|
|
170
|
+
def self.resolve_universe_domain universe_domain = nil
|
|
171
|
+
universe_domain = translation_config&.universe_domain if universe_domain.nil?
|
|
172
|
+
if !universe_domain.nil? && universe_domain.to_s.strip.empty?
|
|
173
|
+
raise ArgumentError, "universe_domain cannot be empty"
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
env_universe = ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"]&.strip
|
|
177
|
+
env_universe = nil if env_universe == ""
|
|
178
|
+
|
|
179
|
+
universe_domain || env_universe || Service::DEFAULT_UNIVERSE_DOMAIN
|
|
180
|
+
end
|
|
181
|
+
|
|
145
182
|
##
|
|
146
183
|
# @private Default project.
|
|
147
184
|
def self.default_project_id
|
|
@@ -159,7 +196,9 @@ module Google
|
|
|
159
196
|
"TRANSLATE_CREDENTIALS", "TRANSLATE_CREDENTIALS_JSON", "TRANSLATE_KEYFILE", "TRANSLATE_KEYFILE_JSON"
|
|
160
197
|
) ||
|
|
161
198
|
Google::Cloud.configure.credentials ||
|
|
162
|
-
Google::Cloud::Translate::V2::Credentials.default(
|
|
199
|
+
Google::Cloud::Translate::V2::Credentials.default(
|
|
200
|
+
{ scope: scope, enable_self_signed_jwt: enable_self_signed_jwt }
|
|
201
|
+
)
|
|
163
202
|
end
|
|
164
203
|
|
|
165
204
|
##
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: google-cloud-translate-v2
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.2.
|
|
4
|
+
version: 1.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Google LLC
|
|
@@ -73,22 +73,16 @@ dependencies:
|
|
|
73
73
|
name: googleauth
|
|
74
74
|
requirement: !ruby/object:Gem::Requirement
|
|
75
75
|
requirements:
|
|
76
|
-
- - "
|
|
77
|
-
- !ruby/object:Gem::Version
|
|
78
|
-
version: 0.16.2
|
|
79
|
-
- - "<"
|
|
76
|
+
- - "~>"
|
|
80
77
|
- !ruby/object:Gem::Version
|
|
81
|
-
version:
|
|
78
|
+
version: '1.12'
|
|
82
79
|
type: :runtime
|
|
83
80
|
prerelease: false
|
|
84
81
|
version_requirements: !ruby/object:Gem::Requirement
|
|
85
82
|
requirements:
|
|
86
|
-
- - "
|
|
87
|
-
- !ruby/object:Gem::Version
|
|
88
|
-
version: 0.16.2
|
|
89
|
-
- - "<"
|
|
83
|
+
- - "~>"
|
|
90
84
|
- !ruby/object:Gem::Version
|
|
91
|
-
version:
|
|
85
|
+
version: '1.12'
|
|
92
86
|
- !ruby/object:Gem::Dependency
|
|
93
87
|
name: google-cloud-core
|
|
94
88
|
requirement: !ruby/object:Gem::Requirement
|