summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-02 11:37:49 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-02 11:37:49 +0200
commit44e876662f72e88e5bd40c433a14e749d3ad5fdc (patch)
treece2453c4d83be24b37909a3153578b0388e5fee8
parentrole to handle format informations (diff)
downloadmoosex-net-api-44e876662f72e88e5bd40c433a14e749d3ad5fdc.tar.gz
http_request and api_base_url are defined in this role
-rw-r--r--lib/MooseX/Net/API/Role/Request.pm52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/MooseX/Net/API/Role/Request.pm b/lib/MooseX/Net/API/Role/Request.pm
new file mode 100644
index 0000000..eabb32d
--- /dev/null
+++ b/lib/MooseX/Net/API/Role/Request.pm
@@ -0,0 +1,52 @@
+package MooseX::Net::API::Role::Request;
+
+use Moose::Role;
+use HTTP::Request;
+use MooseX::Net::API::Error;
+use MooseX::Types::URI qw(Uri);
+
+has api_base_url => (
+    is      => 'rw',
+    isa     => Uri,
+    coerce  => 1,
+    lazy    => 1,
+    default => sub {
+        my $self         = shift;
+        my $api_base_url = $self->meta->get_option('api_base_url');
+        if (!$api_base_url) {
+            die MooseX::Net::API::Error->new(
+                reason => "'api_base_url' have not been defined");
+        }
+        $api_base_url;
+    }
+);
+
+sub http_request {
+    my ($self, $method, $uri, $params_in_url, $args) = @_;
+
+    my $request;
+
+    if ( $method =~ /^(?:GET|DELETE)$/ || $params_in_url ) {
+        $uri->query_form(%$args);
+        $request = HTTP::Request->new( $method => $uri );
+    }
+    elsif ( $method =~ /^(?:POST|PUT)$/ ) {
+        $request = HTTP::Request->new( $method => $uri );
+        my $content = $self->serialize($args);
+        $request->content($content);
+    }
+    else {
+        die MooseX::Net::API::Error->new(
+            reason => "$method is not defined" );
+    }
+
+    $request->header(
+        'Content-Type' => $self->content_type->{$self->api_format}->{value})
+      if $self->api_format_mode eq 'content-type';
+
+    # XXX lwp hook!
+    my $result = $self->api_useragent->request($request);
+    return $result;
+}
+
+1;