diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-08 13:18:38 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-08 13:18:38 +0200 |
commit | 548f1a38f43a1b5da435b3a7c778f5ec3471f2f2 (patch) | |
tree | 4b36535807ec253033bc6f4ba0f259e62c74d1f6 | |
parent | update name of api methods; fetch doc from method (diff) | |
download | net-http-console-548f1a38f43a1b5da435b3a7c778f5ec3471f2f2.tar.gz |
update regex; modify http methods; ..
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm | 30 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm | 37 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/Headers.pm | 49 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Dispatcher/LoadLib.pm | 7 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/APILib.pm | 26 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/DefaultMethod.pm | 20 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/HTTP.pm | 3 | ||||
-rw-r--r-- | lib/Net/HTTP/Console/Role/Headers.pm | 19 |
8 files changed, 130 insertions, 61 deletions
diff --git a/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm b/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm index 08536f3..12a66e4 100644 --- a/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm +++ b/lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm @@ -1,31 +1,25 @@ package Net::HTTP::Console::Dispatcher::ExecuteMethod; use Moose; -with qw/ - Net::HTTP::Console::Dispatcher - Net::HTTP::Console::Role::HTTP - /; +with qw/Net::HTTP::Console::Dispatcher/; 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); + (my $method, my $args) = $input =~ /^(\w+)\s(.*)$/; + my ($content, $response) = + $self->application->api_object->$method(%{JSON::decode_json($args)}); + $self->application->_set_and_show($content, $response); + 1; } 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; - } + (my $method) = $input =~ /^(\w+)/; + + # XXX find_api_method_by_name ? + $self->application->api_object->meta->find_method_by_name($method) + ? return $input + : return 0; } 1; diff --git a/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm b/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm index fd429b4..ec8892f 100644 --- a/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm +++ b/lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm @@ -3,10 +3,7 @@ package Net::HTTP::Console::Dispatcher::HTTPRequest; use Moose; use Try::Tiny; -with qw/ - Net::HTTP::Console::Dispatcher - Net::HTTP::Console::Role::HTTP - /; +with qw/ Net::HTTP::Console::Dispatcher /; sub _clean_http_lib { my $self = shift; @@ -27,11 +24,12 @@ sub dispatch { elsif ($input =~ /^(POST|PUT)\s(.*)(?:\s(.*))$/) { $self->_do_request_with_body($1, $2, $3); } - elsif($input =~ /^show\s(headers|content)$/) { + elsif ($input =~ /^show\s(headers|content)$/) { my $method = "_show_last_$1"; - $self->$method; + $self->application->$method; } else { + # XXX unsupporter method } return 1; @@ -39,30 +37,39 @@ sub dispatch { sub pattern { my ($self, $input) = @_; - $input =~ /^(?:GET|POST|PUT|DELETE|HEAD|show)/ ? return $input : return 0; + $input =~ /^(?:GET|POST|PUT|DELETE|HEAD|show)\s/ ? return $input : return 0; } sub _do_request { my ($self, $http_method, $path) = @_; - my $http_console = $self->application->new_lib($http_method, $path); + $self->application->new_anonymous_method($http_method, $path); try { - my ($content, $result) = $http_console->anonymous; - $self->_set_and_show($content, $result); + my ($content, $result) = $self->application->api_object->anonymous; + $self->application->_set_and_show($content, $result); + }catch{ + warn $_; }; } 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( + $self->application->new_anonymous_method($http_method, $path); + + # XXX clean handlers + $self->application->api_object->api_useragent->add_handler( request_prepare => sub { my $request = shift; - $request->content($body); + $request->header('Content-Type' => 'application/json'); + $request->content('{"foof":"bar"}'); } ); try { - my ($content, $result) = $http_console->anonymous; - $self->_set_and_show($content, $result); + my ($content, $result) = $self->application->api_object->anonymous; + $self->application->_set_and_show($content, $result); + }catch{ + warn $_; + use YAML::Syck; + warn Dump $_->http_error; }; } diff --git a/lib/Net/HTTP/Console/Dispatcher/Headers.pm b/lib/Net/HTTP/Console/Dispatcher/Headers.pm new file mode 100644 index 0000000..4c29098 --- /dev/null +++ b/lib/Net/HTTP/Console/Dispatcher/Headers.pm @@ -0,0 +1,49 @@ +package Net::HTTP::Console::Dispatcher::Headers; + +use Moose; +with qw/Net::HTTP::Console::Dispatcher/; + +sub dispatch { + my ($self, $input) = @_; + + (my $command, my $header, my $value) = + $input =~ /^([\w_]+)(?:\s([\w-]+))?(?:\s(.*))?$/; + + if ($command eq 'unset_header') { + $self->_unset_header($header); + } + elsif ($command eq 'set_header') { + $self->_set_header($header, $value); + } + elsif ($command eq 'show_defined_headers') { + $self->_show_defined_headers(); + } +} + +sub pattern { + my ($self, $input) = @_; + $input =~ /(un)?set_header|show_defined_headers/ + ? return $input + : return 0; +} + +sub _unset_header { + my ($self, $header) = @_; + $self->application->delete_header($header); + print "header $header unset\n"; +} + +sub _set_header { + my ($self, $header, $value) = @_; + $self->application->set_header($header, $value); + print "header $header set to $value\n"; +} + +sub _show_defined_headers{ + my $self = shift; + foreach ($self->application->all_headers) { + print $_->[0].": ".$_->[1]."\n"; + } +} + +1; diff --git a/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm b/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm index 7aac18f..d5e97cc 100644 --- a/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm +++ b/lib/Net/HTTP/Console/Dispatcher/LoadLib.pm @@ -7,12 +7,7 @@ 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 + $self->application->load_api_lib($input); } sub pattern { diff --git a/lib/Net/HTTP/Console/Role/APILib.pm b/lib/Net/HTTP/Console/Role/APILib.pm new file mode 100644 index 0000000..5f5aff1 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/APILib.pm @@ -0,0 +1,26 @@ +package Net::HTTP::Console::Role::APILib; + +use Moose::Role; + +has lib => (isa => 'Str', is => 'rw', default => 'Net::HTTP::Console::Dummy'); +has api_object => ( + isa => 'Object', + is => 'rw', + lazy => 1, + default => sub { + my $self = shift; + $self->load_api_lib($self->lib); + }, +); + +sub load_api_lib { + my ($self, $lib) = @_; + Class::MOP::load_class($lib); + my $o = $lib->new(); + $o->api_base_url($self->url) if $self->has_url; + $o->api_format($self->format) if $self->has_format; + $o->api_format_mode($self->format_mode) if $self->has_format_mode; + $o; +} + +1; diff --git a/lib/Net/HTTP/Console/Role/DefaultMethod.pm b/lib/Net/HTTP/Console/Role/DefaultMethod.pm deleted file mode 100644 index c8f858e..0000000 --- a/lib/Net/HTTP/Console/Role/DefaultMethod.pm +++ /dev/null @@ -1,20 +0,0 @@ -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 index d0bd2c8..292de40 100644 --- a/lib/Net/HTTP/Console/Role/HTTP.pm +++ b/lib/Net/HTTP/Console/Role/HTTP.pm @@ -33,8 +33,7 @@ sub _show_last_headers { sub _set_and_show { my ($self, $content, $response) = @_; - my $json = $self->_json->pretty->encode($content); - $self->_last_http_content($json); + $self->_last_http_content($self->_json->pretty->encode($content)); $self->_last_http_response($response); $self->_show_last_content; } diff --git a/lib/Net/HTTP/Console/Role/Headers.pm b/lib/Net/HTTP/Console/Role/Headers.pm new file mode 100644 index 0000000..d694d85 --- /dev/null +++ b/lib/Net/HTTP/Console/Role/Headers.pm @@ -0,0 +1,19 @@ +package Net::HTTP::Console::Role::Headers; + +use Moose::Role; + +has custom_headers => ( + traits => ['Hash'], + is => 'ro', + isa => 'HashRef[Str]', + default => sub { {} }, + handles => { + set_header => 'set', + get_header => 'get', + has_no_headers => 'is_empty', + delete_header => 'delete', + all_headers => 'kv', + }, +); + +1; |