diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-06 16:52:32 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-06 16:52:32 +0200 |
commit | 6754c25c01c202ff788ffba6265d98b4b75f4db4 (patch) | |
tree | 8ec3b04963a14462a51479555381cdec361f5f43 /lib/Net/HTTP/Console | |
download | net-http-console-6754c25c01c202ff788ffba6265d98b4b75f4db4.tar.gz |
initial commit
Diffstat (limited to 'lib/Net/HTTP/Console')
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher.pm | 21 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm | 31 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm | 71 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Help.pm | 25 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/LoadLib.pm | 23 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dummy.pm | 6 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/DefaultMethod.pm | 20 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/HTTP.pm | 42 |
8 files changed, 239 insertions, 0 deletions
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 <<EOF +help command - help about a command +help request - help about request +EOF + + } +} + +sub pattern { + my ($self, $input) = @_; + $input =~ /^help/ ? return $input : return 0; +} + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm b/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm new file mode 100644 index 0000000..7aac18f --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm @@ -0,0 +1,23 @@ +package Net::HTTP::Console::Dispatcher::LoadLib; + +use Moose; +use namespace::autoclean; + +with qw/Net::HTTP::Console::Dispatcher/; + +sub dispatch { + my ($self, $input) = @_; + if (Class::MOP::load_class($input)) { + print "loaded ".$input."\n"; + $self->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; |