Added interactive session command.
[arvados.git] / bin / google-api
index 02f2f6f5751f938205517121e32c9a354865f248..6ba4d392507924b4ea9697f018c06a9b5b6b5336 100755 (executable)
@@ -21,12 +21,17 @@ OptionParser.new do |opts|
   opts.banner =
     "Usage: google-api <rpcname> [options] -- <parameters>\n" +
     "   or: google-api --oauth-login=<scope> [options]\n" +
+    "   or: google-api --interactive=<service> [options]\n" +
     "   or: google-api --fuzz [options]"
 
   opts.separator ""
 
   opts.on(
       "--oauth-login <scope>", String, "Authorize for the scope") do |s|
+    if command != 'execute'
+      STDERR.puts("Ambiguous command: #{command}")
+      exit(1)
+    end
     command = 'oauth-login'
     options[:scope] = s
   end
@@ -34,11 +39,39 @@ OptionParser.new do |opts|
       "-s", "--service <name>", String, "Perform discovery on service") do |s|
     options[:service_name] = s
   end
+  opts.on(
+      "-i", "--interactive <name>", String, "Start interactive session") do |s|
+    if command != 'execute'
+      STDERR.puts("Ambiguous command: #{command}")
+      exit(1)
+    end
+    command = 'interactive'
+    options[:service_name] = s
+  end
   opts.on(
       "--service-version <id>", String, "Select service version") do |id|
     options[:service_version] = id
   end
+  opts.on(
+      "--content-type <format>", String, "Content-Type for request") do |f|
+    # Resolve content type shortcuts
+    case f
+    when 'json'
+      f = 'application/json'
+    when 'xml'
+      f = 'application/xml'
+    when 'atom'
+      f = 'application/atom+xml'
+    when 'rss'
+      f = 'application/rss+xml'
+    end
+    options[:content_type] = f
+  end
   opts.on("--fuzz [rpcname]", String, "Fuzz an API or endpoint") do |rpcname|
+    if command != 'execute'
+      STDERR.puts("Ambiguous command: #{command}")
+      exit(1)
+    end
     command = 'fuzz'
     options[:fuzz] = rpcname
   end
@@ -135,6 +168,7 @@ def oauth_login(options={})
   server.start
   oauth_client.fetch_token_credential!(:verifier => $verifier)
   config = {
+    "scope" => scope,
     "client_credential_key" => oauth_client.client_credential_key,
     "client_credential_secret" => oauth_client.client_credential_secret,
     "token_credential_key" => oauth_client.token_credential_key,
@@ -146,6 +180,8 @@ def oauth_login(options={})
 end
 
 def execute(options={})
+  require 'signet/oauth_1/client'
+  require 'yaml'
   config_file = File.expand_path('~/.google-api.yaml')
   signed = File.exist?(config_file)
   rpcname = ARGV.detect { |p| p =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i }
@@ -156,7 +192,10 @@ def execute(options={})
     exit(1)
   end
   service_name = options[:service_name] || rpcname[/^([^\.]+)\./, 1]
-  client = Google::APIClient.new(:service => service_name)
+  client = Google::APIClient.new(
+    :service => service_name,
+    :authorization => :oauth_1
+  )
   if signed
     if !client.authorization.kind_of?(Signet::OAuth1::Client)
       STDERR.puts(
@@ -175,7 +214,8 @@ def execute(options={})
       config["token_credential_secret"]
   end
   service_version =
-    options[:service_version] || client.latest_service(service_name).version
+    options[:service_version] ||
+    client.latest_service_version(service_name).version
   service = client.discovered_service(service_name, service_version)
   method = service.to_h[rpcname]
   if !method
@@ -193,14 +233,55 @@ def execute(options={})
   request_body = ''
   input_streams, _, _ = IO.select([STDIN], [], [], 0)
   request_body = STDIN.read || '' if input_streams
+  headers = []
+  if options[:content_type]
+    headers << ['Content-Type', options[:content_type]]
+  elsif request_body
+    # Default to JSON
+    headers << ['Content-Type', 'application/json']
+  end
   response = client.execute(
-    method, parameters, request_body, [], {:signed => signed}
+    method, parameters, request_body, headers, {:signed => signed}
   )
   status, headers, body = response
   puts body
   exit(0)
 end
 
+def interactive(options={})
+  require 'signet/oauth_1/client'
+  require 'yaml'
+  config_file = File.expand_path('~/.google-api.yaml')
+  signed = File.exist?(config_file)
+
+  $client = Google::APIClient.new(
+    :service => options[:service_name],
+    :authorization => (signed ? :oauth_1 : nil)
+  )
+
+  if signed
+    if $client.authorization &&
+        !$client.authorization.kind_of?(Signet::OAuth1::Client)
+      STDERR.puts(
+        "Unexpected authorization mechanism: #{$client.authorization.class}"
+      )
+      exit(1)
+    end
+    config = open(config_file, 'r') { |file| YAML.load(file.read) }
+    $client.authorization.client_credential_key =
+      config["client_credential_key"]
+    $client.authorization.client_credential_secret =
+      config["client_credential_secret"]
+    $client.authorization.token_credential_key =
+      config["token_credential_key"]
+    $client.authorization.token_credential_secret =
+      config["token_credential_secret"]
+  end
+
+  require 'irb'
+  IRB.start(__FILE__)
+end
+
 def fuzz(options={})
   STDERR.puts('API fuzzing not yet supported.')
   if rpcname