Refactored CLI tool to be more maintainable.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@89 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman 2010-10-20 23:49:15 +00:00
parent 7a343496ac
commit 0986b4e64f
2 changed files with 298 additions and 258 deletions

View File

@ -10,95 +10,27 @@ OAUTH_SERVER_PORT = 12736
require 'rubygems' require 'rubygems'
require 'optparse' require 'optparse'
require 'httpadapter'
require 'webrick'
require 'google/api_client/version' require 'google/api_client/version'
require 'google/api_client' require 'google/api_client'
ARGV.unshift('--help') if ARGV.empty? ARGV.unshift('--help') if ARGV.empty?
command = 'execute' module Google
options = {} class APIClient
OptionParser.new do |opts| class CLI
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
opts.on(
"-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
opts.on_tail("-v", "--verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
exit
end
end.parse!
if command == 'oauth-login' # Guard to keep start-up time short
require 'webrick'
# Used for oauth login # Used for oauth login
class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet class OAuthVerifierServlet < WEBrick::HTTPServlet::AbstractServlet
attr_reader :verifier
def do_GET(request, response) def do_GET(request, response)
$verifier ||= Addressable::URI.unencode_component( $verifier ||= Addressable::URI.unencode_component(
request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1] request.request_uri.to_s[/\?.*oauth_verifier=([^&$]+)(&|$)/, 1]
) )
response.status = WEBrick::HTTPStatus::RC_ACCEPTED response.status = WEBrick::HTTPStatus::RC_ACCEPTED
# This javascript will auto-close the tab after the verifier is obtained. # This javascript will auto-close the tab after the
# verifier is obtained.
response.body = <<-HTML response.body = <<-HTML
<html> <html>
<head> <head>
@ -115,12 +47,97 @@ if command == 'oauth-login' # Guard to keep start-up time short
</body> </body>
</html> </html>
HTML HTML
self.instance_variable_get('@server').stop # Eww, hack!
server = self.instance_variable_get('@server')
server.stop if server
end
end
# Initialize with default parameter values
def initialize(argv)
@options = {
:command => 'execute',
:rpcname => nil,
:verbose => false
}
@argv = argv.clone
if @argv.first =~ /^[a-z0-9_-]+$/i
self.options[:command] = @argv.shift
end
if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
self.options[:rpcname] = @argv.shift
end
end
attr_reader :options
attr_reader :argv
def command
return self.options[:command]
end
def rpcname
return self.options[:rpcname]
end
def parser
@parser ||= OptionParser.new do |opts|
opts.banner = "Usage: google-api " +
"(execute <rpcname> | [command]) [options] [-- <parameters>]"
opts.separator ""
opts.on(
"--scope <scope>", String, "Set the OAuth scope") do |s|
options[:scope] = s
end
opts.on(
"-s", "--service <name>", String,
"Perform discovery on service") do |s|
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_tail("-v", "--verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
exit
end end
end end
end end
def oauth_login(options={}) def parse!
self.parser.parse!(self.argv)
self.send(self.command.gsub(/-/, "_").to_sym)
end
def oauth_login
require 'signet/oauth_1/client' require 'signet/oauth_1/client'
require 'launchy' require 'launchy'
require 'yaml' require 'yaml'
@ -179,19 +196,31 @@ def oauth_login(options={})
exit(0) exit(0)
end end
def execute(options={}) def list
service_name = options[:service_name]
client = Google::APIClient.new(
:service => service_name,
:authorization => nil
)
service_version =
options[:service_version] ||
client.latest_service_version(service_name).version
service = client.discovered_service(service_name, service_version)
rpcnames = service.to_h.keys
puts rpcnames.sort.join("\n")
exit(0)
end
def execute
require 'signet/oauth_1/client' require 'signet/oauth_1/client'
require 'yaml' require 'yaml'
config_file = File.expand_path('~/.google-api.yaml') config_file = File.expand_path('~/.google-api.yaml')
signed = File.exist?(config_file) signed = File.exist?(config_file)
rpcname = ARGV.detect { |p| p =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i } if !self.rpcname
if rpcname STDERR.puts('No rpcname supplied.')
ARGV.delete(rpcname)
else
STDERR.puts('Could not find rpcname.')
exit(1) exit(1)
end end
service_name = options[:service_name] || rpcname[/^([^\.]+)\./, 1] service_name = options[:service_name] || self.rpcname[/^([^\.]+)\./, 1]
client = Google::APIClient.new( client = Google::APIClient.new(
:service => service_name, :service => service_name,
:authorization => :oauth_1 :authorization => :oauth_1
@ -199,7 +228,8 @@ def execute(options={})
if signed if signed
if !client.authorization.kind_of?(Signet::OAuth1::Client) if !client.authorization.kind_of?(Signet::OAuth1::Client)
STDERR.puts( STDERR.puts(
"Unexpected authorization mechanism: #{client.authorization.class}" "Unexpected authorization mechanism: " +
"#{client.authorization.class}"
) )
exit(1) exit(1)
end end
@ -217,15 +247,15 @@ def execute(options={})
options[:service_version] || options[:service_version] ||
client.latest_service_version(service_name).version client.latest_service_version(service_name).version
service = client.discovered_service(service_name, service_version) service = client.discovered_service(service_name, service_version)
method = service.to_h[rpcname] method = service.to_h[self.rpcname]
if !method if !method
STDERR.puts( STDERR.puts(
"Method #{rpcname} does not exist for " + "Method #{self.rpcname} does not exist for " +
"#{service_name}-#{service_version}." "#{service_name}-#{service_version}."
) )
exit(1) exit(1)
end end
parameters = ARGV.inject({}) do |accu, pair| parameters = self.argv.inject({}) do |accu, pair|
name, value = pair.split('=', 2) name, value = pair.split('=', 2)
accu[name] = value accu[name] = value
accu accu
@ -240,15 +270,20 @@ def execute(options={})
# Default to JSON # Default to JSON
headers << ['Content-Type', 'application/json'] headers << ['Content-Type', 'application/json']
end end
begin
response = client.execute( response = client.execute(
method, parameters, request_body, headers, {:signed => signed} method, parameters, request_body, headers, {:signed => signed}
) )
status, headers, body = response status, headers, body = response
puts body puts body
exit(0) exit(0)
rescue ArgumentError => e
puts e.message
exit(1)
end
end end
def interactive(options={}) def interactive
require 'signet/oauth_1/client' require 'signet/oauth_1/client'
require 'yaml' require 'yaml'
config_file = File.expand_path('~/.google-api.yaml') config_file = File.expand_path('~/.google-api.yaml')
@ -263,7 +298,8 @@ def interactive(options={})
if $client.authorization && if $client.authorization &&
!$client.authorization.kind_of?(Signet::OAuth1::Client) !$client.authorization.kind_of?(Signet::OAuth1::Client)
STDERR.puts( STDERR.puts(
"Unexpected authorization mechanism: #{$client.authorization.class}" "Unexpected authorization mechanism: " +
"#{$client.authorization.class}"
) )
exit(1) exit(1)
end end
@ -282,14 +318,17 @@ def interactive(options={})
IRB.start(__FILE__) IRB.start(__FILE__)
end end
def fuzz(options={}) def fuzz
STDERR.puts('API fuzzing not yet supported.') STDERR.puts('API fuzzing not yet supported.')
if rpcname if self.rpcname
# Fuzz just one method # Fuzz just one method
else else
# Fuzz the entire API # Fuzz the entire API
end end
exit(1) exit(1)
end end
end
end
end
self.send(command.gsub(/-/, "_").to_sym, options) Google::APIClient::CLI.new(ARGV).parse!

View File

@ -14,6 +14,7 @@
require 'httpadapter' require 'httpadapter'
require 'json' require 'json'
require 'stringio'
require 'google/api_client/discovery' require 'google/api_client/discovery'