Skip to content

Commit

Permalink
Added interactive session command.
Browse files Browse the repository at this point in the history
git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@69 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
  • Loading branch information
Bob Aman committed Oct 13, 2010
1 parent 3eb108a commit 61fc497
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions bin/google-api
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,33 @@ 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
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
Expand All @@ -54,6 +68,10 @@ OptionParser.new do |opts|
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
Expand Down Expand Up @@ -150,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,
Expand Down Expand Up @@ -229,6 +248,40 @@ def execute(options={})
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
Expand Down

0 comments on commit 61fc497

Please sign in to comment.