google-api-client 0.7.0.rc2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -2
- data/Gemfile +10 -1
- data/README.md +11 -1
- data/Rakefile +1 -1
- data/lib/google/api_client.rb +54 -33
- data/lib/google/api_client/errors.rb +11 -0
- data/lib/google/api_client/request.rb +1 -0
- data/lib/google/api_client/service.rb +233 -0
- data/lib/google/api_client/service/batch.rb +103 -0
- data/lib/google/api_client/service/request.rb +144 -0
- data/lib/google/api_client/service/resource.rb +40 -0
- data/lib/google/api_client/service/result.rb +162 -0
- data/lib/google/api_client/service/simple_file_store.rb +151 -0
- data/lib/google/api_client/service/stub_generator.rb +59 -0
- data/lib/google/api_client/version.rb +1 -1
- data/spec/google/api_client/batch_spec.rb +3 -3
- data/spec/google/api_client/discovery_spec.rb +24 -7
- data/spec/google/api_client/service_spec.rb +586 -0
- data/spec/google/api_client/simple_file_store_spec.rb +137 -0
- data/spec/google/api_client_spec.rb +84 -0
- data/tasks/gem.rake +1 -2
- metadata +17 -10
- data/bin/google-api +0 -390
@@ -0,0 +1,137 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
|
3
|
+
# Copyright 2013 Google Inc.
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
require 'google/api_client/service/simple_file_store'
|
20
|
+
|
21
|
+
describe Google::APIClient::Service::SimpleFileStore do
|
22
|
+
|
23
|
+
FILE_NAME = 'test.cache'
|
24
|
+
|
25
|
+
before(:all) do
|
26
|
+
File.delete(FILE_NAME) if File.exists?(FILE_NAME)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe 'with no cache file' do
|
30
|
+
before(:each) do
|
31
|
+
File.delete(FILE_NAME) if File.exists?(FILE_NAME)
|
32
|
+
@cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should return nil when asked if a key exists' do
|
36
|
+
@cache.exist?('invalid').should be_nil
|
37
|
+
File.exists?(FILE_NAME).should be_false
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should return nil when asked to read a key' do
|
41
|
+
@cache.read('invalid').should be_nil
|
42
|
+
File.exists?(FILE_NAME).should be_false
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should return nil when asked to fetch a key' do
|
46
|
+
@cache.fetch('invalid').should be_nil
|
47
|
+
File.exists?(FILE_NAME).should be_false
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should create a cache file when asked to fetch a key with a default' do
|
51
|
+
@cache.fetch('new_key') do
|
52
|
+
'value'
|
53
|
+
end.should == 'value'
|
54
|
+
File.exists?(FILE_NAME).should be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should create a cache file when asked to write a key' do
|
58
|
+
@cache.write('new_key', 'value')
|
59
|
+
File.exists?(FILE_NAME).should be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should return nil when asked to delete a key' do
|
63
|
+
@cache.delete('invalid').should be_nil
|
64
|
+
File.exists?(FILE_NAME).should be_false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe 'with an existing cache' do
|
69
|
+
before(:each) do
|
70
|
+
File.delete(FILE_NAME) if File.exists?(FILE_NAME)
|
71
|
+
@cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
|
72
|
+
@cache.write('existing_key', 'existing_value')
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should return true when asked if an existing key exists' do
|
76
|
+
@cache.exist?('existing_key').should be_true
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should return false when asked if a nonexistent key exists' do
|
80
|
+
@cache.exist?('invalid').should be_false
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return the value for an existing key when asked to read it' do
|
84
|
+
@cache.read('existing_key').should == 'existing_value'
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should return nil for a nonexistent key when asked to read it' do
|
88
|
+
@cache.read('invalid').should be_nil
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should return the value for an existing key when asked to read it' do
|
92
|
+
@cache.read('existing_key').should == 'existing_value'
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should return nil for a nonexistent key when asked to fetch it' do
|
96
|
+
@cache.fetch('invalid').should be_nil
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should return and save the default value for a nonexistent key when asked to fetch it with a default' do
|
100
|
+
@cache.fetch('new_key') do
|
101
|
+
'value'
|
102
|
+
end.should == 'value'
|
103
|
+
@cache.read('new_key').should == 'value'
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should remove an existing value and return true when asked to delete it' do
|
107
|
+
@cache.delete('existing_key').should be_true
|
108
|
+
@cache.read('existing_key').should be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'should return false when asked to delete a nonexistent key' do
|
112
|
+
@cache.delete('invalid').should be_false
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should convert keys to strings when storing them' do
|
116
|
+
@cache.write(:symbol_key, 'value')
|
117
|
+
@cache.read('symbol_key').should == 'value'
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should convert keys to strings when reading them' do
|
121
|
+
@cache.read(:existing_key).should == 'existing_value'
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should convert keys to strings when fetching them' do
|
125
|
+
@cache.fetch(:existing_key).should == 'existing_value'
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'should convert keys to strings when deleting them' do
|
129
|
+
@cache.delete(:existing_key).should be_true
|
130
|
+
@cache.read('existing_key').should be_nil
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
after(:all) do
|
135
|
+
File.delete(FILE_NAME) if File.exists?(FILE_NAME)
|
136
|
+
end
|
137
|
+
end
|
@@ -114,6 +114,7 @@ describe Google::APIClient do
|
|
114
114
|
@connection = stub_connection do |stub|
|
115
115
|
stub.post('/prediction/v1.2/training?data=12345') do |env|
|
116
116
|
env[:request_headers]['Authorization'].should == 'Bearer 12345'
|
117
|
+
[200, {}, '{}']
|
117
118
|
end
|
118
119
|
end
|
119
120
|
end
|
@@ -166,4 +167,87 @@ describe Google::APIClient do
|
|
166
167
|
)
|
167
168
|
end
|
168
169
|
end
|
170
|
+
|
171
|
+
describe 'when retiries enabled' do
|
172
|
+
before do
|
173
|
+
client.retries = 2
|
174
|
+
end
|
175
|
+
|
176
|
+
after do
|
177
|
+
@connection.verify
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'should follow redirects' do
|
181
|
+
client.authorization = nil
|
182
|
+
@connection = stub_connection do |stub|
|
183
|
+
stub.get('/foo') do |env|
|
184
|
+
[302, {'location' => 'https://www.google.com/bar'}, '{}']
|
185
|
+
end
|
186
|
+
stub.get('/bar') do |env|
|
187
|
+
[200, {}, '{}']
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
client.execute(
|
192
|
+
:uri => 'https://www.gogole.com/foo',
|
193
|
+
:connection => @connection
|
194
|
+
)
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'should refresh tokens on 401 tokens' do
|
198
|
+
client.authorization.access_token = '12345'
|
199
|
+
expect(client.authorization).to receive(:fetch_access_token!)
|
200
|
+
|
201
|
+
@connection = stub_connection do |stub|
|
202
|
+
stub.get('/foo') do |env|
|
203
|
+
[401, {}, '{}']
|
204
|
+
end
|
205
|
+
stub.get('/foo') do |env|
|
206
|
+
[200, {}, '{}']
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
client.execute(
|
211
|
+
:uri => 'https://www.gogole.com/foo',
|
212
|
+
:connection => @connection
|
213
|
+
)
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'should retry on 500 errors' do
|
217
|
+
client.authorization = nil
|
218
|
+
|
219
|
+
@connection = stub_connection do |stub|
|
220
|
+
stub.get('/foo') do |env|
|
221
|
+
[500, {}, '{}']
|
222
|
+
end
|
223
|
+
stub.get('/foo') do |env|
|
224
|
+
[200, {}, '{}']
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
client.execute(
|
229
|
+
:uri => 'https://www.gogole.com/foo',
|
230
|
+
:connection => @connection
|
231
|
+
).status.should == 200
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'should fail after max retries' do
|
236
|
+
client.authorization = nil
|
237
|
+
count = 0
|
238
|
+
@connection = stub_connection do |stub|
|
239
|
+
stub.get('/foo') do |env|
|
240
|
+
count += 1
|
241
|
+
[500, {}, '{}']
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
client.execute(
|
246
|
+
:uri => 'https://www.gogole.com/foo',
|
247
|
+
:connection => @connection
|
248
|
+
).status.should == 500
|
249
|
+
count.should == 3
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
169
253
|
end
|
data/tasks/gem.rake
CHANGED
@@ -18,7 +18,6 @@ namespace :gem do
|
|
18
18
|
s.description = PKG_DESCRIPTION
|
19
19
|
s.license = 'Apache 2.0'
|
20
20
|
s.files = PKG_FILES.to_a
|
21
|
-
s.executables << 'google-api'
|
22
21
|
|
23
22
|
s.extra_rdoc_files = %w( README.md )
|
24
23
|
s.rdoc_options.concat ['--main', 'README.md']
|
@@ -28,7 +27,7 @@ namespace :gem do
|
|
28
27
|
s.add_runtime_dependency('addressable', '>= 2.3.2')
|
29
28
|
s.add_runtime_dependency('uuidtools', '>= 2.1.0')
|
30
29
|
s.add_runtime_dependency('autoparse', '>= 0.3.3')
|
31
|
-
s.add_runtime_dependency('faraday', '>= 0.9.0
|
30
|
+
s.add_runtime_dependency('faraday', '>= 0.9.0')
|
32
31
|
s.add_runtime_dependency('multi_json', '>= 1.0.0')
|
33
32
|
s.add_runtime_dependency('extlib', '>= 0.9.15')
|
34
33
|
s.add_runtime_dependency('jwt', '>= 0.1.5')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.0
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Aman
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: signet
|
@@ -73,14 +73,14 @@ dependencies:
|
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: 0.9.0
|
76
|
+
version: 0.9.0
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
81
|
- - ! '>='
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version: 0.9.0
|
83
|
+
version: 0.9.0
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: multi_json
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
@@ -172,8 +172,7 @@ description: ! 'The Google API Ruby Client makes it trivial to discover and acce
|
|
172
172
|
|
173
173
|
'
|
174
174
|
email: sbazyl@google.com
|
175
|
-
executables:
|
176
|
-
- google-api
|
175
|
+
executables: []
|
177
176
|
extensions: []
|
178
177
|
extra_rdoc_files:
|
179
178
|
- README.md
|
@@ -204,6 +203,13 @@ files:
|
|
204
203
|
- lib/google/api_client/reference.rb
|
205
204
|
- lib/google/api_client/request.rb
|
206
205
|
- lib/google/api_client/result.rb
|
206
|
+
- lib/google/api_client/service.rb
|
207
|
+
- lib/google/api_client/service/batch.rb
|
208
|
+
- lib/google/api_client/service/request.rb
|
209
|
+
- lib/google/api_client/service/resource.rb
|
210
|
+
- lib/google/api_client/service/result.rb
|
211
|
+
- lib/google/api_client/service/simple_file_store.rb
|
212
|
+
- lib/google/api_client/service/stub_generator.rb
|
207
213
|
- lib/google/api_client/service_account.rb
|
208
214
|
- lib/google/api_client/version.rb
|
209
215
|
- lib/google/inflection.rb
|
@@ -217,6 +223,8 @@ files:
|
|
217
223
|
- spec/google/api_client/request_spec.rb
|
218
224
|
- spec/google/api_client/result_spec.rb
|
219
225
|
- spec/google/api_client/service_account_spec.rb
|
226
|
+
- spec/google/api_client/service_spec.rb
|
227
|
+
- spec/google/api_client/simple_file_store_spec.rb
|
220
228
|
- spec/google/api_client_spec.rb
|
221
229
|
- spec/spec_helper.rb
|
222
230
|
- tasks/gem.rake
|
@@ -231,8 +239,7 @@ files:
|
|
231
239
|
- LICENSE
|
232
240
|
- README.md
|
233
241
|
- Rakefile
|
234
|
-
|
235
|
-
homepage: http://code.google.com/p/google-api-ruby-client/
|
242
|
+
homepage: https://github.com/google/google-api-ruby-client
|
236
243
|
licenses:
|
237
244
|
- Apache 2.0
|
238
245
|
metadata: {}
|
@@ -249,9 +256,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
249
256
|
version: '0'
|
250
257
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
251
258
|
requirements:
|
252
|
-
- - ! '
|
259
|
+
- - ! '>='
|
253
260
|
- !ruby/object:Gem::Version
|
254
|
-
version:
|
261
|
+
version: '0'
|
255
262
|
requirements: []
|
256
263
|
rubyforge_project:
|
257
264
|
rubygems_version: 2.0.7
|
data/bin/google-api
DELETED
@@ -1,390 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
bin_dir = File.expand_path("..", __FILE__)
|
4
|
-
lib_dir = File.expand_path("../lib", bin_dir)
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(lib_dir)
|
7
|
-
$LOAD_PATH.uniq!
|
8
|
-
|
9
|
-
OAUTH_SERVER_PORT = 12736
|
10
|
-
|
11
|
-
require 'rubygems'
|
12
|
-
require 'optparse'
|
13
|
-
require 'faraday'
|
14
|
-
require 'webrick'
|
15
|
-
require 'google/api_client/version'
|
16
|
-
require 'google/api_client'
|
17
|
-
require 'google/api_client/auth/installed_app'
|
18
|
-
require 'irb'
|
19
|
-
|
20
|
-
ARGV.unshift('--help') if ARGV.empty?
|
21
|
-
|
22
|
-
module Google
|
23
|
-
class APIClient
|
24
|
-
class CLI
|
25
|
-
|
26
|
-
# Initialize with default parameter values
|
27
|
-
def initialize(argv)
|
28
|
-
@options = {
|
29
|
-
:command => 'execute',
|
30
|
-
:rpcname => nil,
|
31
|
-
:verbose => false
|
32
|
-
}
|
33
|
-
@argv = argv.clone
|
34
|
-
if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
|
35
|
-
self.options[:command] = @argv.shift
|
36
|
-
end
|
37
|
-
if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
|
38
|
-
self.options[:rpcname] = @argv.shift
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
attr_reader :options
|
43
|
-
attr_reader :argv
|
44
|
-
|
45
|
-
def command
|
46
|
-
return self.options[:command]
|
47
|
-
end
|
48
|
-
|
49
|
-
def rpcname
|
50
|
-
return self.options[:rpcname]
|
51
|
-
end
|
52
|
-
|
53
|
-
def parser
|
54
|
-
@parser ||= OptionParser.new do |opts|
|
55
|
-
opts.banner = "Usage: google-api " +
|
56
|
-
"(execute <rpcname> | [command]) [options] [-- <parameters>]"
|
57
|
-
|
58
|
-
opts.separator "\nAvailable options:"
|
59
|
-
|
60
|
-
opts.on(
|
61
|
-
"--scope <scope>", String, "Set the OAuth scope") do |s|
|
62
|
-
options[:scope] = s
|
63
|
-
end
|
64
|
-
opts.on(
|
65
|
-
"--client-id <key>", String,
|
66
|
-
"Set the OAuth client id or key") do |k|
|
67
|
-
options[:client_credential_key] = k
|
68
|
-
end
|
69
|
-
opts.on(
|
70
|
-
"--client-secret <secret>", String,
|
71
|
-
"Set the OAuth client secret") do |s|
|
72
|
-
options[:client_credential_secret] = s
|
73
|
-
end
|
74
|
-
opts.on(
|
75
|
-
"--api <name>", String,
|
76
|
-
"Perform discovery on API") do |s|
|
77
|
-
options[:api] = s
|
78
|
-
end
|
79
|
-
opts.on(
|
80
|
-
"--api-version <id>", String,
|
81
|
-
"Select api version") do |id|
|
82
|
-
options[:version] = id
|
83
|
-
end
|
84
|
-
opts.on(
|
85
|
-
"--content-type <format>", String,
|
86
|
-
"Content-Type for request") do |f|
|
87
|
-
# Resolve content type shortcuts
|
88
|
-
case f
|
89
|
-
when 'json'
|
90
|
-
f = 'application/json'
|
91
|
-
when 'xml'
|
92
|
-
f = 'application/xml'
|
93
|
-
when 'atom'
|
94
|
-
f = 'application/atom+xml'
|
95
|
-
when 'rss'
|
96
|
-
f = 'application/rss+xml'
|
97
|
-
end
|
98
|
-
options[:content_type] = f
|
99
|
-
end
|
100
|
-
opts.on(
|
101
|
-
"-u", "--uri <uri>", String,
|
102
|
-
"Sets the URI to perform a request against") do |u|
|
103
|
-
options[:uri] = u
|
104
|
-
end
|
105
|
-
opts.on(
|
106
|
-
"--discovery-uri <uri>", String,
|
107
|
-
"Sets the URI to perform discovery") do |u|
|
108
|
-
options[:discovery_uri] = u
|
109
|
-
end
|
110
|
-
opts.on(
|
111
|
-
"-m", "--method <method>", String,
|
112
|
-
"Sets the HTTP method to use for the request") do |m|
|
113
|
-
options[:http_method] = m
|
114
|
-
end
|
115
|
-
opts.on(
|
116
|
-
"--requestor-id <email>", String,
|
117
|
-
"Sets the email address of the requestor") do |e|
|
118
|
-
options[:requestor_id] = e
|
119
|
-
end
|
120
|
-
|
121
|
-
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
122
|
-
options[:verbose] = v
|
123
|
-
end
|
124
|
-
opts.on("-h", "--help", "Show this message") do
|
125
|
-
puts opts
|
126
|
-
exit
|
127
|
-
end
|
128
|
-
opts.on("--version", "Show version") do
|
129
|
-
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
|
130
|
-
exit
|
131
|
-
end
|
132
|
-
|
133
|
-
opts.separator(
|
134
|
-
"\nAvailable commands:\n" +
|
135
|
-
" oauth-2-login Log a user into an API with OAuth 2.0\n" +
|
136
|
-
" list List the methods available for an API\n" +
|
137
|
-
" execute Execute a method on the API\n" +
|
138
|
-
" irb Start an interactive client session"
|
139
|
-
)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
|
143
|
-
def parse!
|
144
|
-
self.parser.parse!(self.argv)
|
145
|
-
symbol = self.command.gsub(/-/, "_").to_sym
|
146
|
-
if !COMMANDS.include?(symbol)
|
147
|
-
STDERR.puts("Invalid command: #{self.command}")
|
148
|
-
exit(1)
|
149
|
-
end
|
150
|
-
self.send(symbol)
|
151
|
-
end
|
152
|
-
|
153
|
-
def client
|
154
|
-
require 'yaml'
|
155
|
-
config_file = File.expand_path('~/.google-api.yaml')
|
156
|
-
authorization = nil
|
157
|
-
if File.exist?(config_file)
|
158
|
-
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
159
|
-
else
|
160
|
-
config = {}
|
161
|
-
end
|
162
|
-
if config["mechanism"]
|
163
|
-
authorization = config["mechanism"].to_sym
|
164
|
-
end
|
165
|
-
|
166
|
-
client = Google::APIClient.new(
|
167
|
-
:application_name => 'Ruby CLI',
|
168
|
-
:application_version => Google::APIClient::VERSION::STRING,
|
169
|
-
:authorization => authorization)
|
170
|
-
|
171
|
-
case authorization
|
172
|
-
when :oauth_1
|
173
|
-
STDERR.puts('OAuth 1 is deprecated. Please reauthorize with OAuth 2.')
|
174
|
-
client.authorization.client_credential_key =
|
175
|
-
config["client_credential_key"]
|
176
|
-
client.authorization.client_credential_secret =
|
177
|
-
config["client_credential_secret"]
|
178
|
-
client.authorization.token_credential_key =
|
179
|
-
config["token_credential_key"]
|
180
|
-
client.authorization.token_credential_secret =
|
181
|
-
config["token_credential_secret"]
|
182
|
-
when :oauth_2
|
183
|
-
client.authorization.scope = options[:scope]
|
184
|
-
client.authorization.client_id = config["client_id"]
|
185
|
-
client.authorization.client_secret = config["client_secret"]
|
186
|
-
client.authorization.access_token = config["access_token"]
|
187
|
-
client.authorization.refresh_token = config["refresh_token"]
|
188
|
-
else
|
189
|
-
# Dunno?
|
190
|
-
end
|
191
|
-
|
192
|
-
if options[:discovery_uri]
|
193
|
-
if options[:api] && options[:version]
|
194
|
-
client.register_discovery_uri(
|
195
|
-
options[:api], options[:version], options[:discovery_uri]
|
196
|
-
)
|
197
|
-
else
|
198
|
-
STDERR.puts(
|
199
|
-
'Cannot register a discovery URI without ' +
|
200
|
-
'specifying an API and version.'
|
201
|
-
)
|
202
|
-
exit(1)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
return client
|
207
|
-
end
|
208
|
-
|
209
|
-
def api_version(api_name, version)
|
210
|
-
v = version
|
211
|
-
if !version
|
212
|
-
if client.preferred_version(api_name)
|
213
|
-
v = client.preferred_version(api_name).version
|
214
|
-
else
|
215
|
-
v = 'v1'
|
216
|
-
end
|
217
|
-
end
|
218
|
-
return v
|
219
|
-
end
|
220
|
-
|
221
|
-
COMMANDS = [
|
222
|
-
:oauth_2_login,
|
223
|
-
:list,
|
224
|
-
:execute,
|
225
|
-
:irb,
|
226
|
-
]
|
227
|
-
|
228
|
-
def oauth_2_login
|
229
|
-
require 'signet/oauth_2/client'
|
230
|
-
require 'yaml'
|
231
|
-
if !options[:client_credential_key] ||
|
232
|
-
!options[:client_credential_secret]
|
233
|
-
STDERR.puts('No client ID and secret supplied.')
|
234
|
-
exit(1)
|
235
|
-
end
|
236
|
-
if options[:access_token]
|
237
|
-
config = {
|
238
|
-
"mechanism" => "oauth_2",
|
239
|
-
"scope" => options[:scope],
|
240
|
-
"client_id" => options[:client_credential_key],
|
241
|
-
"client_secret" => options[:client_credential_secret],
|
242
|
-
"access_token" => options[:access_token],
|
243
|
-
"refresh_token" => options[:refresh_token]
|
244
|
-
}
|
245
|
-
config_file = File.expand_path('~/.google-api.yaml')
|
246
|
-
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
247
|
-
exit(0)
|
248
|
-
else
|
249
|
-
flow = Google::APIClient::InstalledAppFlow.new(
|
250
|
-
:port => OAUTH_SERVER_PORT,
|
251
|
-
:client_id => options[:client_credential_key],
|
252
|
-
:client_secret => options[:client_credential_secret],
|
253
|
-
:scope => options[:scope]
|
254
|
-
)
|
255
|
-
|
256
|
-
oauth_client = flow.authorize
|
257
|
-
if oauth_client
|
258
|
-
config = {
|
259
|
-
"mechanism" => "oauth_2",
|
260
|
-
"scope" => options[:scope],
|
261
|
-
"client_id" => oauth_client.client_id,
|
262
|
-
"client_secret" => oauth_client.client_secret,
|
263
|
-
"access_token" => oauth_client.access_token,
|
264
|
-
"refresh_token" => oauth_client.refresh_token
|
265
|
-
}
|
266
|
-
config_file = File.expand_path('~/.google-api.yaml')
|
267
|
-
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
268
|
-
end
|
269
|
-
exit(0)
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
def list
|
274
|
-
api_name = options[:api]
|
275
|
-
unless api_name
|
276
|
-
STDERR.puts('No API name supplied.')
|
277
|
-
exit(1)
|
278
|
-
end
|
279
|
-
#client = Google::APIClient.new(:authorization => nil)
|
280
|
-
if options[:discovery_uri]
|
281
|
-
if options[:api] && options[:version]
|
282
|
-
client.register_discovery_uri(
|
283
|
-
options[:api], options[:version], options[:discovery_uri]
|
284
|
-
)
|
285
|
-
else
|
286
|
-
STDERR.puts(
|
287
|
-
'Cannot register a discovery URI without ' +
|
288
|
-
'specifying an API and version.'
|
289
|
-
)
|
290
|
-
exit(1)
|
291
|
-
end
|
292
|
-
end
|
293
|
-
version = api_version(api_name, options[:version])
|
294
|
-
api = client.discovered_api(api_name, version)
|
295
|
-
rpcnames = api.to_h.keys
|
296
|
-
puts rpcnames.sort.join("\n")
|
297
|
-
exit(0)
|
298
|
-
end
|
299
|
-
|
300
|
-
def execute
|
301
|
-
client = self.client
|
302
|
-
|
303
|
-
# Setup HTTP request data
|
304
|
-
request_body = ''
|
305
|
-
input_streams, _, _ = IO.select([STDIN], [], [], 0)
|
306
|
-
request_body = STDIN.read || '' if input_streams
|
307
|
-
headers = []
|
308
|
-
if options[:content_type]
|
309
|
-
headers << ['Content-Type', options[:content_type]]
|
310
|
-
elsif request_body
|
311
|
-
# Default to JSON
|
312
|
-
headers << ['Content-Type', 'application/json']
|
313
|
-
end
|
314
|
-
|
315
|
-
if options[:uri]
|
316
|
-
# Make request with URI manually specified
|
317
|
-
uri = Addressable::URI.parse(options[:uri])
|
318
|
-
if uri.relative?
|
319
|
-
STDERR.puts('URI may not be relative.')
|
320
|
-
exit(1)
|
321
|
-
end
|
322
|
-
if options[:requestor_id]
|
323
|
-
uri.query_values = uri.query_values.merge(
|
324
|
-
'xoauth_requestor_id' => options[:requestor_id]
|
325
|
-
)
|
326
|
-
end
|
327
|
-
method = options[:http_method]
|
328
|
-
method ||= request_body == '' ? 'GET' : 'POST'
|
329
|
-
method.upcase!
|
330
|
-
response = client.execute(:http_method => method, :uri => uri.to_str,
|
331
|
-
:headers => headers, :body => request_body)
|
332
|
-
puts response.body
|
333
|
-
exit(0)
|
334
|
-
else
|
335
|
-
# Make request with URI generated from template and parameters
|
336
|
-
if !self.rpcname
|
337
|
-
STDERR.puts('No rpcname supplied.')
|
338
|
-
exit(1)
|
339
|
-
end
|
340
|
-
api_name = options[:api] || self.rpcname[/^([^\.]+)\./, 1]
|
341
|
-
version = api_version(api_name, options[:version])
|
342
|
-
api = client.discovered_api(api_name, version)
|
343
|
-
method = api.to_h[self.rpcname]
|
344
|
-
if !method
|
345
|
-
STDERR.puts(
|
346
|
-
"Method #{self.rpcname} does not exist for " +
|
347
|
-
"#{api_name}-#{version}."
|
348
|
-
)
|
349
|
-
exit(1)
|
350
|
-
end
|
351
|
-
parameters = self.argv.inject({}) do |accu, pair|
|
352
|
-
name, value = pair.split('=', 2)
|
353
|
-
accu[name] = value
|
354
|
-
accu
|
355
|
-
end
|
356
|
-
if options[:requestor_id]
|
357
|
-
parameters['xoauth_requestor_id'] = options[:requestor_id]
|
358
|
-
end
|
359
|
-
begin
|
360
|
-
result = client.execute(
|
361
|
-
:api_method => method,
|
362
|
-
:parameters => parameters,
|
363
|
-
:merged_body => request_body,
|
364
|
-
:headers => headers
|
365
|
-
)
|
366
|
-
puts result.response.body
|
367
|
-
exit(0)
|
368
|
-
rescue ArgumentError => e
|
369
|
-
puts e.message
|
370
|
-
exit(1)
|
371
|
-
end
|
372
|
-
end
|
373
|
-
end
|
374
|
-
|
375
|
-
def irb
|
376
|
-
$client = self.client
|
377
|
-
# Otherwise IRB will misinterpret command-line options
|
378
|
-
ARGV.clear
|
379
|
-
IRB.start(__FILE__)
|
380
|
-
end
|
381
|
-
|
382
|
-
def help
|
383
|
-
puts self.parser
|
384
|
-
exit(0)
|
385
|
-
end
|
386
|
-
end
|
387
|
-
end
|
388
|
-
end
|
389
|
-
|
390
|
-
Google::APIClient::CLI.new(ARGV).parse!
|