google-api-client 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +43 -0
- data/README.md +4 -5
- data/bin/google-api +12 -7
- data/lib/google/api_client.rb +179 -161
- data/lib/google/api_client/discovery/api.rb +1 -0
- data/lib/google/api_client/discovery/method.rb +11 -8
- data/lib/google/api_client/discovery/resource.rb +1 -0
- data/lib/google/api_client/discovery/schema.rb +2 -1
- data/lib/google/api_client/environment.rb +15 -0
- data/lib/google/api_client/reference.rb +41 -31
- data/lib/google/api_client/result.rb +5 -10
- data/lib/google/api_client/version.rb +3 -1
- data/spec/google/api_client/discovery_spec.rb +44 -51
- data/spec/google/api_client_spec.rb +19 -10
- data/tasks/gem.rake +7 -6
- data/tasks/git.rake +1 -1
- data/tasks/rdoc.rake +5 -8
- data/tasks/spec.rake +3 -2
- data/tasks/wiki.rake +48 -7
- data/tasks/yard.rake +7 -4
- metadata +11 -17
- data/CHANGELOG +0 -36
- data/lib/google/api_client/parser.rb +0 -59
- data/lib/google/api_client/parsers/json/error_parser.rb +0 -34
- data/lib/google/api_client/parsers/json/pagination.rb +0 -40
- data/lib/google/api_client/parsers/json_parser.rb +0 -119
- data/spec/google/api_client/parsers/json_parser_spec.rb +0 -55
- data/tasks/clobber.rake +0 -2
data/CHANGELOG.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# 0.4.0
|
2
|
+
|
3
|
+
* Replaced httpadapter gem dependency with faraday
|
4
|
+
* Replaced json gem dependency with multi_json
|
5
|
+
* Fixed /dev/null issues on Windows
|
6
|
+
* Repeated parameters now work
|
7
|
+
|
8
|
+
# 0.3.0
|
9
|
+
|
10
|
+
* Updated to use v1 of the discovery API
|
11
|
+
* Updated to use httpadapter 1.0.0
|
12
|
+
* Added OAuth 2 support to the command line tool
|
13
|
+
* Renamed some switches in the command line tool
|
14
|
+
* Added additional configuration capabilities
|
15
|
+
* Fixed a few deprecation warnings from dependencies
|
16
|
+
* Added gemspec to source control
|
17
|
+
|
18
|
+
# 0.2.0
|
19
|
+
|
20
|
+
* Updated to use v1 of the discovery API
|
21
|
+
* Updated to use httpadapter 1.0.0
|
22
|
+
* Added OAuth 2 support to the command line tool
|
23
|
+
* Renamed some switches in the command line tool
|
24
|
+
* Added additional configuration capabilities
|
25
|
+
|
26
|
+
# 0.1.3
|
27
|
+
|
28
|
+
* Added support for manual overrides of the discovery URI
|
29
|
+
* Added support for manual overrides of the API base
|
30
|
+
* Added support for xoauth_requestor_id
|
31
|
+
|
32
|
+
# 0.1.2
|
33
|
+
|
34
|
+
* Added support for two-legged OAuth
|
35
|
+
* Moved some development dependencies into runtime
|
36
|
+
|
37
|
+
# 0.1.1
|
38
|
+
|
39
|
+
* Substantial improvements to the command line interface
|
40
|
+
|
41
|
+
# 0.1.0
|
42
|
+
|
43
|
+
* Initial release
|
data/README.md
CHANGED
@@ -46,14 +46,13 @@ APIs.
|
|
46
46
|
client.authorization.fetch_token_credential!(:verifier => '12345')
|
47
47
|
|
48
48
|
# Discover available methods
|
49
|
-
method_names = client.discovered_api('
|
49
|
+
method_names = client.discovered_api('plus').to_h.keys
|
50
50
|
|
51
51
|
# Make an API call
|
52
|
-
|
53
|
-
'
|
54
|
-
{'
|
52
|
+
result = client.execute(
|
53
|
+
'plus.activities.list',
|
54
|
+
{'collection' => 'public', 'userId' => 'me'}
|
55
55
|
)
|
56
|
-
status, headers, body = response
|
57
56
|
|
58
57
|
# Install
|
59
58
|
|
data/bin/google-api
CHANGED
@@ -10,7 +10,11 @@ OAUTH_SERVER_PORT = 12736
|
|
10
10
|
|
11
11
|
require 'rubygems'
|
12
12
|
require 'optparse'
|
13
|
-
|
13
|
+
|
14
|
+
gem 'faraday', '~> 0.7.0'
|
15
|
+
require 'faraday'
|
16
|
+
require 'faraday/utils'
|
17
|
+
|
14
18
|
require 'webrick'
|
15
19
|
require 'google/api_client/version'
|
16
20
|
require 'google/api_client'
|
@@ -183,6 +187,7 @@ HTML
|
|
183
187
|
end
|
184
188
|
|
185
189
|
def client
|
190
|
+
gem 'signet', '~> 0.3.0'
|
186
191
|
require 'signet/oauth_1/client'
|
187
192
|
require 'yaml'
|
188
193
|
require 'irb'
|
@@ -276,6 +281,7 @@ HTML
|
|
276
281
|
]
|
277
282
|
|
278
283
|
def oauth_1_login
|
284
|
+
gem 'signet', '~> 0.3.0'
|
279
285
|
require 'signet/oauth_1/client'
|
280
286
|
require 'launchy'
|
281
287
|
require 'yaml'
|
@@ -343,6 +349,7 @@ HTML
|
|
343
349
|
end
|
344
350
|
|
345
351
|
def oauth_2_login
|
352
|
+
gem 'signet', '~> 0.3.0'
|
346
353
|
require 'signet/oauth_2/client'
|
347
354
|
require 'launchy'
|
348
355
|
require 'yaml'
|
@@ -365,8 +372,8 @@ HTML
|
|
365
372
|
exit(0)
|
366
373
|
else
|
367
374
|
$verifier = nil
|
368
|
-
|
369
|
-
logger =
|
375
|
+
logger = WEBrick::Log.new
|
376
|
+
logger.level = 0
|
370
377
|
server = WEBrick::HTTPServer.new(
|
371
378
|
:Port => OAUTH_SERVER_PORT,
|
372
379
|
:Logger => logger,
|
@@ -467,8 +474,7 @@ HTML
|
|
467
474
|
request = [method, uri.to_str, headers, [request_body]]
|
468
475
|
request = client.generate_authenticated_request(:request => request)
|
469
476
|
response = client.transmit(request)
|
470
|
-
|
471
|
-
puts body
|
477
|
+
puts response.body
|
472
478
|
exit(0)
|
473
479
|
else
|
474
480
|
# Make request with URI generated from template and parameters
|
@@ -502,8 +508,7 @@ HTML
|
|
502
508
|
:merged_body => request_body,
|
503
509
|
:headers => headers
|
504
510
|
)
|
505
|
-
|
506
|
-
puts body
|
511
|
+
puts result.response.body
|
507
512
|
exit(0)
|
508
513
|
rescue ArgumentError => e
|
509
514
|
puts e.message
|
data/lib/google/api_client.rb
CHANGED
@@ -13,8 +13,10 @@
|
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
15
|
|
16
|
-
|
17
|
-
require '
|
16
|
+
gem 'faraday', '~> 0.7.0'
|
17
|
+
require 'faraday'
|
18
|
+
require 'faraday/utils'
|
19
|
+
require 'multi_json'
|
18
20
|
require 'stringio'
|
19
21
|
|
20
22
|
require 'google/api_client/version'
|
@@ -24,6 +26,7 @@ require 'google/api_client/discovery'
|
|
24
26
|
require 'google/api_client/reference'
|
25
27
|
require 'google/api_client/result'
|
26
28
|
|
29
|
+
|
27
30
|
module Google
|
28
31
|
# TODO(bobaman): Document all this stuff.
|
29
32
|
|
@@ -47,8 +50,9 @@ module Google
|
|
47
50
|
# @option options [String] :host ("www.googleapis.com")
|
48
51
|
# The API hostname used by the client. This rarely needs to be changed.
|
49
52
|
# @option options [String] :application_name
|
50
|
-
# The name
|
51
|
-
#
|
53
|
+
# The name of the application using the client.
|
54
|
+
# @option options [String] :application_version
|
55
|
+
# The version number of the application using the client.
|
52
56
|
# @option options [String] :user_agent
|
53
57
|
# ("{app_name} google-api-ruby-client/{version} {os_name}/{os_version}")
|
54
58
|
# The user agent used by the client. Most developers will want to
|
@@ -63,26 +67,22 @@ module Google
|
|
63
67
|
self.host = options["host"] || 'www.googleapis.com'
|
64
68
|
# Most developers will want to leave this value alone and use the
|
65
69
|
# application_name option.
|
70
|
+
application_string = (
|
71
|
+
options["application_name"] ? (
|
72
|
+
"#{options["application_name"]}/" +
|
73
|
+
"#{options["application_version"] || '0.0.0'}"
|
74
|
+
) : ""
|
75
|
+
)
|
66
76
|
self.user_agent = options["user_agent"] || (
|
67
|
-
|
68
|
-
|
69
|
-
|
77
|
+
"#{application_string} " +
|
78
|
+
"google-api-ruby-client/#{VERSION::STRING} " +
|
79
|
+
ENV::OS_VERSION
|
70
80
|
).strip
|
71
81
|
# The writer method understands a few Symbols and will generate useful
|
72
82
|
# default authentication mechanisms.
|
73
83
|
self.authorization = options["authorization"] || :oauth_2
|
74
84
|
self.key = options["key"]
|
75
85
|
self.user_ip = options["user_ip"]
|
76
|
-
# The HTTP adapter controls all of the HTTP traffic the client generates.
|
77
|
-
# By default, Net::HTTP is used, but adding support for other clients
|
78
|
-
# is trivial.
|
79
|
-
if options["http_adapter"]
|
80
|
-
self.http_adapter = options["http_adapter"]
|
81
|
-
else
|
82
|
-
require 'httpadapter/adapters/net_http'
|
83
|
-
# NOTE: Do not rely on this default value, as it may change
|
84
|
-
self.http_adapter = HTTPAdapter::NetHTTPAdapter.new
|
85
|
-
end
|
86
86
|
@discovery_uris = {}
|
87
87
|
@discovery_documents = {}
|
88
88
|
@discovered_apis = {}
|
@@ -103,6 +103,7 @@ module Google
|
|
103
103
|
def authorization=(new_authorization)
|
104
104
|
case new_authorization
|
105
105
|
when :oauth_1, :oauth
|
106
|
+
gem 'signet', '~> 0.3.0'
|
106
107
|
require 'signet/oauth_1/client'
|
107
108
|
# NOTE: Do not rely on this default value, as it may change
|
108
109
|
new_authorization = Signet::OAuth1::Client.new(
|
@@ -116,6 +117,7 @@ module Google
|
|
116
117
|
:client_credential_secret => 'anonymous'
|
117
118
|
)
|
118
119
|
when :two_legged_oauth_1, :two_legged_oauth
|
120
|
+
gem 'signet', '~> 0.3.0'
|
119
121
|
require 'signet/oauth_1/client'
|
120
122
|
# NOTE: Do not rely on this default value, as it may change
|
121
123
|
new_authorization = Signet::OAuth1::Client.new(
|
@@ -124,6 +126,7 @@ module Google
|
|
124
126
|
:two_legged => true
|
125
127
|
)
|
126
128
|
when :oauth_2
|
129
|
+
gem 'signet', '~> 0.3.0'
|
127
130
|
require 'signet/oauth_2/client'
|
128
131
|
# NOTE: Do not rely on this default value, as it may change
|
129
132
|
new_authorization = Signet::OAuth2::Client.new(
|
@@ -148,7 +151,7 @@ module Google
|
|
148
151
|
##
|
149
152
|
# The application's API key issued by the API console.
|
150
153
|
#
|
151
|
-
# @return [String] The API key
|
154
|
+
# @return [String] The API key.
|
152
155
|
attr_accessor :key
|
153
156
|
|
154
157
|
##
|
@@ -157,28 +160,6 @@ module Google
|
|
157
160
|
# @return [String] The user's IP address.
|
158
161
|
attr_accessor :user_ip
|
159
162
|
|
160
|
-
##
|
161
|
-
# Returns the HTTP adapter used by the client.
|
162
|
-
#
|
163
|
-
# @return [HTTPAdapter]
|
164
|
-
# The HTTP adapter object. The object must include the
|
165
|
-
# HTTPAdapter module and conform to its interface.
|
166
|
-
attr_reader :http_adapter
|
167
|
-
|
168
|
-
##
|
169
|
-
# Returns the HTTP adapter used by the client.
|
170
|
-
#
|
171
|
-
# @return [HTTPAdapter]
|
172
|
-
# The HTTP adapter object. The object must include the
|
173
|
-
# HTTPAdapter module and conform to its interface.
|
174
|
-
def http_adapter=(new_http_adapter)
|
175
|
-
if new_http_adapter.kind_of?(HTTPAdapter)
|
176
|
-
@http_adapter = new_http_adapter
|
177
|
-
else
|
178
|
-
raise TypeError, "Expected HTTPAdapter, got #{new_http_adapter.class}."
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
163
|
##
|
183
164
|
# The API hostname used by the client.
|
184
165
|
#
|
@@ -208,8 +189,8 @@ module Google
|
|
208
189
|
# Manually registers a URI as a discovery document for a specific version
|
209
190
|
# of an API.
|
210
191
|
#
|
211
|
-
# @param [String, Symbol] api The
|
212
|
-
# @param [String] version The desired version of the
|
192
|
+
# @param [String, Symbol] api The API name.
|
193
|
+
# @param [String] version The desired version of the API.
|
213
194
|
# @param [Addressable::URI] uri The URI of the discovery document.
|
214
195
|
def register_discovery_uri(api, version, uri)
|
215
196
|
api = api.to_s
|
@@ -220,8 +201,8 @@ module Google
|
|
220
201
|
##
|
221
202
|
# Returns the URI for the discovery document.
|
222
203
|
#
|
223
|
-
# @param [String, Symbol] api The
|
224
|
-
# @param [String] version The desired version of the
|
204
|
+
# @param [String, Symbol] api The API name.
|
205
|
+
# @param [String] version The desired version of the API.
|
225
206
|
# @return [Addressable::URI] The URI of the discovery document.
|
226
207
|
def discovery_uri(api, version=nil)
|
227
208
|
api = api.to_s
|
@@ -243,8 +224,8 @@ module Google
|
|
243
224
|
# Manually registers a pre-loaded discovery document for a specific version
|
244
225
|
# of an API.
|
245
226
|
#
|
246
|
-
# @param [String, Symbol] api The
|
247
|
-
# @param [String] version The desired version of the
|
227
|
+
# @param [String, Symbol] api The API name.
|
228
|
+
# @param [String] version The desired version of the API.
|
248
229
|
# @param [String, StringIO] discovery_document
|
249
230
|
# The contents of the discovery document.
|
250
231
|
def register_discovery_document(api, version, discovery_document)
|
@@ -260,7 +241,7 @@ module Google
|
|
260
241
|
"Expected String or StringIO, got #{discovery_document.class}."
|
261
242
|
end
|
262
243
|
@discovery_documents["#{api}:#{version}"] =
|
263
|
-
|
244
|
+
MultiJson.decode(discovery_document)
|
264
245
|
end
|
265
246
|
|
266
247
|
##
|
@@ -270,31 +251,25 @@ module Google
|
|
270
251
|
def directory_document
|
271
252
|
return @directory_document ||= (begin
|
272
253
|
request = self.generate_request(
|
273
|
-
:http_method =>
|
254
|
+
:http_method => :get,
|
274
255
|
:uri => self.directory_uri,
|
275
256
|
:authenticated => false
|
276
257
|
)
|
277
|
-
response = self.transmit(request)
|
278
|
-
status
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
258
|
+
response = self.transmit(:request => request)
|
259
|
+
if response.status >= 200 && response.status < 300
|
260
|
+
MultiJson.decode(response.body)
|
261
|
+
elsif response.status >= 400
|
262
|
+
case response.status
|
263
|
+
when 400...500
|
264
|
+
exception_type = ClientError
|
265
|
+
when 500...600
|
266
|
+
exception_type = ServerError
|
267
|
+
else
|
268
|
+
exception_type = TransmissionError
|
284
269
|
end
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
raise ClientError,
|
289
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
290
|
-
elsif status >= 500 && status < 600
|
291
|
-
_, request_uri, _, _ = request
|
292
|
-
raise ServerError,
|
293
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
294
|
-
elsif status > 600
|
295
|
-
_, request_uri, _, _ = request
|
296
|
-
raise TransmissionError,
|
297
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
270
|
+
url = request.to_env(Faraday.default_connection)[:url]
|
271
|
+
raise exception_type,
|
272
|
+
"Could not retrieve directory document at: #{url}"
|
298
273
|
end
|
299
274
|
end)
|
300
275
|
end
|
@@ -302,39 +277,33 @@ module Google
|
|
302
277
|
##
|
303
278
|
# Returns the parsed discovery document.
|
304
279
|
#
|
305
|
-
# @param [String, Symbol] api The
|
306
|
-
# @param [String] version The desired version of the
|
280
|
+
# @param [String, Symbol] api The API name.
|
281
|
+
# @param [String] version The desired version of the API.
|
307
282
|
# @return [Hash] The parsed JSON from the discovery document.
|
308
283
|
def discovery_document(api, version=nil)
|
309
284
|
api = api.to_s
|
310
285
|
version = version || 'v1'
|
311
286
|
return @discovery_documents["#{api}:#{version}"] ||= (begin
|
312
287
|
request = self.generate_request(
|
313
|
-
:http_method =>
|
288
|
+
:http_method => :get,
|
314
289
|
:uri => self.discovery_uri(api, version),
|
315
290
|
:authenticated => false
|
316
291
|
)
|
317
|
-
response = self.transmit(request)
|
318
|
-
status
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
292
|
+
response = self.transmit(:request => request)
|
293
|
+
if response.status >= 200 && response.status < 300
|
294
|
+
MultiJson.decode(response.body)
|
295
|
+
elsif response.status >= 400
|
296
|
+
case response.status
|
297
|
+
when 400...500
|
298
|
+
exception_type = ClientError
|
299
|
+
when 500...600
|
300
|
+
exception_type = ServerError
|
301
|
+
else
|
302
|
+
exception_type = TransmissionError
|
324
303
|
end
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
raise ClientError,
|
329
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
330
|
-
elsif status >= 500 && status < 600
|
331
|
-
_, request_uri, _, _ = request
|
332
|
-
raise ServerError,
|
333
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
334
|
-
elsif status > 600
|
335
|
-
_, request_uri, _, _ = request
|
336
|
-
raise TransmissionError,
|
337
|
-
"Could not retrieve discovery document at: #{request_uri}"
|
304
|
+
url = request.to_env(Faraday.default_connection)[:url]
|
305
|
+
raise exception_type,
|
306
|
+
"Could not retrieve discovery document at: #{url}"
|
338
307
|
end
|
339
308
|
end)
|
340
309
|
end
|
@@ -362,8 +331,8 @@ module Google
|
|
362
331
|
##
|
363
332
|
# Returns the service object for a given service name and service version.
|
364
333
|
#
|
365
|
-
# @param [String, Symbol] api The
|
366
|
-
# @param [String] version The desired version of the
|
334
|
+
# @param [String, Symbol] api The API name.
|
335
|
+
# @param [String] version The desired version of the API.
|
367
336
|
#
|
368
337
|
# @return [Google::APIClient::API] The service object.
|
369
338
|
def discovered_api(api, version=nil)
|
@@ -391,7 +360,8 @@ module Google
|
|
391
360
|
# Returns the method object for a given RPC name and service version.
|
392
361
|
#
|
393
362
|
# @param [String, Symbol] rpc_name The RPC name of the desired method.
|
394
|
-
# @param [String]
|
363
|
+
# @param [String, Symbol] rpc_name The API the method is within.
|
364
|
+
# @param [String] version The desired version of the API.
|
395
365
|
#
|
396
366
|
# @return [Google::APIClient::Method] The method object.
|
397
367
|
def discovered_method(rpc_name, api, version=nil)
|
@@ -426,7 +396,6 @@ module Google
|
|
426
396
|
"Expected String or Symbol, got #{api.class}."
|
427
397
|
end
|
428
398
|
api = api.to_s
|
429
|
-
# TODO(bobaman): Update to use directory API.
|
430
399
|
return self.discovered_apis.detect do |a|
|
431
400
|
a.name == api && a.preferred == true
|
432
401
|
end
|
@@ -435,35 +404,29 @@ module Google
|
|
435
404
|
##
|
436
405
|
# Generates a request.
|
437
406
|
#
|
438
|
-
# @
|
407
|
+
# @option options [Google::APIClient::Method, String] :api_method
|
439
408
|
# The method object or the RPC name of the method being executed.
|
440
|
-
# @
|
409
|
+
# @option options [Hash, Array] :parameters
|
441
410
|
# The parameters to send to the method.
|
442
|
-
# @
|
443
|
-
# @
|
444
|
-
# @
|
445
|
-
# The
|
446
|
-
#
|
447
|
-
#
|
448
|
-
#
|
449
|
-
#
|
450
|
-
#
|
451
|
-
#
|
452
|
-
#
|
453
|
-
#
|
454
|
-
# authenticated, <code>false</code>
|
455
|
-
# otherwise. Defaults to <code>true</code> if an authorization
|
456
|
-
# mechanism has been set, <code>false</code> otherwise.
|
457
|
-
#
|
458
|
-
# @return [Array] The generated request.
|
411
|
+
# @option options [Hash, Array] :headers The HTTP headers for the request.
|
412
|
+
# @option options [String] :body The body of the request.
|
413
|
+
# @option options [String] :version ("v1")
|
414
|
+
# The service version. Only used if `api_method` is a `String`.
|
415
|
+
# @option options [#generate_authenticated_request] :authorization
|
416
|
+
# The authorization mechanism for the response. Used only if
|
417
|
+
# `:authenticated` is `true`.
|
418
|
+
# @option options [TrueClass, FalseClass] :authenticated (true)
|
419
|
+
# `true` if the request must be signed or somehow
|
420
|
+
# authenticated, `false` otherwise.
|
421
|
+
#
|
422
|
+
# @return [Faraday::Request] The generated request.
|
459
423
|
#
|
460
424
|
# @example
|
461
425
|
# request = client.generate_request(
|
462
|
-
# :api_method => '
|
426
|
+
# :api_method => 'plus.activities.list',
|
463
427
|
# :parameters =>
|
464
|
-
# {'
|
428
|
+
# {'collection' => 'public', 'userId' => 'me'}
|
465
429
|
# )
|
466
|
-
# method, uri, headers, body = request
|
467
430
|
def generate_request(options={})
|
468
431
|
# Note: The merge method on a Hash object will coerce an API Reference
|
469
432
|
# object into a Hash and merge with the default options.
|
@@ -471,7 +434,8 @@ module Google
|
|
471
434
|
:version => 'v1',
|
472
435
|
:authorization => self.authorization,
|
473
436
|
:key => self.key,
|
474
|
-
:user_ip => self.user_ip
|
437
|
+
:user_ip => self.user_ip,
|
438
|
+
:connection => Faraday.default_connection
|
475
439
|
}.merge(options)
|
476
440
|
# The Reference object is going to need this to do method ID lookups.
|
477
441
|
options[:client] = self
|
@@ -485,7 +449,10 @@ module Google
|
|
485
449
|
reference = Google::APIClient::Reference.new(options)
|
486
450
|
request = reference.to_request
|
487
451
|
if options[:authenticated]
|
488
|
-
request = self.generate_authenticated_request(
|
452
|
+
request = self.generate_authenticated_request(
|
453
|
+
:request => request,
|
454
|
+
:connection => options[:connection]
|
455
|
+
)
|
489
456
|
end
|
490
457
|
return request
|
491
458
|
end
|
@@ -493,9 +460,9 @@ module Google
|
|
493
460
|
##
|
494
461
|
# Signs a request using the current authorization mechanism.
|
495
462
|
#
|
496
|
-
# @param [Hash] options
|
463
|
+
# @param [Hash] options a customizable set of options
|
497
464
|
#
|
498
|
-
# @return [
|
465
|
+
# @return [Faraday::Request] The signed or otherwise authenticated request.
|
499
466
|
def generate_authenticated_request(options={})
|
500
467
|
return authorization.generate_authenticated_request(options)
|
501
468
|
end
|
@@ -503,14 +470,59 @@ module Google
|
|
503
470
|
##
|
504
471
|
# Transmits the request using the current HTTP adapter.
|
505
472
|
#
|
506
|
-
# @
|
507
|
-
#
|
508
|
-
#
|
509
|
-
#
|
510
|
-
|
473
|
+
# @option options [Array, Faraday::Request] :request
|
474
|
+
# The HTTP request to transmit.
|
475
|
+
# @option options [String, Symbol] :method
|
476
|
+
# The method for the HTTP request.
|
477
|
+
# @option options [String, Addressable::URI] :uri
|
478
|
+
# The URI for the HTTP request.
|
479
|
+
# @option options [Array, Hash] :headers
|
480
|
+
# The headers for the HTTP request.
|
481
|
+
# @option options [String] :body
|
482
|
+
# The body for the HTTP request.
|
483
|
+
# @option options [Faraday::Connection] :connection
|
484
|
+
# The HTTP connection to use.
|
485
|
+
#
|
486
|
+
# @return [Faraday::Response] The response from the server.
|
487
|
+
def transmit(options={})
|
488
|
+
options[:connection] ||= Faraday.default_connection
|
489
|
+
if options[:request]
|
490
|
+
if options[:request].kind_of?(Array)
|
491
|
+
method, uri, headers, body = options[:request]
|
492
|
+
elsif options[:request].kind_of?(Faraday::Request)
|
493
|
+
unless options[:connection]
|
494
|
+
raise ArgumentError,
|
495
|
+
"Faraday::Request used, requires a connection to be provided."
|
496
|
+
end
|
497
|
+
method = options[:request].method.to_s.downcase.to_sym
|
498
|
+
uri = options[:connection].build_url(
|
499
|
+
options[:request].path, options[:request].params
|
500
|
+
)
|
501
|
+
headers = options[:request].headers || {}
|
502
|
+
body = options[:request].body || ''
|
503
|
+
end
|
504
|
+
else
|
505
|
+
method = options[:method] || :get
|
506
|
+
uri = options[:uri]
|
507
|
+
headers = options[:headers] || []
|
508
|
+
body = options[:body] || ''
|
509
|
+
end
|
510
|
+
headers = headers.to_a if headers.kind_of?(Hash)
|
511
|
+
request_components = {
|
512
|
+
:method => method,
|
513
|
+
:uri => uri,
|
514
|
+
:headers => headers,
|
515
|
+
:body => body
|
516
|
+
}
|
517
|
+
# Verify that we have all pieces required to transmit an HTTP request
|
518
|
+
request_components.each do |(key, value)|
|
519
|
+
unless value
|
520
|
+
raise ArgumentError, "Missing :#{key} parameter."
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
511
524
|
if self.user_agent != nil
|
512
525
|
# If there's no User-Agent header, set one.
|
513
|
-
method, uri, headers, body = request
|
514
526
|
unless headers.kind_of?(Enumerable)
|
515
527
|
# We need to use some Enumerable methods, relying on the presence of
|
516
528
|
# the #each method.
|
@@ -527,7 +539,15 @@ module Google
|
|
527
539
|
"Expected User-Agent to be String, got #{self.user_agent.class}"
|
528
540
|
end
|
529
541
|
end
|
530
|
-
|
542
|
+
|
543
|
+
request = Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
|
544
|
+
req.url(Addressable::URI.parse(uri))
|
545
|
+
req.headers = Faraday::Utils::Headers.new(headers)
|
546
|
+
req.body = body
|
547
|
+
end
|
548
|
+
request_env = request.to_env(options[:connection])
|
549
|
+
response = options[:connection].app.call(request_env)
|
550
|
+
return response
|
531
551
|
end
|
532
552
|
|
533
553
|
##
|
@@ -539,29 +559,24 @@ module Google
|
|
539
559
|
# The parameters to send to the method.
|
540
560
|
# @param [String] body The body of the request.
|
541
561
|
# @param [Hash, Array] headers The HTTP headers for the request.
|
542
|
-
# @
|
543
|
-
# The
|
544
|
-
#
|
545
|
-
#
|
546
|
-
#
|
547
|
-
#
|
548
|
-
#
|
549
|
-
#
|
550
|
-
#
|
551
|
-
#
|
552
|
-
# - <code>:authenticated</code> —
|
553
|
-
# <code>true</code> if the request must be signed or otherwise
|
554
|
-
# authenticated, <code>false</code>
|
555
|
-
# otherwise. Defaults to <code>true</code>.
|
556
|
-
#
|
557
|
-
# @return [Array] The response from the API.
|
562
|
+
# @option options [String] :version ("v1")
|
563
|
+
# The service version. Only used if `api_method` is a `String`.
|
564
|
+
# @option options [#generate_authenticated_request] :authorization
|
565
|
+
# The authorization mechanism for the response. Used only if
|
566
|
+
# `:authenticated` is `true`.
|
567
|
+
# @option options [TrueClass, FalseClass] :authenticated (true)
|
568
|
+
# `true` if the request must be signed or somehow
|
569
|
+
# authenticated, `false` otherwise.
|
570
|
+
#
|
571
|
+
# @return [Google::APIClient::Result] The result from the API.
|
558
572
|
#
|
559
573
|
# @example
|
560
|
-
#
|
561
|
-
# :api_method => '
|
562
|
-
# :parameters =>
|
563
|
-
# {'scope' => '@self', 'userId' => '@me', 'alt' => 'json'}
|
574
|
+
# result = client.execute(
|
575
|
+
# :api_method => 'plus.activities.list',
|
576
|
+
# :parameters => {'collection' => 'public', 'userId' => 'me'}
|
564
577
|
# )
|
578
|
+
#
|
579
|
+
# @see Google::APIClient#generate_request
|
565
580
|
def execute(*params)
|
566
581
|
# This block of code allows us to accept multiple parameter passing
|
567
582
|
# styles, and maintaining some backwards compatibility.
|
@@ -574,15 +589,15 @@ module Google
|
|
574
589
|
end
|
575
590
|
options[:api_method] = params.shift if params.size > 0
|
576
591
|
options[:parameters] = params.shift if params.size > 0
|
577
|
-
options[:
|
592
|
+
options[:body] = params.shift if params.size > 0
|
578
593
|
options[:headers] = params.shift if params.size > 0
|
579
594
|
options[:client] = self
|
580
595
|
|
581
596
|
reference = Google::APIClient::Reference.new(options)
|
582
597
|
request = self.generate_request(reference)
|
583
598
|
response = self.transmit(
|
584
|
-
request,
|
585
|
-
options[:
|
599
|
+
:request => request,
|
600
|
+
:connection => options[:connection]
|
586
601
|
)
|
587
602
|
return Google::APIClient::Result.new(reference, request, response)
|
588
603
|
end
|
@@ -594,7 +609,6 @@ module Google
|
|
594
609
|
# @see Google::APIClient#execute
|
595
610
|
def execute!(*params)
|
596
611
|
result = self.execute(*params)
|
597
|
-
status, _, _ = result.response
|
598
612
|
if result.data.respond_to?(:error) &&
|
599
613
|
result.data.error.respond_to?(:message)
|
600
614
|
# You're going to get a terrible error message if the response isn't
|
@@ -603,15 +617,19 @@ module Google
|
|
603
617
|
elsif result.data['error'] && result.data['error']['message']
|
604
618
|
error_message = result.data['error']['message']
|
605
619
|
end
|
606
|
-
if status >= 400
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
620
|
+
if result.response.status >= 400
|
621
|
+
case result.response.status
|
622
|
+
when 400...500
|
623
|
+
exception_type = ClientError
|
624
|
+
error_message ||= "A client error has occurred."
|
625
|
+
when 500...600
|
626
|
+
exception_type = ServerError
|
627
|
+
error_message ||= "A server error has occurred."
|
628
|
+
else
|
629
|
+
exception_type = TransmissionError
|
630
|
+
error_message ||= "A transmission error has occurred."
|
631
|
+
end
|
632
|
+
raise exception_type, error_message
|
615
633
|
end
|
616
634
|
return result
|
617
635
|
end
|