From 6754c25c01c202ff788ffba6265d98b4b75f4db4 Mon Sep 17 00:00:00 2001 From: franck cuny Date: Sun, 6 Jun 2010 16:52:32 +0200 Subject: initial commit --- lib/Net/HTTP/Console.pm | 95 ++++++++++++++++++++++++ lib/Net/HTTP/Console/Dispatcher.pm | 21 ++++++ lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm | 31 ++++++++ lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm | 71 ++++++++++++++++++ lib/Net/HTTP/Console/Dispatcher/Help.pm | 25 +++++++ lib/Net/HTTP/Console/Dispatcher/LoadLib.pm | 23 ++++++ lib/Net/HTTP/Console/Dummy.pm | 6 ++ lib/Net/HTTP/Console/Role/DefaultMethod.pm | 20 +++++ lib/Net/HTTP/Console/Role/HTTP.pm | 42 +++++++++++ 9 files changed, 334 insertions(+) create mode 100644 lib/Net/HTTP/Console.pm create mode 100644 lib/Net/HTTP/Console/Dispatcher.pm create mode 100644 lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm create mode 100644 lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm create mode 100644 lib/Net/HTTP/Console/Dispatcher/Help.pm create mode 100644 lib/Net/HTTP/Console/Dispatcher/LoadLib.pm create mode 100644 lib/Net/HTTP/Console/Dummy.pm create mode 100644 lib/Net/HTTP/Console/Role/DefaultMethod.pm create mode 100644 lib/Net/HTTP/Console/Role/HTTP.pm (limited to 'lib') diff --git a/lib/Net/HTTP/Console.pm b/lib/Net/HTTP/Console.pm new file mode 100644 index 0000000..42d30f6 --- /dev/null +++ b/lib/Net/HTTP/Console.pm @@ -0,0 +1,95 @@ +package Net::HTTP::Console; + +use Moose; +use Try::Tiny; +use Method::Signatures::Simple; +use namespace::autoclean; + +with qw/MooseX::Getopt/; + +has url => (isa => 'Str', is => 'rw'); +has format => (isa => 'Str', is => 'rw', default => 'json'); +has format_mode => (isa => 'Str', is => 'rw', default => 'content-type'); +has lib => ( + isa => 'Str', + is => 'rw', + default => sub { + Class::MOP::load_class('Net::HTTP::Console::Dummy'); + 'Net::HTTP::Console::Dummy'; + }, + handles => qr/.*/, +); +has prompt => ( + isa => 'Str', + is => 'rw', + lazy => 1, + default => sub { + my $self = shift; + # FIXME +# my $url = $self->lib->api_base_url ? $self->lib->api_base_url : $self->url; +# my $url= "http"; + return $self->url . '> '; + } +); +has plugins => ( + traits => ['Array'], + is => 'rw', + isa => 'ArrayRef[Object]', + lazy => 1, + handles => {all_plugins => 'elements', add_plugin => 'push'}, + default => sub { + my $self = shift; + my @p; + for (qw/LoadLib HTTPRequest Help ExecuteMethod/) { + my $p = "Net::HTTP::Console::Dispatcher::" . $_; + Class::MOP::load_class($p); + my $o = $p->new(application => $self); + push @p, $o; + } + \@p; + }, +); + +sub BUILDARGS { + my ($class, %args) = @_; + if ($args{lib}) { + Class::MOP::load_class($args{lib}); + } + return {%args}; +} + +method dispatch($input) { + my @plugins = $self->all_plugins(); + my $result; + foreach (@plugins) { + $result = $_->dispatch($input); + last if $result; + } + # if (!$result) { + # try { + + # } + # catch { + # warn $_; + # print "no command found!\n" unless $result; + # }; +#} +} + +method new_lib($http_method, $path) { + my $lib = $self->lib->new( + api_base_url => $self->url, + api_format => $self->format, + api_format_mode => $self->format_mode, + ); + $lib->meta->add_net_api_method( + 'anonymous', + method => $http_method, + path => $path, + ); + return $lib; +} + +no Moose; + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher.pm b/lib/Net/HTTP/Console/Dispatcher.pm new file mode 100644 index 0000000..052e493 --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher.pm @@ -0,0 +1,21 @@ +package Net::HTTP::Console::Dispatcher; + +use Moose::Role; + +has application => (is => 'rw', isa => 'Net::HTTP::Console'); + +requires qw/dispatch pattern/; + +around dispatch => sub { + my $orig = shift; + my $self = shift; + my $in = shift; + + if (my $r = $self->pattern($in)) { + return $self->$orig($r); + }else{ + return undef; + } +}; + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm b/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm new file mode 100644 index 0000000..08536f3 --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm @@ -0,0 +1,31 @@ +package Net::HTTP::Console::Dispatcher::ExecuteMethod; + +use Moose; +with qw/ + Net::HTTP::Console::Dispatcher + Net::HTTP::Console::Role::HTTP + /; + +sub dispatch { + my ($self, $input) = @_; + $input =~ /^(\w+)\s(.*)$/; + my $method = $1; + my $args = $2; + my $o = $self->lib->new(); + my ($content, $response) = $o->$method(%{JSON::decode_json($args)}); + $self->_set_and_show($content, $response); +} + +sub pattern { + my ($self, $input) = @_; + $input =~ /^(\w+)/; + my $method = $1; + # find_api_method_by_name ? + if ($self->application->lib->meta->find_method_by_name($method)) { + return 1; + }else{ + return 0; + } +} + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm b/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm new file mode 100644 index 0000000..fd429b4 --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm @@ -0,0 +1,71 @@ +package Net::HTTP::Console::Dispatcher::HTTPRequest; + +use Moose; +use Try::Tiny; + +with qw/ + Net::HTTP::Console::Dispatcher + Net::HTTP::Console::Role::HTTP + /; + +sub _clean_http_lib { + my $self = shift; + if ($self->application->lib eq "Net::HTTP::Console::Dummy") { + map { $self->application->lib->meta->remove_net_api_method($_) } + $self->application->lib->meta->get_all_api_methods(); + } +} + +sub dispatch { + my ($self, $input) = @_; + + $self->_clean_http_lib; + + if ($input =~ /^(GET|DELETE)\s(.*)$/) { + $self->_do_request($1, $2); + } + elsif ($input =~ /^(POST|PUT)\s(.*)(?:\s(.*))$/) { + $self->_do_request_with_body($1, $2, $3); + } + elsif($input =~ /^show\s(headers|content)$/) { + my $method = "_show_last_$1"; + $self->$method; + } + else { + # XXX unsupporter method + } + return 1; +} + +sub pattern { + my ($self, $input) = @_; + $input =~ /^(?:GET|POST|PUT|DELETE|HEAD|show)/ ? return $input : return 0; +} + +sub _do_request { + my ($self, $http_method, $path) = @_; + my $http_console = $self->application->new_lib($http_method, $path); + try { + my ($content, $result) = $http_console->anonymous; + $self->_set_and_show($content, $result); + }; +} + +sub _do_request_with_body { + my ($self, $http_method, $path, $body) = @_; + my $http_console = $self->application->new_lib($http_method, $path); + $http_console->api_useragent->add_handler( + request_prepare => sub { + my $request = shift; + $request->content($body); + } + ); + try { + my ($content, $result) = $http_console->anonymous; + $self->_set_and_show($content, $result); + }; +} + +no Moose; + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher/Help.pm b/lib/Net/HTTP/Console/Dispatcher/Help.pm new file mode 100644 index 0000000..d38b186 --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher/Help.pm @@ -0,0 +1,25 @@ +package Net::HTTP::Console::Dispatcher::Help; + +use Moose; +with qw/Net::HTTP::Console::Dispatcher/; + +sub dispatch { + my ($self, $input) = @_; + $input =~ /^help\s(.*)?/; + my $cmd = $1; + if ($cmd) { + }else{ + print <application->lib($input); + return 1; + } + # XXX error confess & co +} + +sub pattern { + my ($self, $input) = @_; + $input =~ /load\s(.*)$/ ? $1 : 0; +} + +1; diff --git a/lib/Net/HTTP/Console/Dummy.pm b/lib/Net/HTTP/Console/Dummy.pm new file mode 100644 index 0000000..363ab1b --- /dev/null +++ b/lib/Net/HTTP/Console/Dummy.pm @@ -0,0 +1,6 @@ +package + Net::HTTP::Console::Dummy; + +use MooseX::Net::API; + +1; diff --git a/lib/Net/HTTP/Console/Role/DefaultMethod.pm b/lib/Net/HTTP/Console/Role/DefaultMethod.pm new file mode 100644 index 0000000..c8f858e --- /dev/null +++ b/lib/Net/HTTP/Console/Role/DefaultMethod.pm @@ -0,0 +1,20 @@ +package Net::HTTP::Console::Role::DefaultMethod; + +use Moose::Role; +use JSON; +use Method::Signatures::Simple; +use namespace::autoclean; + +with qw/Net::HTTP::Console::Role::HTTP/; + +method from_lib { + my $input = shift; + $input =~ /^(\w+)\s(.*)$/; + my $method = $1; + my $args = $2; + my $o = $self->lib->new(); + my ($content, $response) = $o->$method(%{JSON::decode_json($args)}); + $self->_set_and_show($content, $response); +} + +1; diff --git a/lib/Net/HTTP/Console/Role/HTTP.pm b/lib/Net/HTTP/Console/Role/HTTP.pm new file mode 100644 index 0000000..d0bd2c8 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/HTTP.pm @@ -0,0 +1,42 @@ +package Net::HTTP::Console::Role::HTTP; + +use Moose::Role; + +has _last_http_response => ( + is => 'rw', + isa => 'Object', + clearer => '_clear_last_http_response', +); +has _last_http_content => ( + is => 'rw', + isa => 'Str', + clearer => '_clear_last_http_content', +); +has _json => ( + is => 'rw', + isa => 'Object', + lazy => 1, + default => sub { JSON->new; }, +); + +sub _show_last_content { + my $self = shift; + print $self->_last_http_content; +} + +sub _show_last_headers { + my $self = shift; + foreach my $k (keys %{$self->_last_http_response->headers}) { + print "$k: ".$self->_last_http_response->header($k)."\n"; + } +} + +sub _set_and_show { + my ($self, $content, $response) = @_; + my $json = $self->_json->pretty->encode($content); + $self->_last_http_content($json); + $self->_last_http_response($response); + $self->_show_last_content; +} + +1; -- cgit 1.4.1