summary refs log tree commit diff
path: root/lib/Net/HTTP/Console/Dispatcher
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-08 13:18:38 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-08 13:18:38 +0200
commit548f1a38f43a1b5da435b3a7c778f5ec3471f2f2 (patch)
tree4b36535807ec253033bc6f4ba0f259e62c74d1f6 /lib/Net/HTTP/Console/Dispatcher
parentupdate name of api methods; fetch doc from method (diff)
downloadnet-http-console-548f1a38f43a1b5da435b3a7c778f5ec3471f2f2.tar.gz
update regex; modify http methods; ..
Diffstat (limited to 'lib/Net/HTTP/Console/Dispatcher')
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm30
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm37
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Headers.pm49
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/LoadLib.pm7
4 files changed, 84 insertions, 39 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 {