Merge pull request #20 from simplymeasured/feature/make-autorefresh-of-token-optional
[arvados.git] / bin / google-api
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 'faraday/utils'
15 require 'webrick'
16 require 'google/api_client/version'
17 require 'google/api_client'
18 require 'google/api_client/auth/installed_app'
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!