gapic-common 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d577e52554064fdda97228a6570313d8df9cc3d830ca75b0b43bfd1412a1222
4
- data.tar.gz: 9ec865b1b777f308449320d2813745738af835c5289df0d4783739dac0310f4c
3
+ metadata.gz: 9d5a60dd75b6fe30ba3654e69aa129b07c7a2159dff65134c9fd67ed8a598d70
4
+ data.tar.gz: c8cd0851a4f8f04dab553bf55bda12851cfdec0eaa9e1f6859c0e1a93f2555c7
5
5
  SHA512:
6
- metadata.gz: 71599558c7d1e83abfe002213b24c7089179e52c2e68214002293c0d936ca38283fd4f38e790a3c0ec4c77e228ed1e0083d1ca871c1eaaa43ae33cad7a00ff90
7
- data.tar.gz: d148bf75ff9afed66dd27fd206db1aaaf9627f7ecfd87d0db0abd0090b984e3318e476aa95e719aa580b3dbac7772c5b6856914d66699b54c1dbf2821e37e019
6
+ metadata.gz: e12310e5446b7dffa8a6c5cc3df4b0181414ae2e3118199d45b4cf2e54d1b61a7d72561634402ae319f66a0bcfa3786be67aaf4c8fb3e4a6e839ebdf27a58e64
7
+ data.tar.gz: 46752b4a1dee19463222b187de90071cf1a569ac83a8eabc13373f2d35460eb6a6f0ee681fbeab7165cebed0bf730d08cfe48da326d61c5fb5ef702fd6b020d1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Release History
2
2
 
3
+ ### 0.4.0 / 2021-02-23
4
+
5
+ * Support for the REST calls via the Gapic::Rest::ClientStub and other classes in Gapic::Rest
6
+ REST support is still being developed. Notably the full retries handling is not implemented yet.
7
+
3
8
  ### 0.3.4 / 2020-08-07
4
9
 
5
10
  * Support the :this_channel_is_insecure gRPC pseudo-credential, used by tests and emulators.
@@ -26,7 +26,9 @@ module Gapic
26
26
  # @return [RetryPolicy, Object]
27
27
  #
28
28
  class CallOptions
29
- attr_reader :timeout, :metadata, :retry_policy
29
+ attr_reader :timeout
30
+ attr_reader :metadata
31
+ attr_reader :retry_policy
30
32
 
31
33
  ##
32
34
  # Create a new Options object instance.
@@ -14,6 +14,6 @@
14
14
 
15
15
  module Gapic
16
16
  module Common
17
- VERSION = "0.3.4".freeze
17
+ VERSION = "0.4.0".freeze
18
18
  end
19
19
  end
data/lib/gapic/config.rb CHANGED
@@ -72,7 +72,7 @@ module Gapic
72
72
 
73
73
  if instance_variable_defined? :@parent_config
74
74
  parent = instance_variable_get :@parent_config
75
- return parent.__send__ name if parent&.respond_to? name
75
+ return parent.__send__ name if parent.respond_to? name
76
76
  end
77
77
 
78
78
  default
@@ -122,9 +122,7 @@ module Gapic
122
122
  yield response, operation if block_given?
123
123
  response
124
124
  rescue StandardError => e
125
- if check_retry? deadline
126
- retry if options.retry_policy.call e
127
- end
125
+ retry if check_retry?(deadline) && options.retry_policy.call(e)
128
126
 
129
127
  raise e
130
128
  end
@@ -27,13 +27,11 @@ module GRPC
27
27
  return nil.to_a if rpc_status.nil?
28
28
 
29
29
  rpc_status.details.map do |detail|
30
- begin
31
- detail_type = Google::Protobuf::DescriptorPool.generated_pool.lookup detail.type_name
32
- detail = detail.unpack detail_type.msgclass if detail_type
33
- detail
34
- rescue Google::Protobuf::ParseError
35
- detail
36
- end
30
+ detail_type = Google::Protobuf::DescriptorPool.generated_pool.lookup detail.type_name
31
+ detail = detail.unpack detail_type.msgclass if detail_type
32
+ detail
33
+ rescue Google::Protobuf::ParseError
34
+ detail
37
35
  end
38
36
  end
39
37
  end
@@ -80,13 +80,11 @@ module Gapic
80
80
  #
81
81
  # @raise [RuntimeError] if it's not started yet.
82
82
  #
83
- def each
83
+ def each &block
84
84
  return enum_for :each unless block_given?
85
85
 
86
86
  each_page do |page|
87
- page.each do |obj|
88
- yield obj
89
- end
87
+ page.each(&block)
90
88
  end
91
89
  end
92
90
 
@@ -201,7 +199,8 @@ module Gapic
201
199
  # @return [GRPC::ActiveCall::Operation] the RPC operation for the page.
202
200
  class Page
203
201
  include Enumerable
204
- attr_reader :response, :operation
202
+ attr_reader :response
203
+ attr_reader :operation
205
204
 
206
205
  ##
207
206
  # @private
@@ -96,7 +96,7 @@ module Gapic
96
96
  #
97
97
  # @return [Array<Object>] The coerced version of the given values.
98
98
  def self.coerce_array array, field_descriptor
99
- raise ArgumentError, "Value " + array.to_s + " must be an array" unless array.is_a? Array
99
+ raise ArgumentError, "Value #{array} must be an array" unless array.is_a? Array
100
100
  array.map do |val|
101
101
  coerce_value val, field_descriptor
102
102
  end
data/lib/gapic/rest.rb ADDED
@@ -0,0 +1,27 @@
1
+ # Copyright 2021 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ ##
16
+ # Rest GAPIC features are still under development.
17
+ #
18
+
19
+ require "faraday"
20
+ require "gapic/call_options"
21
+ require "gapic/common/version"
22
+ require "gapic/headers"
23
+ require "gapic/protobuf"
24
+ require "gapic/rest/client_stub"
25
+ require "gapic/rest/error"
26
+ require "gapic/rest/faraday_middleware"
27
+ require "json"
@@ -0,0 +1,126 @@
1
+ # Copyright 2021 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "googleauth"
16
+ require "gapic/rest/faraday_middleware"
17
+
18
+ module Gapic
19
+ module Rest
20
+ ##
21
+ # A class for making REST calls through Faraday
22
+ # ClientStub's responsibilities:
23
+ # - wrap Faraday methods with a bounded explicit interface
24
+ # - store service endpoint and create full url for the request
25
+ # - store credentials and add auth information to the request
26
+ #
27
+ class ClientStub
28
+ ##
29
+ # Initializes with an endpoint and credentials
30
+ # @param endpoint [String] an endpoint for the service that this stub will send requests to
31
+ # @param credentials [Google::Auth::Credentials]
32
+ # Credentials to send with calls in form of a googleauth credentials object.
33
+ # (see the [googleauth docs](https://googleapis.dev/ruby/googleauth/latest/index.html))
34
+ #
35
+ # @yield [Faraday::Connection]
36
+ #
37
+ def initialize endpoint:, credentials:
38
+ @endpoint = endpoint
39
+ @endpoint = "https://#{endpoint}" unless /^https?:/.match? endpoint
40
+ @endpoint.sub! %r{/$}, ""
41
+
42
+ @credentials = credentials
43
+
44
+ @connection = Faraday.new url: @endpoint do |conn|
45
+ conn.headers = { "Content-Type" => "application/json" }
46
+ conn.request :google_authorization, @credentials
47
+ conn.request :retry
48
+ conn.response :raise_error
49
+ conn.adapter :net_http
50
+ end
51
+
52
+ yield @connection if block_given?
53
+ end
54
+
55
+ ##
56
+ # Makes a GET request
57
+ #
58
+ # @param uri [String] uri to send this request to
59
+ # @param params [Hash] query string parameters for the request
60
+ # @param options [::Gapic::CallOptions] gapic options to be applied to the REST call.
61
+ # Currently only timeout and headers are supported.
62
+ # @return [Faraday::Response]
63
+ def make_get_request uri:, params: {}, options: {}
64
+ make_http_request :get, uri: uri, body: nil, params: params, options: options
65
+ end
66
+
67
+ ##
68
+ # Makes a DELETE request
69
+ #
70
+ # @param uri [String] uri to send this request to
71
+ # @param params [Hash] query string parameters for the request
72
+ # @param options [::Gapic::CallOptions] gapic options to be applied to the REST call.
73
+ # Currently only timeout and headers are supported.
74
+ # @return [Faraday::Response]
75
+ def make_delete_request uri:, params: {}, options: {}
76
+ make_http_request :delete, uri: uri, body: nil, params: params, options: options
77
+ end
78
+
79
+ ##
80
+ # Makes a POST request
81
+ #
82
+ # @param uri [String] uri to send this request to
83
+ # @param body [String] a body to send with the request, nil for requests without a body
84
+ # @param params [Hash] query string parameters for the request
85
+ # @param options [::Gapic::CallOptions] gapic options to be applied to the REST call.
86
+ # Currently only timeout and headers are supported.
87
+ # @return [Faraday::Response]
88
+ def make_post_request uri:, body:, params: {}, options: {}
89
+ make_http_request :post, uri: uri, body: body, params: params, options: options
90
+ end
91
+
92
+ ##
93
+ # Makes a PATCH request
94
+ #
95
+ # @param uri [String] uri to send this request to
96
+ # @param body [String] a body to send with the request, nil for requests without a body
97
+ # @param params [Hash] query string parameters for the request
98
+ # @param options [::Gapic::CallOptions] gapic options to be applied to the REST call.
99
+ # Currently only timeout and headers are supported.
100
+ # @return [Faraday::Response]
101
+ def make_patch_request uri:, body:, params: {}, options: {}
102
+ make_http_request :patch, uri: uri, body: body, params: params, options: options
103
+ end
104
+
105
+ protected
106
+
107
+ ##
108
+ # Sends a http request via Faraday
109
+ # @param verb [Symbol] http verb
110
+ # @param uri [String] uri to send this request to
111
+ # @param body [String, nil] a body to send with the request, nil for requests without a body
112
+ # @param params [Hash] query string parameters for the request
113
+ # @param options [::Gapic::CallOptions] gapic options to be applied to the REST call.
114
+ # Currently only timeout and headers are supported.
115
+ # @return [Faraday::Response]
116
+ def make_http_request verb, uri:, body:, params:, options:
117
+ @connection.send verb, uri do |req|
118
+ req.params = params if params.any?
119
+ req.body = body unless body.nil?
120
+ req.headers = req.headers.merge options.metadata
121
+ req.options.timeout = options.timeout if options.timeout&.positive?
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,73 @@
1
+ # Copyright 2021 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require "json"
16
+
17
+ module Gapic
18
+ module Rest
19
+ # Gapic REST exception class
20
+ class Error < StandardError
21
+ # @return [Integer] the http status code for the error
22
+ attr_reader :status_code
23
+
24
+ ##
25
+ # @param message [String, nil] error message
26
+ # @param status_code [Integer, nil] HTTP status code of this error
27
+ #
28
+ def initialize message, status_code
29
+ @status_code = status_code
30
+ super message
31
+ end
32
+
33
+ class << self
34
+ ##
35
+ # This creates a new error message wrapping the Faraday's one. Additionally
36
+ # it tries to parse and set a detailed message and an error code from
37
+ # from the Google Cloud's response body
38
+ #
39
+ def wrap_faraday_error err
40
+ message = err.message
41
+ status_code = err.response_status
42
+
43
+ if err.response_body
44
+ msg, code = try_parse_from_body err.response_body
45
+ message = "An error has occurred when making a REST request: #{msg}" unless msg.nil?
46
+ status_code = code unless code.nil?
47
+ end
48
+
49
+ Gapic::Rest::Error.new message, status_code
50
+ end
51
+
52
+ private
53
+
54
+ ##
55
+ # Tries to get the error information from the JSON bodies
56
+ #
57
+ # @param body_str [String]
58
+ # @return [Array(String, String)]
59
+ def try_parse_from_body body_str
60
+ body = JSON.parse body_str
61
+ return [nil, nil] unless body && body["error"].is_a?(Hash)
62
+
63
+ message = body["error"]["message"]
64
+ code = body["error"]["code"]
65
+
66
+ [message, code]
67
+ rescue JSON::ParserError
68
+ [nil, nil]
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,45 @@
1
+ # Copyright 2021 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ require "faraday"
15
+
16
+ module Gapic
17
+ module Rest
18
+ # Registers the middleware with Faraday
19
+ module FaradayMiddleware
20
+ ##
21
+ # Request middleware that constructs the Authorization HTTP header
22
+ # using ::Google::Auth::Credentials
23
+ #
24
+ class GoogleAuthorization < Faraday::Middleware
25
+ ##
26
+ # @param app [#call]
27
+ # @param credentials [::Google::Auth::Credentials]
28
+ def initialize app, credentials
29
+ @credentials = credentials
30
+ super app
31
+ end
32
+
33
+ # @param env [Faraday::Env]
34
+ def call env
35
+ auth_hash = @credentials.client.apply({})
36
+ env.request_headers["Authorization"] = auth_hash[:authorization]
37
+
38
+ @app.call env
39
+ end
40
+ end
41
+
42
+ Faraday::Request.register_middleware google_authorization: -> { GoogleAuthorization }
43
+ end
44
+ end
45
+ end
metadata CHANGED
@@ -1,42 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gapic-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google API Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-07 00:00:00.000000000 Z
11
+ date: 2021-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: google-protobuf
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.12'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 3.12.2
19
+ version: '1.3'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '3.12'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 3.12.2
26
+ version: '1.3'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: googleapis-common-protos
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
31
  - - ">="
38
32
  - !ruby/object:Gem::Version
39
- version: 1.3.9
33
+ version: 1.3.11
40
34
  - - "<"
41
35
  - !ruby/object:Gem::Version
42
36
  version: '2.0'
@@ -46,7 +40,7 @@ dependencies:
46
40
  requirements:
47
41
  - - ">="
48
42
  - !ruby/object:Gem::Version
49
- version: 1.3.9
43
+ version: 1.3.11
50
44
  - - "<"
51
45
  - !ruby/object:Gem::Version
52
46
  version: '2.0'
@@ -56,7 +50,7 @@ dependencies:
56
50
  requirements:
57
51
  - - ">="
58
52
  - !ruby/object:Gem::Version
59
- version: 1.0.4
53
+ version: 1.0.6
60
54
  - - "<"
61
55
  - !ruby/object:Gem::Version
62
56
  version: '2.0'
@@ -66,7 +60,7 @@ dependencies:
66
60
  requirements:
67
61
  - - ">="
68
62
  - !ruby/object:Gem::Version
69
- version: 1.0.4
63
+ version: 1.0.6
70
64
  - - "<"
71
65
  - !ruby/object:Gem::Version
72
66
  version: '2.0'
@@ -76,28 +70,54 @@ dependencies:
76
70
  requirements:
77
71
  - - "~>"
78
72
  - !ruby/object:Gem::Version
79
- version: '0.9'
73
+ version: '0.15'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 0.15.1
80
77
  type: :runtime
81
78
  prerelease: false
82
79
  version_requirements: !ruby/object:Gem::Requirement
83
80
  requirements:
84
81
  - - "~>"
85
82
  - !ruby/object:Gem::Version
86
- version: '0.9'
83
+ version: '0.15'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 0.15.1
87
+ - !ruby/object:Gem::Dependency
88
+ name: google-protobuf
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '3.15'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 3.15.2
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.15'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.15.2
87
107
  - !ruby/object:Gem::Dependency
88
108
  name: grpc
89
109
  requirement: !ruby/object:Gem::Requirement
90
110
  requirements:
91
111
  - - "~>"
92
112
  - !ruby/object:Gem::Version
93
- version: '1.25'
113
+ version: '1.36'
94
114
  type: :runtime
95
115
  prerelease: false
96
116
  version_requirements: !ruby/object:Gem::Requirement
97
117
  requirements:
98
118
  - - "~>"
99
119
  - !ruby/object:Gem::Version
100
- version: '1.25'
120
+ version: '1.36'
101
121
  - !ruby/object:Gem::Dependency
102
122
  name: google-cloud-core
103
123
  requirement: !ruby/object:Gem::Requirement
@@ -118,14 +138,14 @@ dependencies:
118
138
  requirements:
119
139
  - - "~>"
120
140
  - !ruby/object:Gem::Version
121
- version: 1.24.0
141
+ version: 1.25.1
122
142
  type: :development
123
143
  prerelease: false
124
144
  version_requirements: !ruby/object:Gem::Requirement
125
145
  requirements:
126
146
  - - "~>"
127
147
  - !ruby/object:Gem::Version
128
- version: 1.24.0
148
+ version: 1.25.1
129
149
  - !ruby/object:Gem::Dependency
130
150
  name: minitest
131
151
  requirement: !ruby/object:Gem::Requirement
@@ -254,6 +274,10 @@ files:
254
274
  - lib/gapic/operation/retry_policy.rb
255
275
  - lib/gapic/paged_enumerable.rb
256
276
  - lib/gapic/protobuf.rb
277
+ - lib/gapic/rest.rb
278
+ - lib/gapic/rest/client_stub.rb
279
+ - lib/gapic/rest/error.rb
280
+ - lib/gapic/rest/faraday_middleware.rb
257
281
  - lib/gapic/stream_input.rb
258
282
  homepage: https://github.com/googleapis/gapic-generator-ruby
259
283
  licenses:
@@ -267,14 +291,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
267
291
  requirements:
268
292
  - - ">="
269
293
  - !ruby/object:Gem::Version
270
- version: '2.4'
294
+ version: '2.5'
271
295
  required_rubygems_version: !ruby/object:Gem::Requirement
272
296
  requirements:
273
297
  - - ">="
274
298
  - !ruby/object:Gem::Version
275
299
  version: '0'
276
300
  requirements: []
277
- rubygems_version: 3.0.3
301
+ rubygems_version: 3.1.4
278
302
  signing_key:
279
303
  specification_version: 4
280
304
  summary: Common code for GAPIC-generated API clients