google-api-client 0.6.4 → 0.7.0

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.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +14 -0
  3. data/Gemfile +13 -3
  4. data/README.md +133 -97
  5. data/Rakefile +1 -1
  6. data/lib/cacerts.pem +2183 -0
  7. data/lib/google/api_client/auth/compute_service_account.rb +28 -0
  8. data/lib/google/api_client/auth/file_storage.rb +87 -0
  9. data/lib/google/api_client/auth/installed_app.rb +8 -1
  10. data/lib/google/api_client/auth/jwt_asserter.rb +0 -11
  11. data/lib/google/api_client/client_secrets.rb +1 -1
  12. data/lib/google/api_client/discovery/method.rb +1 -0
  13. data/lib/google/api_client/errors.rb +11 -0
  14. data/lib/google/api_client/gzip.rb +28 -0
  15. data/lib/google/api_client/media.rb +80 -5
  16. data/lib/google/api_client/request.rb +15 -9
  17. data/lib/google/api_client/service/batch.rb +103 -0
  18. data/lib/google/api_client/service/request.rb +144 -0
  19. data/lib/google/api_client/service/resource.rb +40 -0
  20. data/lib/google/api_client/service/result.rb +162 -0
  21. data/lib/google/api_client/service/simple_file_store.rb +151 -0
  22. data/lib/google/api_client/service/stub_generator.rb +59 -0
  23. data/lib/google/api_client/service.rb +233 -0
  24. data/lib/google/api_client/service_account.rb +1 -0
  25. data/lib/google/api_client/version.rb +4 -3
  26. data/lib/google/api_client.rb +81 -42
  27. data/spec/google/api_client/batch_spec.rb +4 -4
  28. data/spec/google/api_client/discovery_spec.rb +60 -102
  29. data/spec/google/api_client/gzip_spec.rb +86 -0
  30. data/spec/google/api_client/media_spec.rb +49 -1
  31. data/spec/google/api_client/request_spec.rb +30 -0
  32. data/spec/google/api_client/result_spec.rb +3 -4
  33. data/spec/google/api_client/service_account_spec.rb +21 -0
  34. data/spec/google/api_client/service_spec.rb +586 -0
  35. data/spec/google/api_client/simple_file_store_spec.rb +137 -0
  36. data/spec/google/api_client_spec.rb +101 -3
  37. data/spec/spec_helper.rb +3 -0
  38. data/tasks/gem.rake +3 -4
  39. metadata +95 -50
  40. data/bin/google-api +0 -391
  41. /data/{CONTRIB.md → CONTRIBUTING.md} +0 -0
@@ -14,10 +14,10 @@
14
14
 
15
15
 
16
16
  require 'faraday'
17
- require 'faraday/utils'
18
17
  require 'multi_json'
19
18
  require 'compat/multi_json'
20
19
  require 'stringio'
20
+ require 'retriable'
21
21
 
22
22
  require 'google/api_client/version'
23
23
  require 'google/api_client/logging'
@@ -30,6 +30,8 @@ require 'google/api_client/result'
30
30
  require 'google/api_client/media'
31
31
  require 'google/api_client/service_account'
32
32
  require 'google/api_client/batch'
33
+ require 'google/api_client/gzip'
34
+ require 'google/api_client/client_secrets'
33
35
  require 'google/api_client/railtie' if defined?(Rails::Railtie)
34
36
 
35
37
  module Google
@@ -70,6 +72,9 @@ module Google
70
72
  # The port number used by the client. This rarely needs to be changed.
71
73
  # @option options [String] :discovery_path ("/discovery/v1")
72
74
  # The discovery base path. This rarely needs to be changed.
75
+ # @option options [String] :ca_file
76
+ # Optional set of root certificates to use when validating SSL connections.
77
+ # By default, a bundled set of trusted roots will be used.
73
78
  def initialize(options={})
74
79
  logger.debug { "#{self.class} - Initializing client with options #{options}" }
75
80
 
@@ -94,8 +99,7 @@ module Google
94
99
  end
95
100
  self.user_agent = options[:user_agent] || (
96
101
  "#{application_string} " +
97
- "google-api-ruby-client/#{Google::APIClient::VERSION::STRING} " +
98
- ENV::OS_VERSION
102
+ "google-api-ruby-client/#{Google::APIClient::VERSION::STRING} #{ENV::OS_VERSION} (gzip)"
99
103
  ).strip
100
104
  # The writer method understands a few Symbols and will generate useful
101
105
  # default authentication mechanisms.
@@ -104,10 +108,18 @@ module Google
104
108
  self.auto_refresh_token = options.fetch(:auto_refresh_token) { true }
105
109
  self.key = options[:key]
106
110
  self.user_ip = options[:user_ip]
111
+ self.retries = options.fetch(:retries) { 0 }
107
112
  @discovery_uris = {}
108
113
  @discovery_documents = {}
109
114
  @discovered_apis = {}
110
-
115
+ ca_file = options[:ca_file] || File.expand_path('../../cacerts.pem', __FILE__)
116
+ self.connection = Faraday.new do |faraday|
117
+ faraday.response :gzip
118
+ faraday.options.params_encoder = Faraday::FlatParamsEncoder
119
+ faraday.ssl.ca_file = ca_file
120
+ faraday.ssl.verify = true
121
+ faraday.adapter Faraday.default_adapter
122
+ end
111
123
  return self
112
124
  end
113
125
 
@@ -167,6 +179,12 @@ module Google
167
179
  return @authorization
168
180
  end
169
181
 
182
+ ##
183
+ # Default Faraday/HTTP connection.
184
+ #
185
+ # @return [Faraday::Connection]
186
+ attr_accessor :connection
187
+
170
188
  ##
171
189
  # The setting that controls whether or not the api client attempts to
172
190
  # refresh authorization when a 401 is hit in #execute.
@@ -214,6 +232,13 @@ module Google
214
232
  # The base path. Should almost always be '/discovery/v1'.
215
233
  attr_accessor :discovery_path
216
234
 
235
+ ##
236
+ # Number of times to retry on recoverable errors
237
+ #
238
+ # @return [FixNum]
239
+ # Number of retries
240
+ attr_accessor :retries
241
+
217
242
  ##
218
243
  # Returns the URI for the directory document.
219
244
  #
@@ -408,6 +433,8 @@ module Google
408
433
  # Verifies an ID token against a server certificate. Used to ensure that
409
434
  # an ID token supplied by an untrusted client-side mechanism is valid.
410
435
  # Raises an error if the token is invalid or missing.
436
+ #
437
+ # @deprecated Use the google-id-token gem for verifying JWTs
411
438
  def verify_id_token!
412
439
  require 'jwt'
413
440
  require 'openssl'
@@ -515,6 +542,10 @@ module Google
515
542
  # - (TrueClass, FalseClass) :authenticated (default: true) -
516
543
  # `true` if the request must be signed or somehow
517
544
  # authenticated, `false` otherwise.
545
+ # - (TrueClass, FalseClass) :gzip (default: true) -
546
+ # `true` if gzip enabled, `false` otherwise.
547
+ # - (FixNum) :retries -
548
+ # # of times to retry on recoverable errors
518
549
  #
519
550
  # @return [Google::APIClient::Result] The result from the API, nil if batch.
520
551
  #
@@ -529,11 +560,10 @@ module Google
529
560
  # )
530
561
  #
531
562
  # @see Google::APIClient#generate_request
532
- def execute(*params)
533
- if params.last.kind_of?(Google::APIClient::Request) &&
534
- params.size == 1
535
- request = params.pop
536
- options = {}
563
+ def execute!(*params)
564
+ if params.first.kind_of?(Google::APIClient::Request)
565
+ request = params.shift
566
+ options = params.shift || {}
537
567
  else
538
568
  # This block of code allows us to accept multiple parameter passing
539
569
  # styles, and maintaining some backwards compatibility.
@@ -554,53 +584,61 @@ module Google
554
584
  end
555
585
 
556
586
  request.headers['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
587
+ request.headers['Accept-Encoding'] ||= 'gzip' unless options[:gzip] == false
588
+ request.headers['Content-Type'] ||= ''
557
589
  request.parameters['key'] ||= self.key unless self.key.nil?
558
590
  request.parameters['userIp'] ||= self.user_ip unless self.user_ip.nil?
559
591
 
560
- connection = options[:connection] || Faraday.default_connection
592
+ connection = options[:connection] || self.connection
561
593
  request.authorization = options[:authorization] || self.authorization unless options[:authenticated] == false
562
-
563
- result = request.send(connection)
564
- if result.status == 401 && request.authorization.respond_to?(:refresh_token) && auto_refresh_token
565
- begin
566
- logger.debug("Attempting refresh of access token & retry of request")
567
- request.authorization.fetch_access_token!
568
- result = request.send(connection)
569
- rescue Signet::AuthorizationError
570
- # Ignore since we want the original error
594
+ tries = 1 + (options[:retries] || self.retries)
595
+ Retriable.retriable :tries => tries,
596
+ :on => [TransmissionError],
597
+ :interval => lambda {|attempts| (2 ** attempts) + rand} do
598
+ result = request.send(connection, true)
599
+
600
+ case result.status
601
+ when 200...300
602
+ result
603
+ when 301, 302, 303, 307
604
+ request = generate_request(request.to_hash.merge({
605
+ :uri => result.headers['location'],
606
+ :api_method => nil
607
+ }))
608
+ raise RedirectError.new(result.headers['location'], result)
609
+ when 400...500
610
+ if result.status == 401 && request.authorization.respond_to?(:refresh_token) && auto_refresh_token
611
+ begin
612
+ logger.debug("Attempting refresh of access token & retry of request")
613
+ request.authorization.fetch_access_token!
614
+ rescue Signet::AuthorizationError
615
+ # Ignore since we want the original error
616
+ end
617
+ end
618
+ raise ClientError.new(result.error_message || "A client error has occurred", result)
619
+ when 500...600
620
+ raise ServerError.new(result.error_message || "A server error has occurred", result)
621
+ else
622
+ raise TransmissionError.new(result.error_message || "A transmission error has occurred", result)
571
623
  end
572
624
  end
573
-
574
- return result
575
625
  end
576
626
 
577
627
  ##
578
- # Same as Google::APIClient#execute, but raises an exception if there was
579
- # an error.
628
+ # Same as Google::APIClient#execute!, but does not raise an exception for
629
+ # normal API errros.
580
630
  #
581
631
  # @see Google::APIClient#execute
582
- def execute!(*params)
583
- result = self.execute(*params)
584
- if result.error?
585
- error_message = result.error_message
586
- case result.response.status
587
- when 400...500
588
- exception_type = ClientError
589
- error_message ||= "A client error has occurred."
590
- when 500...600
591
- exception_type = ServerError
592
- error_message ||= "A server error has occurred."
593
- else
594
- exception_type = TransmissionError
595
- error_message ||= "A transmission error has occurred."
596
- end
597
- raise exception_type, error_message
632
+ def execute(*params)
633
+ begin
634
+ return self.execute!(*params)
635
+ rescue TransmissionError => e
636
+ return e.result
598
637
  end
599
- return result
600
638
  end
601
-
639
+
602
640
  protected
603
-
641
+
604
642
  ##
605
643
  # Resolves a URI template against the client's configured base.
606
644
  #
@@ -628,6 +666,7 @@ module Google
628
666
  end
629
667
 
630
668
  end
669
+
631
670
  end
632
671
 
633
672
  require 'google/api_client/version'
@@ -53,7 +53,7 @@ describe Google::APIClient::BatchRequest do
53
53
  @call1 = {
54
54
  :api_method => @discovery.apis.get_rest,
55
55
  :parameters => {
56
- 'api' => 'adsense',
56
+ 'api' => 'plus',
57
57
  'version' => 'v1'
58
58
  }
59
59
  }
@@ -118,7 +118,7 @@ describe Google::APIClient::BatchRequest do
118
118
  @call1 = {
119
119
  :api_method => @discovery.apis.get_rest,
120
120
  :parameters => {
121
- 'api' => 'adsense',
121
+ 'api' => 'plus',
122
122
  'version' => 'v1'
123
123
  }
124
124
  }
@@ -234,7 +234,7 @@ describe Google::APIClient::BatchRequest do
234
234
  it 'should convert to a correct HTTP request' do
235
235
  batch = Google::APIClient::BatchRequest.new { |result| }
236
236
  batch.add(@call1, '1').add(@call2, '2')
237
- request = batch.to_env(Faraday.default_connection)
237
+ request = batch.to_env(CLIENT.connection)
238
238
  boundary = Google::APIClient::BatchRequest::BATCH_BOUNDARY
239
239
  request[:method].to_s.downcase.should == 'post'
240
240
  request[:url].to_s.should == 'https://www.googleapis.com/batch'
@@ -244,6 +244,6 @@ describe Google::APIClient::BatchRequest do
244
244
  #request[:body].read.gsub("\r", "").should =~ expected_body
245
245
  end
246
246
  end
247
-
247
+
248
248
  end
249
249
  end
@@ -18,29 +18,12 @@
18
18
  require 'spec_helper'
19
19
 
20
20
  require 'faraday'
21
- require 'faraday/utils'
22
21
  require 'multi_json'
23
22
  require 'compat/multi_json'
24
23
  require 'signet/oauth_1/client'
25
24
  require 'google/api_client'
26
25
  require 'google/api_client/version'
27
26
 
28
- def TestHandler
29
- def initialize(&block)
30
- @block = block
31
- end
32
-
33
- def call(env)
34
- @block.call(env)
35
- end
36
- end
37
-
38
- def mock_connection(&block)
39
- connection = Faraday.new do |builder|
40
- use TestHandler block
41
- end
42
- end
43
-
44
27
  describe Google::APIClient do
45
28
  include ConnectionHelpers
46
29
  CLIENT = Google::APIClient.new(:application_name => 'API Client Tests') unless defined?(CLIENT)
@@ -103,9 +86,10 @@ describe Google::APIClient do
103
86
 
104
87
  it 'should correctly determine the discovery URI if :user_ip is set' do
105
88
  CLIENT.user_ip = '127.0.0.1'
106
-
89
+
107
90
  conn = stub_connection do |stub|
108
91
  stub.get('/discovery/v1/apis/prediction/v1.2/rest?userIp=127.0.0.1') do |env|
92
+ [200, {}, '{}']
109
93
  end
110
94
  end
111
95
  CLIENT.execute(
@@ -121,6 +105,7 @@ describe Google::APIClient do
121
105
  CLIENT.key = 'qwerty'
122
106
  conn = stub_connection do |stub|
123
107
  stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty') do |env|
108
+ [200, {}, '{}']
124
109
  end
125
110
  end
126
111
  request = CLIENT.execute(
@@ -137,6 +122,7 @@ describe Google::APIClient do
137
122
  CLIENT.user_ip = '127.0.0.1'
138
123
  conn = stub_connection do |stub|
139
124
  stub.get('/discovery/v1/apis/prediction/v1.2/rest?key=qwerty&userIp=127.0.0.1') do |env|
125
+ [200, {}, '{}']
140
126
  end
141
127
  end
142
128
  request = CLIENT.execute(
@@ -204,6 +190,7 @@ describe Google::APIClient do
204
190
  conn = stub_connection do |stub|
205
191
  stub.post('/prediction/v1.2/training?data=12345') do |env|
206
192
  env[:body].should == ''
193
+ [200, {}, '{}']
207
194
  end
208
195
  end
209
196
  request = CLIENT.execute(
@@ -216,11 +203,12 @@ describe Google::APIClient do
216
203
 
217
204
  it 'should generate valid requests when parameter value includes semicolon' do
218
205
  conn = stub_connection do |stub|
219
- # semicolon (;) in parameter value was being converted to
220
- # bare ampersand (&) in 0.4.7. ensure that it gets converted
206
+ # semicolon (;) in parameter value was being converted to
207
+ # bare ampersand (&) in 0.4.7. ensure that it gets converted
221
208
  # to a CGI-escaped semicolon (%3B) instead.
222
209
  stub.post('/prediction/v1.2/training?data=12345%3B67890') do |env|
223
210
  env[:body].should == ''
211
+ [200, {}, '{}']
224
212
  end
225
213
  end
226
214
  request = CLIENT.execute(
@@ -231,16 +219,16 @@ describe Google::APIClient do
231
219
  conn.verify
232
220
  end
233
221
 
234
- it 'should generate valid requests when repeated parameters are passed' do
235
- pending("This is caused by Faraday's encoding of query parameters.")
222
+ it 'should generate valid requests when multivalued parameters are passed' do
236
223
  conn = stub_connection do |stub|
237
224
  stub.post('/prediction/v1.2/training?data=1&data=2') do |env|
238
- env[:params]['data'].should include('1', '2')
225
+ env.params['data'].should include('1', '2')
226
+ [200, {}, '{}']
239
227
  end
240
228
  end
241
229
  request = CLIENT.execute(
242
230
  :api_method => @prediction.training.insert,
243
- :parameters => [['data', '1'], ['data','2']],
231
+ :parameters => {'data' => ['1', '2']},
244
232
  :connection => conn
245
233
  )
246
234
  conn.verify
@@ -249,6 +237,7 @@ describe Google::APIClient do
249
237
  it 'should generate requests against the correct URIs' do
250
238
  conn = stub_connection do |stub|
251
239
  stub.post('/prediction/v1.2/training?data=12345') do |env|
240
+ [200, {}, '{}']
252
241
  end
253
242
  end
254
243
  request = CLIENT.execute(
@@ -262,6 +251,7 @@ describe Google::APIClient do
262
251
  it 'should generate requests against the correct URIs' do
263
252
  conn = stub_connection do |stub|
264
253
  stub.post('/prediction/v1.2/training?data=12345') do |env|
254
+ [200, {}, '{}']
265
255
  end
266
256
  end
267
257
  request = CLIENT.execute(
@@ -282,9 +272,10 @@ describe Google::APIClient do
282
272
  conn = stub_connection do |stub|
283
273
  stub.post('/prediction/v1.2/training') do |env|
284
274
  env[:url].host.should == 'testing-domain.example.com'
275
+ [200, {}, '{}']
285
276
  end
286
277
  end
287
-
278
+
288
279
  request = CLIENT.execute(
289
280
  :api_method => prediction_rebase.training.insert,
290
281
  :parameters => {'data' => '123'},
@@ -302,6 +293,7 @@ describe Google::APIClient do
302
293
  stub.post('/prediction/v1.2/training?data=12345') do |env|
303
294
  env[:request_headers].should have_key('Authorization')
304
295
  env[:request_headers]['Authorization'].should =~ /^OAuth/
296
+ [200, {}, '{}']
305
297
  end
306
298
  end
307
299
 
@@ -321,6 +313,7 @@ describe Google::APIClient do
321
313
  stub.post('/prediction/v1.2/training?data=12345') do |env|
322
314
  env[:request_headers].should have_key('Authorization')
323
315
  env[:request_headers]['Authorization'].should =~ /^Bearer/
316
+ [200, {}, '{}']
324
317
  end
325
318
  end
326
319
 
@@ -381,6 +374,7 @@ describe Google::APIClient do
381
374
  stub.post('/prediction/v1.2/training') do |env|
382
375
  env[:request_headers].should have_key('Content-Type')
383
376
  env[:request_headers]['Content-Type'].should == 'application/json'
377
+ [200, {}, '{}']
384
378
  end
385
379
  end
386
380
  CLIENT.authorization = :oauth_2
@@ -433,9 +427,10 @@ describe Google::APIClient do
433
427
  it 'should generate requests against the correct URIs' do
434
428
  conn = stub_connection do |stub|
435
429
  stub.get('/plus/v1/people/107807692475771887386/activities/public') do |env|
430
+ [200, {}, '{}']
436
431
  end
437
432
  end
438
-
433
+
439
434
  request = CLIENT.execute(
440
435
  :api_method => @plus.activities.list,
441
436
  :parameters => {
@@ -465,107 +460,45 @@ describe Google::APIClient do
465
460
  'userId' => '107807692475771887386', 'collection' => 'bogus'
466
461
  },
467
462
  :authenticated => false
468
- ).to_env(Faraday.default_connection)
463
+ ).to_env(CLIENT.connection)
469
464
  end).should raise_error(ArgumentError)
470
465
  end
471
466
  end
472
-
473
- describe 'with the latitude API' do
474
- before do
475
- CLIENT.authorization = nil
476
- @latitude = CLIENT.discovered_api('latitude')
477
- end
478
-
479
- it 'should correctly determine the discovery URI' do
480
- CLIENT.discovery_uri('latitude').should ===
481
- 'https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest'
482
- end
483
-
484
- it 'should find APIs that are in the discovery document' do
485
- CLIENT.discovered_api('latitude').name.should == 'latitude'
486
- CLIENT.discovered_api('latitude').version.should == 'v1'
487
- end
488
-
489
- it 'should return a batch path' do
490
- CLIENT.discovered_api('latitude').batch_path.should_not be_nil
491
- end
492
-
493
- it 'should find methods that are in the discovery document' do
494
- CLIENT.discovered_method(
495
- 'latitude.currentLocation.get', 'latitude'
496
- ).name.should == 'get'
497
- end
498
-
499
- it 'should define the origin API in discovered methods' do
500
- CLIENT.discovered_method(
501
- 'latitude.currentLocation.get', 'latitude'
502
- ).api.name.should == 'latitude'
503
- end
504
-
505
- it 'should not find methods that are not in the discovery document' do
506
- CLIENT.discovered_method('latitude.bogus', 'latitude').should == nil
507
- end
508
-
509
- it 'should generate requests against the correct URIs' do
510
- request = CLIENT.generate_request(
511
- :api_method => @latitude.current_location.get,
512
- :authenticated => false
513
- )
514
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
515
- 'https://www.googleapis.com/latitude/v1/currentLocation'
516
- end
517
-
518
- it 'should generate requests against the correct URIs' do
519
- request = CLIENT.generate_request(
520
- :api_method => @latitude.current_location.get,
521
- :authenticated => false
522
- )
523
- request.to_env(Faraday.default_connection)[:url].to_s.should ===
524
- 'https://www.googleapis.com/latitude/v1/currentLocation'
525
- end
526
-
527
- it 'should not be able to execute requests without authorization' do
528
- result = CLIENT.execute(
529
- :api_method => @latitude.current_location.get,
530
- :authenticated => false
531
- )
532
- result.response.status.should == 401
533
- end
534
- end
535
467
 
536
468
  describe 'with the adsense API' do
537
469
  before do
538
470
  CLIENT.authorization = nil
539
- @adsense = CLIENT.discovered_api('adsense', 'v1')
471
+ @adsense = CLIENT.discovered_api('adsense', 'v1.3')
540
472
  end
541
473
 
542
474
  it 'should correctly determine the discovery URI' do
543
- CLIENT.discovery_uri('adsense').should ===
544
- 'https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest'
475
+ CLIENT.discovery_uri('adsense', 'v1.3').to_s.should ===
476
+ 'https://www.googleapis.com/discovery/v1/apis/adsense/v1.3/rest'
545
477
  end
546
478
 
547
479
  it 'should find APIs that are in the discovery document' do
548
- CLIENT.discovered_api('adsense').name.should == 'adsense'
549
- CLIENT.discovered_api('adsense').version.should == 'v1'
480
+ CLIENT.discovered_api('adsense', 'v1.3').name.should == 'adsense'
481
+ CLIENT.discovered_api('adsense', 'v1.3').version.should == 'v1.3'
550
482
  end
551
483
 
552
484
  it 'should return a batch path' do
553
- CLIENT.discovered_api('adsense').batch_path.should_not be_nil
485
+ CLIENT.discovered_api('adsense', 'v1.3').batch_path.should_not be_nil
554
486
  end
555
487
 
556
488
  it 'should find methods that are in the discovery document' do
557
489
  CLIENT.discovered_method(
558
- 'adsense.reports.generate', 'adsense'
490
+ 'adsense.reports.generate', 'adsense', 'v1.3'
559
491
  ).name.should == 'generate'
560
492
  end
561
493
 
562
494
  it 'should not find methods that are not in the discovery document' do
563
- CLIENT.discovered_method('adsense.bogus', 'adsense').should == nil
495
+ CLIENT.discovered_method('adsense.bogus', 'adsense', 'v1.3').should == nil
564
496
  end
565
497
 
566
498
  it 'should generate requests against the correct URIs' do
567
499
  conn = stub_connection do |stub|
568
- stub.get('/adsense/v1/adclients') do |env|
500
+ stub.get('/adsense/v1.3/adclients') do |env|
501
+ [200, {}, '{}']
569
502
  end
570
503
  end
571
504
  request = CLIENT.execute(
@@ -595,7 +528,8 @@ describe Google::APIClient do
595
528
 
596
529
  it 'should succeed when validating parameters in a correct call' do
597
530
  conn = stub_connection do |stub|
598
- stub.get('/adsense/v1/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
531
+ stub.get('/adsense/v1.3/reports?dimension=DATE&endDate=2010-01-01&metric=PAGE_VIEWS&startDate=2000-01-01') do |env|
532
+ [200, {}, '{}']
599
533
  end
600
534
  end
601
535
  (lambda do
@@ -630,11 +564,11 @@ describe Google::APIClient do
630
564
  end
631
565
 
632
566
  it 'should succeed when validating repeated parameters in a correct call' do
633
- pending("This is caused by Faraday's encoding of query parameters.")
634
567
  conn = stub_connection do |stub|
635
- stub.get('/adsense/v1/reports?dimension=DATE&dimension=PRODUCT_CODE'+
568
+ stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
636
569
  '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
637
570
  'startDate=2000-01-01') do |env|
571
+ [200, {}, '{}']
638
572
  end
639
573
  end
640
574
  (lambda do
@@ -667,6 +601,30 @@ describe Google::APIClient do
667
601
  )
668
602
  end).should raise_error(ArgumentError)
669
603
  end
604
+
605
+ it 'should generate valid requests when multivalued parameters are passed' do
606
+ conn = stub_connection do |stub|
607
+ stub.get('/adsense/v1.3/reports?dimension=DATE&dimension=PRODUCT_CODE'+
608
+ '&endDate=2010-01-01&metric=CLICKS&metric=PAGE_VIEWS&'+
609
+ 'startDate=2000-01-01') do |env|
610
+ env.params['dimension'].should include('DATE', 'PRODUCT_CODE')
611
+ env.params['metric'].should include('CLICKS', 'PAGE_VIEWS')
612
+ [200, {}, '{}']
613
+ end
614
+ end
615
+ request = CLIENT.execute(
616
+ :api_method => @adsense.reports.generate,
617
+ :parameters => {
618
+ 'startDate' => '2000-01-01',
619
+ 'endDate' => '2010-01-01',
620
+ 'dimension' => ['DATE', 'PRODUCT_CODE'],
621
+ 'metric' => ['PAGE_VIEWS', 'CLICKS']
622
+ },
623
+ :authenticated => false,
624
+ :connection => conn
625
+ )
626
+ conn.verify
627
+ end
670
628
  end
671
629
 
672
630
  describe 'with the Drive API' do
@@ -0,0 +1,86 @@
1
+ # Copyright 2012 Google Inc.
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
+ # http://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 'spec_helper'
16
+
17
+ require 'google/api_client'
18
+ require 'google/api_client/version'
19
+
20
+ describe Google::APIClient::Gzip do
21
+
22
+ def create_connection(&block)
23
+ Faraday.new do |b|
24
+ b.response :gzip
25
+ b.adapter :test do |stub|
26
+ stub.get '/', &block
27
+ end
28
+ end
29
+ end
30
+
31
+ it 'should ignore non-zipped content' do
32
+ conn = create_connection do |env|
33
+ [200, {}, 'Hello world']
34
+ end
35
+ result = conn.get('/')
36
+ result.body.should == "Hello world"
37
+ end
38
+
39
+ it 'should decompress gziped content' do
40
+ conn = create_connection do |env|
41
+ [200, { 'Content-Encoding' => 'gzip'}, Base64.decode64('H4sICLVGwlEAA3RtcADzSM3JyVcozy/KSeECANXgObcMAAAA')]
42
+ end
43
+ result = conn.get('/')
44
+ result.body.should == "Hello world\n"
45
+ end
46
+
47
+ describe 'with API Client' do
48
+
49
+ before do
50
+ @client = Google::APIClient.new(:application_name => 'test')
51
+ @client.authorization = nil
52
+ end
53
+
54
+
55
+ it 'should send gzip in user agent' do
56
+ conn = create_connection do |env|
57
+ agent = env[:request_headers]['User-Agent']
58
+ agent.should_not be_nil
59
+ agent.should include 'gzip'
60
+ [200, {}, 'Hello world']
61
+ end
62
+ @client.execute(:uri => 'http://www.example.com/', :connection => conn)
63
+ end
64
+
65
+ it 'should send gzip in accept-encoding' do
66
+ conn = create_connection do |env|
67
+ encoding = env[:request_headers]['Accept-Encoding']
68
+ encoding.should_not be_nil
69
+ encoding.should include 'gzip'
70
+ [200, {}, 'Hello world']
71
+ end
72
+ @client.execute(:uri => 'http://www.example.com/', :connection => conn)
73
+ end
74
+
75
+ it 'should not send gzip in accept-encoding if disabled for request' do
76
+ conn = create_connection do |env|
77
+ encoding = env[:request_headers]['Accept-Encoding']
78
+ encoding.should_not include('gzip') unless encoding.nil?
79
+ [200, {}, 'Hello world']
80
+ end
81
+ response = @client.execute(:uri => 'http://www.example.com/', :gzip => false, :connection => conn)
82
+ puts response.status
83
+ end
84
+
85
+ end
86
+ end