google-api-client 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.1.2
2
+
3
+ * added support for two-legged OAuth
4
+ * moved some development dependencies into runtime
5
+
1
6
  == 0.1.1
2
7
 
3
8
  * substantial improvements to the command line interface
@@ -91,6 +91,16 @@ HTML
91
91
  "--scope <scope>", String, "Set the OAuth scope") do |s|
92
92
  options[:scope] = s
93
93
  end
94
+ opts.on(
95
+ "--client-key <key>", String,
96
+ "Set the 2-legged OAuth key") do |k|
97
+ options[:client_credential_key] = k
98
+ end
99
+ opts.on(
100
+ "--client-secret <secret>", String,
101
+ "Set the 2-legged OAuth secret") do |s|
102
+ options[:client_credential_secret] = s
103
+ end
94
104
  opts.on(
95
105
  "-s", "--service <name>", String,
96
106
  "Perform discovery on service") do |s|
@@ -117,6 +127,16 @@ HTML
117
127
  end
118
128
  options[:content_type] = f
119
129
  end
130
+ opts.on(
131
+ "-u", "--uri <uri>", String,
132
+ "Sets the URI to perform a request against") do |u|
133
+ options[:uri] = u
134
+ end
135
+ opts.on(
136
+ "-m", "--method <method>", String,
137
+ "Sets the HTTP method to use for the request") do |m|
138
+ options[:http_method] = m
139
+ end
120
140
 
121
141
  opts.on("-v", "--verbose", "Run verbosely") do |v|
122
142
  options[:verbose] = v
@@ -132,76 +152,109 @@ HTML
132
152
 
133
153
  opts.separator(
134
154
  "\nAvailable commands:\n" +
135
- " oauth-login Log a user into an API\n" +
136
- " list List the methods available for a service\n" +
137
- " execute Execute a method on the API\n" +
138
- " irb Start an interactive client session"
155
+ " oauth-login Log a user into an API\n" +
156
+ " list List the methods available for a service\n" +
157
+ " execute Execute a method on the API\n" +
158
+ " irb Start an interactive client session"
139
159
  )
140
160
  end
141
161
  end
142
162
 
143
163
  def parse!
144
164
  self.parser.parse!(self.argv)
145
- self.send(self.command.gsub(/-/, "_").to_sym)
165
+ symbol = self.command.gsub(/-/, "_").to_sym
166
+ if !COMMANDS.include?(symbol)
167
+ STDERR.puts("Invalid command: #{self.command}")
168
+ exit(1)
169
+ end
170
+ self.send(symbol)
146
171
  end
147
172
 
173
+ COMMANDS = [
174
+ :oauth_login,
175
+ :list,
176
+ :execute,
177
+ :irb,
178
+ :fuzz
179
+ ]
180
+
148
181
  def oauth_login
149
182
  require 'signet/oauth_1/client'
150
183
  require 'launchy'
151
184
  require 'yaml'
152
- $verifier = nil
153
- logger = WEBrick::Log.new('/dev/null') # TODO(bobaman): Cross-platform?
154
- server = WEBrick::HTTPServer.new(
155
- :Port => OAUTH_SERVER_PORT,
156
- :Logger => logger,
157
- :AccessLog => logger
158
- )
159
- trap("INT") { server.shutdown }
160
-
161
- server.mount("/", OAuthVerifierServlet)
162
-
163
- oauth_client = Signet::OAuth1::Client.new(
164
- :temporary_credential_uri =>
165
- 'https://www.google.com/accounts/OAuthGetRequestToken',
166
- :authorization_uri =>
167
- 'https://www.google.com/accounts/OAuthAuthorizeToken',
168
- :token_credential_uri =>
169
- 'https://www.google.com/accounts/OAuthGetAccessToken',
170
- :client_credential_key => 'anonymous',
171
- :client_credential_secret => 'anonymous',
172
- :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
173
- )
174
- scope = options[:scope]
175
- # Special cases
176
- case scope
177
- when "https://www.googleapis.com/auth/buzz",
178
- "https://www.googleapis.com/auth/buzz.readonly"
179
- oauth_client.authorization_uri =
180
- 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
181
- "domain=#{oauth_client.client_credential_key}&" +
182
- "scope=#{scope}&" +
183
- "xoauth_displayname=Google%20API%20Client"
185
+ if options[:client_credential_key] &&
186
+ options[:client_credential_secret]
187
+ scope = options[:scope]
188
+ config = {
189
+ "scope" => nil,
190
+ "client_credential_key" => options[:client_credential_key],
191
+ "client_credential_secret" => options[:client_credential_secret],
192
+ "token_credential_key" => nil,
193
+ "token_credential_secret" => nil
194
+ }
195
+ config_file = File.expand_path('~/.google-api.yaml')
196
+ open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
197
+ exit(0)
198
+ else
199
+ $verifier = nil
200
+ # TODO(bobaman): Cross-platform?
201
+ logger = WEBrick::Log.new('/dev/null')
202
+ server = WEBrick::HTTPServer.new(
203
+ :Port => OAUTH_SERVER_PORT,
204
+ :Logger => logger,
205
+ :AccessLog => logger
206
+ )
207
+ trap("INT") { server.shutdown }
208
+
209
+ server.mount("/", OAuthVerifierServlet)
210
+
211
+ oauth_client = Signet::OAuth1::Client.new(
212
+ :temporary_credential_uri =>
213
+ 'https://www.google.com/accounts/OAuthGetRequestToken',
214
+ :authorization_uri =>
215
+ 'https://www.google.com/accounts/OAuthAuthorizeToken',
216
+ :token_credential_uri =>
217
+ 'https://www.google.com/accounts/OAuthGetAccessToken',
218
+ :client_credential_key => 'anonymous',
219
+ :client_credential_secret => 'anonymous',
220
+ :callback => "http://localhost:#{OAUTH_SERVER_PORT}/"
221
+ )
222
+ scope = options[:scope]
223
+ # Special cases
224
+ case scope
225
+ when "https://www.googleapis.com/auth/buzz",
226
+ "https://www.googleapis.com/auth/buzz.readonly"
227
+ oauth_client.authorization_uri =
228
+ 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken?' +
229
+ "domain=#{oauth_client.client_credential_key}&" +
230
+ "scope=#{scope}&" +
231
+ "xoauth_displayname=Google%20API%20Client"
232
+ end
233
+ oauth_client.fetch_temporary_credential!(:additional_parameters => {
234
+ :scope => scope,
235
+ :xoauth_displayname => 'Google API Client'
236
+ })
237
+
238
+ # Launch browser
239
+ Launchy::Browser.run(oauth_client.authorization_uri.to_s)
240
+
241
+ server.start
242
+ oauth_client.fetch_token_credential!(:verifier => $verifier)
243
+ config = {
244
+ "scope" => scope,
245
+ "client_credential_key" =>
246
+ oauth_client.client_credential_key,
247
+ "client_credential_secret" =>
248
+ oauth_client.client_credential_secret,
249
+ "token_credential_key" =>
250
+ oauth_client.token_credential_key,
251
+ "token_credential_secret" =>
252
+ oauth_client.token_credential_secret
253
+ }
254
+ config_file = File.expand_path('~/.google-api.yaml')
255
+ open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
256
+ exit(0)
184
257
  end
185
- oauth_client.fetch_temporary_credential!(:additional_parameters => {
186
- :scope => scope,
187
- :xoauth_displayname => 'Google API Client'
188
- })
189
-
190
- # Launch browser
191
- Launchy::Browser.run(oauth_client.authorization_uri.to_s)
192
-
193
- server.start
194
- oauth_client.fetch_token_credential!(:verifier => $verifier)
195
- config = {
196
- "scope" => scope,
197
- "client_credential_key" => oauth_client.client_credential_key,
198
- "client_credential_secret" => oauth_client.client_credential_secret,
199
- "token_credential_key" => oauth_client.token_credential_key,
200
- "token_credential_secret" => oauth_client.token_credential_secret
201
- }
202
- config_file = File.expand_path('~/.google-api.yaml')
203
- open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
204
- exit(0)
205
258
  end
206
259
 
207
260
  def list
@@ -224,16 +277,20 @@ HTML
224
277
  require 'yaml'
225
278
  config_file = File.expand_path('~/.google-api.yaml')
226
279
  signed = File.exist?(config_file)
227
- if !self.rpcname
228
- STDERR.puts('No rpcname supplied.')
229
- exit(1)
280
+
281
+ # Setup HTTP request data
282
+ request_body = ''
283
+ input_streams, _, _ = IO.select([STDIN], [], [], 0)
284
+ request_body = STDIN.read || '' if input_streams
285
+ headers = []
286
+ if options[:content_type]
287
+ headers << ['Content-Type', options[:content_type]]
288
+ elsif request_body
289
+ # Default to JSON
290
+ headers << ['Content-Type', 'application/json']
230
291
  end
231
- service_name = options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
232
- client = Google::APIClient.new(
233
- :service => service_name,
234
- :authorization => :oauth_1
235
- )
236
- if signed
292
+
293
+ configure_authorization = lambda do |client|
237
294
  if !client.authorization.kind_of?(Signet::OAuth1::Client)
238
295
  STDERR.puts(
239
296
  "Unexpected authorization mechanism: " +
@@ -251,43 +308,66 @@ HTML
251
308
  client.authorization.token_credential_secret =
252
309
  config["token_credential_secret"]
253
310
  end
254
- service_version =
255
- options[:service_version] ||
256
- client.latest_service_version(service_name).version
257
- service = client.discovered_service(service_name, service_version)
258
- method = service.to_h[self.rpcname]
259
- if !method
260
- STDERR.puts(
261
- "Method #{self.rpcname} does not exist for " +
262
- "#{service_name}-#{service_version}."
263
- )
264
- exit(1)
265
- end
266
- parameters = self.argv.inject({}) do |accu, pair|
267
- name, value = pair.split('=', 2)
268
- accu[name] = value
269
- accu
270
- end
271
- request_body = ''
272
- input_streams, _, _ = IO.select([STDIN], [], [], 0)
273
- request_body = STDIN.read || '' if input_streams
274
- headers = []
275
- if options[:content_type]
276
- headers << ['Content-Type', options[:content_type]]
277
- elsif request_body
278
- # Default to JSON
279
- headers << ['Content-Type', 'application/json']
280
- end
281
- begin
282
- response = client.execute(
283
- method, parameters, request_body, headers, {:signed => signed}
284
- )
311
+
312
+ if options[:uri]
313
+ # Make request with URI manually specified
314
+ uri = Addressable::URI.parse(options[:uri])
315
+ if uri.relative?
316
+ STDERR.puts('URI may not be relative.')
317
+ exit(1)
318
+ end
319
+ method = options[:http_method]
320
+ method ||= request_body == '' ? 'GET' : 'POST'
321
+ method.upcase!
322
+ client = Google::APIClient.new(:authorization => :two_legged_oauth_1)
323
+ configure_authorization.call(client) if signed
324
+ request = [method, uri.to_str, headers, [request_body]]
325
+ request = client.sign_request(request)
326
+ response = client.transmit_request(request)
285
327
  status, headers, body = response
286
328
  puts body
287
329
  exit(0)
288
- rescue ArgumentError => e
289
- puts e.message
290
- exit(1)
330
+ else
331
+ # Make request with URI generated from template and parameters
332
+ if !self.rpcname
333
+ STDERR.puts('No rpcname supplied.')
334
+ exit(1)
335
+ end
336
+ service_name =
337
+ options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
338
+ client = Google::APIClient.new(
339
+ :service => service_name,
340
+ :authorization => :oauth_1
341
+ )
342
+ configure_authorization.call(client) if signed
343
+ service_version =
344
+ options[:service_version] ||
345
+ client.latest_service_version(service_name).version
346
+ service = client.discovered_service(service_name, service_version)
347
+ method = service.to_h[self.rpcname]
348
+ if !method
349
+ STDERR.puts(
350
+ "Method #{self.rpcname} does not exist for " +
351
+ "#{service_name}-#{service_version}."
352
+ )
353
+ exit(1)
354
+ end
355
+ parameters = self.argv.inject({}) do |accu, pair|
356
+ name, value = pair.split('=', 2)
357
+ accu[name] = value
358
+ accu
359
+ end
360
+ begin
361
+ response = client.execute(
362
+ method, parameters, request_body, headers, {:signed => signed}
363
+ )
364
+ status, headers, body = response
365
+ puts body
366
+ exit(0)
367
+ rescue ArgumentError => e
368
+ puts e.message
369
+ exit(1)
370
+ end
291
371
  end
292
372
  end
293
373
 
@@ -71,6 +71,14 @@ module Google
71
71
  :client_credential_key => 'anonymous',
72
72
  :client_credential_secret => 'anonymous'
73
73
  )
74
+ when :two_legged_oauth_1, :two_legged_oauth
75
+ require 'signet/oauth_1/client'
76
+ # NOTE: Do not rely on this default value, as it may change
77
+ @options[:authorization] = Signet::OAuth1::Client.new(
78
+ :client_credential_key => nil,
79
+ :client_credential_secret => nil,
80
+ :two_legged => true
81
+ )
74
82
  when nil
75
83
  # No authorization mechanism
76
84
  else
@@ -17,7 +17,7 @@ module Google
17
17
  module VERSION
18
18
  MAJOR = 0
19
19
  MINOR = 1
20
- TINY = 1
20
+ TINY = 2
21
21
 
22
22
  STRING = [MAJOR, MINOR, TINY].join('.')
23
23
  end
@@ -19,18 +19,24 @@ namespace :gem do
19
19
  s.extra_rdoc_files = %w( README )
20
20
  s.rdoc_options.concat ['--main', 'README']
21
21
 
22
- s.add_runtime_dependency('signet', '>= 0.1.3')
22
+ # Dependencies used in the main library
23
+ s.add_runtime_dependency('signet', '>= 0.1.4')
23
24
  s.add_runtime_dependency('addressable', '>= 2.2.2')
24
25
  s.add_runtime_dependency('httpadapter', '>= 0.2.0')
25
26
  s.add_runtime_dependency('json', '>= 1.1.9')
26
27
  s.add_runtime_dependency('extlib', '>= 0.9.15')
27
28
 
28
- s.add_development_dependency('rack', '= 1.2.0')
29
- s.add_development_dependency('sinatra', '>= 1.0')
30
- s.add_development_dependency('liquid', '>= 2.2.2')
29
+ # Dependencies used in the CLI
30
+ s.add_runtime_dependency('launchy', '>= 0.3.2')
31
+ s.add_runtime_dependency('rack', '= 1.2.0')
32
+ s.add_runtime_dependency('sinatra', '>= 1.0')
33
+
34
+ # Dependencies used in the examples
35
+ s.add_runtime_dependency('liquid', '>= 2.2.2')
36
+
31
37
  s.add_development_dependency('rake', '>= 0.7.3')
32
38
  s.add_development_dependency('rspec', '~> 1.2.9')
33
- s.add_development_dependency('launchy', '>= 0.3.2')
39
+ s.add_development_dependency('rcov', '>= 0.9.9')
34
40
  s.add_development_dependency('diff-lcs', '>= 1.1.2')
35
41
 
36
42
  s.require_path = 'lib'
@@ -20,7 +20,8 @@ namespace :spec do
20
20
  '--exclude', 'spec',
21
21
  '--exclude', '\\.rvm\\/gems',
22
22
  '--exclude', '1\\.8\\/gems',
23
- '--exclude', '1\\.9\\/gems'
23
+ '--exclude', '1\\.9\\/gems',
24
+ '--exclude', '\\.rvm'
24
25
  ]
25
26
  end
26
27
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-api-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors: []
13
13
 
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-21 00:00:00 -07:00
18
+ date: 2010-10-22 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 29
29
+ hash: 19
30
30
  segments:
31
31
  - 0
32
32
  - 1
33
- - 3
34
- version: 0.1.3
33
+ - 4
34
+ version: 0.1.4
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  - !ruby/object:Gem::Dependency
@@ -99,9 +99,25 @@ dependencies:
99
99
  type: :runtime
100
100
  version_requirements: *id005
101
101
  - !ruby/object:Gem::Dependency
102
- name: rack
102
+ name: launchy
103
103
  prerelease: false
104
104
  requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 23
110
+ segments:
111
+ - 0
112
+ - 3
113
+ - 2
114
+ version: 0.3.2
115
+ type: :runtime
116
+ version_requirements: *id006
117
+ - !ruby/object:Gem::Dependency
118
+ name: rack
119
+ prerelease: false
120
+ requirement: &id007 !ruby/object:Gem::Requirement
105
121
  none: false
106
122
  requirements:
107
123
  - - "="
@@ -112,12 +128,12 @@ dependencies:
112
128
  - 2
113
129
  - 0
114
130
  version: 1.2.0
115
- type: :development
116
- version_requirements: *id006
131
+ type: :runtime
132
+ version_requirements: *id007
117
133
  - !ruby/object:Gem::Dependency
118
134
  name: sinatra
119
135
  prerelease: false
120
- requirement: &id007 !ruby/object:Gem::Requirement
136
+ requirement: &id008 !ruby/object:Gem::Requirement
121
137
  none: false
122
138
  requirements:
123
139
  - - ">="
@@ -127,12 +143,12 @@ dependencies:
127
143
  - 1
128
144
  - 0
129
145
  version: "1.0"
130
- type: :development
131
- version_requirements: *id007
146
+ type: :runtime
147
+ version_requirements: *id008
132
148
  - !ruby/object:Gem::Dependency
133
149
  name: liquid
134
150
  prerelease: false
135
- requirement: &id008 !ruby/object:Gem::Requirement
151
+ requirement: &id009 !ruby/object:Gem::Requirement
136
152
  none: false
137
153
  requirements:
138
154
  - - ">="
@@ -143,12 +159,12 @@ dependencies:
143
159
  - 2
144
160
  - 2
145
161
  version: 2.2.2
146
- type: :development
147
- version_requirements: *id008
162
+ type: :runtime
163
+ version_requirements: *id009
148
164
  - !ruby/object:Gem::Dependency
149
165
  name: rake
150
166
  prerelease: false
151
- requirement: &id009 !ruby/object:Gem::Requirement
167
+ requirement: &id010 !ruby/object:Gem::Requirement
152
168
  none: false
153
169
  requirements:
154
170
  - - ">="
@@ -160,11 +176,11 @@ dependencies:
160
176
  - 3
161
177
  version: 0.7.3
162
178
  type: :development
163
- version_requirements: *id009
179
+ version_requirements: *id010
164
180
  - !ruby/object:Gem::Dependency
165
181
  name: rspec
166
182
  prerelease: false
167
- requirement: &id010 !ruby/object:Gem::Requirement
183
+ requirement: &id011 !ruby/object:Gem::Requirement
168
184
  none: false
169
185
  requirements:
170
186
  - - ~>
@@ -176,27 +192,27 @@ dependencies:
176
192
  - 9
177
193
  version: 1.2.9
178
194
  type: :development
179
- version_requirements: *id010
195
+ version_requirements: *id011
180
196
  - !ruby/object:Gem::Dependency
181
- name: launchy
197
+ name: rcov
182
198
  prerelease: false
183
- requirement: &id011 !ruby/object:Gem::Requirement
199
+ requirement: &id012 !ruby/object:Gem::Requirement
184
200
  none: false
185
201
  requirements:
186
202
  - - ">="
187
203
  - !ruby/object:Gem::Version
188
- hash: 23
204
+ hash: 41
189
205
  segments:
190
206
  - 0
191
- - 3
192
- - 2
193
- version: 0.3.2
207
+ - 9
208
+ - 9
209
+ version: 0.9.9
194
210
  type: :development
195
- version_requirements: *id011
211
+ version_requirements: *id012
196
212
  - !ruby/object:Gem::Dependency
197
213
  name: diff-lcs
198
214
  prerelease: false
199
- requirement: &id012 !ruby/object:Gem::Requirement
215
+ requirement: &id013 !ruby/object:Gem::Requirement
200
216
  none: false
201
217
  requirements:
202
218
  - - ">="
@@ -208,7 +224,7 @@ dependencies:
208
224
  - 2
209
225
  version: 1.1.2
210
226
  type: :development
211
- version_requirements: *id012
227
+ version_requirements: *id013
212
228
  description: |
213
229
  The Google API Ruby Client makes it trivial to discover and access supported
214
230
  APIs.