2010-10-12 01:53:40 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
bin_dir = File.expand_path("..", __FILE__)
|
|
|
|
lib_dir = File.expand_path("../lib", bin_dir)
|
|
|
|
|
|
|
|
$LOAD_PATH.unshift(lib_dir)
|
|
|
|
$LOAD_PATH.uniq!
|
|
|
|
|
|
|
|
OAUTH_SERVER_PORT = 12736
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
require 'optparse'
|
2012-01-27 14:57:57 +00:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday/utils'
|
2010-10-20 23:49:15 +00:00
|
|
|
require 'webrick'
|
2010-10-12 01:53:40 +00:00
|
|
|
require 'google/api_client/version'
|
|
|
|
require 'google/api_client'
|
2013-01-05 00:14:59 +00:00
|
|
|
require 'google/api_client/auth/installed_app'
|
2013-02-15 21:20:30 +00:00
|
|
|
require 'irb'
|
2010-10-12 01:53:40 +00:00
|
|
|
|
|
|
|
ARGV.unshift('--help') if ARGV.empty?
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
module Google
|
|
|
|
class APIClient
|
|
|
|
class CLI
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
# Initialize with default parameter values
|
|
|
|
def initialize(argv)
|
|
|
|
@options = {
|
|
|
|
:command => 'execute',
|
|
|
|
:rpcname => nil,
|
|
|
|
:verbose => false
|
|
|
|
}
|
|
|
|
@argv = argv.clone
|
2010-10-22 00:36:15 +00:00
|
|
|
if @argv.first =~ /^[a-z0-9][a-z0-9_-]*$/i
|
2010-10-20 23:49:15 +00:00
|
|
|
self.options[:command] = @argv.shift
|
|
|
|
end
|
|
|
|
if @argv.first =~ /^[a-z0-9_-]+\.[a-z0-9_\.-]+$/i
|
|
|
|
self.options[:rpcname] = @argv.shift
|
|
|
|
end
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
attr_reader :options
|
|
|
|
attr_reader :argv
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def command
|
|
|
|
return self.options[:command]
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def rpcname
|
|
|
|
return self.options[:rpcname]
|
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def parser
|
|
|
|
@parser ||= OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: google-api " +
|
|
|
|
"(execute <rpcname> | [command]) [options] [-- <parameters>]"
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.separator "\nAvailable options:"
|
2010-10-12 01:53:40 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
opts.on(
|
|
|
|
"--scope <scope>", String, "Set the OAuth scope") do |s|
|
|
|
|
options[:scope] = s
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
opts.on(
|
2011-05-04 11:44:35 +00:00
|
|
|
"--client-id <key>", String,
|
|
|
|
"Set the OAuth client id or key") do |k|
|
2010-10-22 00:36:23 +00:00
|
|
|
options[:client_credential_key] = k
|
|
|
|
end
|
|
|
|
opts.on(
|
|
|
|
"--client-secret <secret>", String,
|
2011-05-04 11:44:35 +00:00
|
|
|
"Set the OAuth client secret") do |s|
|
2010-10-22 00:36:23 +00:00
|
|
|
options[:client_credential_secret] = s
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
opts.on(
|
2011-05-04 11:44:35 +00:00
|
|
|
"--api <name>", String,
|
|
|
|
"Perform discovery on API") do |s|
|
|
|
|
options[:api] = s
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
opts.on(
|
2011-10-06 09:35:02 +00:00
|
|
|
"--api-version <id>", String,
|
|
|
|
"Select api version") do |id|
|
2011-05-04 11:44:35 +00:00
|
|
|
options[:version] = id
|
2010-10-20 23:49:15 +00:00
|
|
|
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
|
2010-10-22 00:36:23 +00:00
|
|
|
opts.on(
|
|
|
|
"-u", "--uri <uri>", String,
|
|
|
|
"Sets the URI to perform a request against") do |u|
|
|
|
|
options[:uri] = u
|
|
|
|
end
|
2010-10-22 22:21:26 +00:00
|
|
|
opts.on(
|
|
|
|
"--discovery-uri <uri>", String,
|
|
|
|
"Sets the URI to perform discovery") do |u|
|
|
|
|
options[:discovery_uri] = u
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
opts.on(
|
|
|
|
"-m", "--method <method>", String,
|
|
|
|
"Sets the HTTP method to use for the request") do |m|
|
|
|
|
options[:http_method] = m
|
|
|
|
end
|
2010-10-22 19:15:05 +00:00
|
|
|
opts.on(
|
|
|
|
"--requestor-id <email>", String,
|
|
|
|
"Sets the email address of the requestor") do |e|
|
|
|
|
options[:requestor_id] = e
|
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("-v", "--verbose", "Run verbosely") do |v|
|
2010-10-20 23:49:15 +00:00
|
|
|
options[:verbose] = v
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("-h", "--help", "Show this message") do
|
2010-10-20 23:49:15 +00:00
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
opts.on("--version", "Show version") do
|
2010-10-20 23:49:15 +00:00
|
|
|
puts "google-api-client (#{Google::APIClient::VERSION::STRING})"
|
|
|
|
exit
|
|
|
|
end
|
2010-10-22 00:36:15 +00:00
|
|
|
|
|
|
|
opts.separator(
|
|
|
|
"\nAvailable commands:\n" +
|
2013-01-05 00:14:59 +00:00
|
|
|
" oauth-2-login Log a user into an API with OAuth 2.0\n" +
|
2011-10-06 09:35:02 +00:00
|
|
|
" list List the methods available for an API\n" +
|
2011-05-04 11:44:35 +00:00
|
|
|
" execute Execute a method on the API\n" +
|
|
|
|
" irb Start an interactive client session"
|
2010-10-22 00:36:15 +00:00
|
|
|
)
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def parse!
|
|
|
|
self.parser.parse!(self.argv)
|
2010-10-22 00:36:23 +00:00
|
|
|
symbol = self.command.gsub(/-/, "_").to_sym
|
|
|
|
if !COMMANDS.include?(symbol)
|
|
|
|
STDERR.puts("Invalid command: #{self.command}")
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
self.send(symbol)
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-13 00:57:04 +00:00
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
def client
|
|
|
|
require 'yaml'
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
authorization = nil
|
|
|
|
if File.exist?(config_file)
|
|
|
|
config = open(config_file, 'r') { |file| YAML.load(file.read) }
|
|
|
|
else
|
|
|
|
config = {}
|
|
|
|
end
|
|
|
|
if config["mechanism"]
|
|
|
|
authorization = config["mechanism"].to_sym
|
|
|
|
end
|
|
|
|
|
2013-01-05 00:14:59 +00:00
|
|
|
client = Google::APIClient.new(
|
|
|
|
:application_name => 'Ruby CLI',
|
|
|
|
:application_version => Google::APIClient::VERSION::STRING,
|
|
|
|
:authorization => authorization)
|
2011-05-04 11:44:35 +00:00
|
|
|
|
|
|
|
case authorization
|
|
|
|
when :oauth_1
|
2013-01-05 00:14:59 +00:00
|
|
|
STDERR.puts('OAuth 1 is deprecated. Please reauthorize with OAuth 2.')
|
2011-05-04 11:44:35 +00:00
|
|
|
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"]
|
|
|
|
when :oauth_2
|
|
|
|
client.authorization.scope = options[:scope]
|
|
|
|
client.authorization.client_id = config["client_id"]
|
|
|
|
client.authorization.client_secret = config["client_secret"]
|
|
|
|
client.authorization.access_token = config["access_token"]
|
|
|
|
client.authorization.refresh_token = config["refresh_token"]
|
|
|
|
else
|
|
|
|
# Dunno?
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:discovery_uri]
|
2011-10-06 09:35:02 +00:00
|
|
|
if options[:api] && options[:version]
|
|
|
|
client.register_discovery_uri(
|
|
|
|
options[:api], options[:version], options[:discovery_uri]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
STDERR.puts(
|
|
|
|
'Cannot register a discovery URI without ' +
|
|
|
|
'specifying an API and version.'
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return client
|
|
|
|
end
|
|
|
|
|
2011-10-06 09:35:02 +00:00
|
|
|
def api_version(api_name, version)
|
2011-05-04 11:44:35 +00:00
|
|
|
v = version
|
|
|
|
if !version
|
2011-10-06 09:35:02 +00:00
|
|
|
if client.preferred_version(api_name)
|
|
|
|
v = client.preferred_version(api_name).version
|
2011-05-04 11:44:35 +00:00
|
|
|
else
|
|
|
|
v = 'v1'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2010-10-22 00:36:23 +00:00
|
|
|
COMMANDS = [
|
2011-05-04 11:44:35 +00:00
|
|
|
:oauth_2_login,
|
2010-10-22 00:36:23 +00:00
|
|
|
:list,
|
|
|
|
:execute,
|
|
|
|
:irb,
|
|
|
|
]
|
|
|
|
|
2011-05-04 11:44:35 +00:00
|
|
|
def oauth_2_login
|
|
|
|
require 'signet/oauth_2/client'
|
|
|
|
require 'yaml'
|
|
|
|
if !options[:client_credential_key] ||
|
|
|
|
!options[:client_credential_secret]
|
|
|
|
STDERR.puts('No client ID and secret supplied.')
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
if options[:access_token]
|
|
|
|
config = {
|
|
|
|
"mechanism" => "oauth_2",
|
|
|
|
"scope" => options[:scope],
|
|
|
|
"client_id" => options[:client_credential_key],
|
|
|
|
"client_secret" => options[:client_credential_secret],
|
|
|
|
"access_token" => options[:access_token],
|
|
|
|
"refresh_token" => options[:refresh_token]
|
|
|
|
}
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
|
|
|
exit(0)
|
|
|
|
else
|
2013-01-05 00:14:59 +00:00
|
|
|
flow = Google::APIClient::InstalledAppFlow.new(
|
|
|
|
:port => OAUTH_SERVER_PORT,
|
2011-05-04 11:44:35 +00:00
|
|
|
:client_id => options[:client_credential_key],
|
|
|
|
:client_secret => options[:client_credential_secret],
|
|
|
|
:scope => options[:scope]
|
|
|
|
)
|
2013-01-05 00:14:59 +00:00
|
|
|
|
|
|
|
oauth_client = flow.authorize
|
|
|
|
if oauth_client
|
|
|
|
config = {
|
|
|
|
"mechanism" => "oauth_2",
|
|
|
|
"scope" => options[:scope],
|
|
|
|
"client_id" => oauth_client.client_id,
|
|
|
|
"client_secret" => oauth_client.client_secret,
|
|
|
|
"access_token" => oauth_client.access_token,
|
|
|
|
"refresh_token" => oauth_client.refresh_token
|
|
|
|
}
|
|
|
|
config_file = File.expand_path('~/.google-api.yaml')
|
|
|
|
open(config_file, 'w') { |file| file.write(YAML.dump(config)) }
|
|
|
|
end
|
2011-05-04 11:44:35 +00:00
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
def list
|
2011-10-06 09:35:02 +00:00
|
|
|
api_name = options[:api]
|
|
|
|
unless api_name
|
2011-05-12 20:14:15 +00:00
|
|
|
STDERR.puts('No API name supplied.')
|
2010-10-22 22:21:26 +00:00
|
|
|
exit(1)
|
|
|
|
end
|
2013-01-05 00:14:59 +00:00
|
|
|
#client = Google::APIClient.new(:authorization => nil)
|
2010-10-22 22:21:26 +00:00
|
|
|
if options[:discovery_uri]
|
2011-10-07 09:19:29 +00:00
|
|
|
if options[:api] && options[:version]
|
|
|
|
client.register_discovery_uri(
|
|
|
|
options[:api], options[:version], options[:discovery_uri]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
STDERR.puts(
|
|
|
|
'Cannot register a discovery URI without ' +
|
|
|
|
'specifying an API and version.'
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
2010-10-22 22:21:26 +00:00
|
|
|
end
|
2011-10-06 09:35:02 +00:00
|
|
|
version = api_version(api_name, options[:version])
|
|
|
|
api = client.discovered_api(api_name, version)
|
|
|
|
rpcnames = api.to_h.keys
|
2010-10-20 23:49:15 +00:00
|
|
|
puts rpcnames.sort.join("\n")
|
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2011-05-04 11:44:35 +00:00
|
|
|
client = self.client
|
2010-10-22 00:36:23 +00:00
|
|
|
|
|
|
|
# Setup HTTP request data
|
|
|
|
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']
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
|
|
|
|
if options[:uri]
|
|
|
|
# Make request with URI manually specified
|
|
|
|
uri = Addressable::URI.parse(options[:uri])
|
|
|
|
if uri.relative?
|
|
|
|
STDERR.puts('URI may not be relative.')
|
|
|
|
exit(1)
|
|
|
|
end
|
2010-10-22 19:15:05 +00:00
|
|
|
if options[:requestor_id]
|
|
|
|
uri.query_values = uri.query_values.merge(
|
|
|
|
'xoauth_requestor_id' => options[:requestor_id]
|
|
|
|
)
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
method = options[:http_method]
|
|
|
|
method ||= request_body == '' ? 'GET' : 'POST'
|
|
|
|
method.upcase!
|
2012-09-05 21:05:48 +00:00
|
|
|
response = client.execute(:http_method => method, :uri => uri.to_str,
|
|
|
|
:headers => headers, :body => request_body)
|
2012-01-27 14:57:57 +00:00
|
|
|
puts response.body
|
2010-10-20 23:49:15 +00:00
|
|
|
exit(0)
|
2010-10-22 00:36:23 +00:00
|
|
|
else
|
|
|
|
# Make request with URI generated from template and parameters
|
|
|
|
if !self.rpcname
|
|
|
|
STDERR.puts('No rpcname supplied.')
|
|
|
|
exit(1)
|
|
|
|
end
|
2011-10-06 09:35:02 +00:00
|
|
|
api_name = options[:api] || self.rpcname[/^([^\.]+)\./, 1]
|
|
|
|
version = api_version(api_name, options[:version])
|
|
|
|
api = client.discovered_api(api_name, version)
|
|
|
|
method = api.to_h[self.rpcname]
|
2010-10-22 00:36:23 +00:00
|
|
|
if !method
|
|
|
|
STDERR.puts(
|
|
|
|
"Method #{self.rpcname} does not exist for " +
|
2011-10-06 09:35:02 +00:00
|
|
|
"#{api_name}-#{version}."
|
2010-10-22 00:36:23 +00:00
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
parameters = self.argv.inject({}) do |accu, pair|
|
|
|
|
name, value = pair.split('=', 2)
|
|
|
|
accu[name] = value
|
|
|
|
accu
|
|
|
|
end
|
2010-10-22 19:15:05 +00:00
|
|
|
if options[:requestor_id]
|
|
|
|
parameters['xoauth_requestor_id'] = options[:requestor_id]
|
|
|
|
end
|
2010-10-22 00:36:23 +00:00
|
|
|
begin
|
2011-07-29 22:07:04 +00:00
|
|
|
result = client.execute(
|
|
|
|
:api_method => method,
|
|
|
|
:parameters => parameters,
|
|
|
|
:merged_body => request_body,
|
|
|
|
:headers => headers
|
2010-10-22 00:36:23 +00:00
|
|
|
)
|
2012-01-27 14:57:57 +00:00
|
|
|
puts result.response.body
|
2010-10-22 00:36:23 +00:00
|
|
|
exit(0)
|
|
|
|
rescue ArgumentError => e
|
|
|
|
puts e.message
|
|
|
|
exit(1)
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
def irb
|
2011-05-04 11:44:35 +00:00
|
|
|
$client = self.client
|
2010-10-22 00:36:15 +00:00
|
|
|
# Otherwise IRB will misinterpret command-line options
|
|
|
|
ARGV.clear
|
2010-10-20 23:49:15 +00:00
|
|
|
IRB.start(__FILE__)
|
|
|
|
end
|
|
|
|
|
2010-10-22 00:36:15 +00:00
|
|
|
def help
|
|
|
|
puts self.parser
|
|
|
|
exit(0)
|
|
|
|
end
|
2010-10-20 23:49:15 +00:00
|
|
|
end
|
2010-10-12 01:53:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-20 23:49:15 +00:00
|
|
|
Google::APIClient::CLI.new(ARGV).parse!
|