about summary refs log tree commit diff
path: root/bin/http-console
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-03 10:01:01 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-03 10:01:01 +0200
commit3da11a8153d3b42af2f2a250008be6cc52e57b09 (patch)
tree4da02b541e9f8d35e5f20d63908cd33fe64dc7f8 /bin/http-console
parentreplace remainging with nothing (diff)
parentfix attribute declaration (diff)
downloadnet-http-api-3da11a8153d3b42af2f2a250008be6cc52e57b09.tar.gz
merge
Diffstat (limited to '')
-rw-r--r--bin/http-console81
1 files changed, 81 insertions, 0 deletions
diff --git a/bin/http-console b/bin/http-console
new file mode 100644
index 0000000..1fd4fd3
--- /dev/null
+++ b/bin/http-console
@@ -0,0 +1,81 @@
+use strict;
+use warnings;
+use 5.010;
+
+use Term::ReadLine;
+use Getopt::Long;
+use YAML::Syck;
+
+my $url         = shift;
+my $format_mode = 'append';
+my $format      = 'json';
+
+GetOptions(
+    'm=s' => \$format_mode,
+    'f=s' => \$format,
+);
+
+package http::net::console;
+use MooseX::Net::API;
+
+package main;
+
+my ($content, $result);
+
+my $term   = Term::ReadLine->new("http::net::console");
+my $prompt = $url . '> ';
+while (defined(my $in = $term->readline($prompt))) {
+    if ($in =~ /^(GET|DELETE)\s(.*)$/) {
+        my $http_console = http::net::console->new(
+            api_base_url    => $url,
+            api_format      => $format,
+            api_format_mode => $format_mode
+        );
+        $http_console->meta->add_net_api_method(
+            'anonymous',
+            method => $1,
+            path   => $2
+        );
+        ($content, $result) = $http_console->anonymous;
+        say $result->content;
+    }
+    elsif ($in =~ /^(POST|PUT)\s(.*)(?:\s\-d\s(.*))$/) {
+        my $method       = $1;
+        my $path         = $2;
+        my $data         = $3;
+        my $http_console = http::net::console->new(
+            api_base_url    => $url,
+            api_format      => $format,
+            api_format_mode => $format_mode,
+        );
+        $http_console->meta->add_net_api_method(
+            'anonymous',
+            method => $method,
+            path   => $path
+        );
+        $http_console->api_useragent->add_handler(
+            request_prepare => sub {
+                my $request = shift;
+                $request->content($data);
+            }
+        );
+        ($content, $result) = $http_console->anonymous;
+        say $result->content;
+    }
+    elsif ($in eq 'show headers') {
+        if (defined $result) {
+            say Dump $result->headers;
+        }
+        else {
+            say "no headers to show";
+        }
+    }
+    elsif ($in eq 'show content') {
+        if (defined $content) {
+            say Dump $result->content;
+        }
+        else {
+            say "no content to show";
+        }
+    }
+}