summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-06 16:52:32 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-06 16:52:32 +0200
commit6754c25c01c202ff788ffba6265d98b4b75f4db4 (patch)
tree8ec3b04963a14462a51479555381cdec361f5f43
downloadnet-http-console-6754c25c01c202ff788ffba6265d98b4b75f4db4.tar.gz
initial commit
-rw-r--r--bin/http-console12
-rw-r--r--dist.ini29
-rw-r--r--lib/Net/HTTP/Console.pm95
-rw-r--r--lib/Net/HTTP/Console/Dispatcher.pm21
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/ExecuteMethod.pm31
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/HTTPRequest.pm71
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/Help.pm25
-rw-r--r--lib/Net/HTTP/Console/Dispatcher/LoadLib.pm23
-rw-r--r--lib/Net/HTTP/Console/Dummy.pm6
-rw-r--r--lib/Net/HTTP/Console/Role/DefaultMethod.pm20
-rw-r--r--lib/Net/HTTP/Console/Role/HTTP.pm42
11 files changed, 375 insertions, 0 deletions
diff --git a/bin/http-console b/bin/http-console
new file mode 100644
index 0000000..e579945
--- /dev/null
+++ b/bin/http-console
@@ -0,0 +1,12 @@
+use strict;
+use warnings;
+
+use Term::ReadLine;
+use Net::HTTP::Console;
+
+my $console = Net::HTTP::Console->new_with_options();
+
+my $term = Term::ReadLine->new("http::net::console");
+while (defined(my $in = $term->readline($console->prompt))) {
+    $console->dispatch($in);
+}
diff --git a/dist.ini b/dist.ini
new file mode 100644
index 0000000..67ed96b
--- /dev/null
+++ b/dist.ini
@@ -0,0 +1,29 @@
+name = Net-HTTP-Console
+author = franck cuny <franck@lumberjaph.net>
+license = Perl_5
+copyright_holder = franck cuny
+copyright_year = 2010
+version = 0.01
+
+[@Filter]
+bundle = @Basic
+
+[MetaConfig]
+[MetaJSON]
+[PkgVersion]
+[PodSyntaxTests]
+[KwaliteeTests]
+[PodCoverageTests]
+[NoTabsTests]
+[EOLTests]
+
+[MetaResources]
+repository = git://github.com/franckcuny/net-http-console.git
+bugtracker = http://rt.cpan.org/Public/Dist/Display.html?Name=Net::HTTP::Console
+homepage = http://search.cpan.org/perldoc?Net::HTTP::Console
+
+[PodWeaver]
+[AutoPrereq]
+[ReadmeFromPod]
+[CheckChangeLog]
+[UploadToCPAN]
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 <<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;