summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-10-25 18:21:40 +0200
committerfranck cuny <franck@lumberjaph.net>2010-10-25 18:21:40 +0200
commit0e754169a099cd007d4a5abd72eae2832c34bc43 (patch)
treebedb00a1bb02d5a4bce4300aa3098327aa8b22e2
parentapi spec can be modularized: (diff)
downloadnet-http-spore-0e754169a099cd007d4a5abd72eae2832c34bc43.tar.gz
add payload and form-data to the method object
-rw-r--r--lib/Net/HTTP/Spore/Meta/Method.pm51
-rw-r--r--t/spore-method/payload.t17
2 files changed, 59 insertions, 9 deletions
diff --git a/lib/Net/HTTP/Spore/Meta/Method.pm b/lib/Net/HTTP/Spore/Meta/Method.pm
index 54967e9..8a1e0a4 100644
--- a/lib/Net/HTTP/Spore/Meta/Method.pm
+++ b/lib/Net/HTTP/Spore/Meta/Method.pm
@@ -50,12 +50,20 @@ has path   => ( is => 'ro', isa => 'UriPath', required => 1 );
 has method => ( is => 'ro', isa => 'Method',  required => 1 );
 has description => ( is => 'ro', isa => 'Str', predicate => 'has_description' );
 
+has required_payload => (
+    is        => 'ro',
+    isa       => 'Boolean',
+    predicate => 'payload_is_required',
+    lazy      => 1,
+    default   => 0,
+    coerce    => 1,
+);
 has authentication => (
     is        => 'ro',
     isa       => 'Boolean',
     predicate => 'has_authentication',
     default   => 0,
-    coerce => 1,
+    coerce    => 1,
 );
 has base_url => (
     is        => 'ro',
@@ -77,17 +85,24 @@ has expected_status => (
     handles    => { find_expected_status => 'grep', },
 );
 has optional_params => (
-    traits  => ['Array'],
-    is      => 'ro',
-    isa     => ArrayRef [Str],
-    predicate => 'has_optional_params',
+    traits     => ['Array'],
+    is         => 'ro',
+    isa        => ArrayRef [Str],
+    predicate  => 'has_optional_params',
     auto_deref => 1,
 );
 has required_params => (
-    traits  => ['Array'],
-    is      => 'ro',
-    isa     => ArrayRef [Str],
-    predicate => 'has_required_params',
+    traits     => ['Array'],
+    is         => 'ro',
+    isa        => ArrayRef [Str],
+    predicate  => 'has_required_params',
+    auto_deref => 1,
+);
+has form_data => (
+    traits     => ['Hash'],
+    is         => 'ro',
+    isa        => 'HashRef',
+    predicate  => 'has_form_data',
     auto_deref => 1,
 );
 has documentation => (
@@ -123,6 +138,24 @@ sub wrap {
           ? delete $method_args{spore_payload}
           : delete $method_args{payload};
 
+        if ( $payload
+            && ( $method->method ne 'POST' || $method->method ne 'PUT' ) )
+        {
+            die Net::HTTP::Spore::Response->new( 599, [],
+                { error => "payload requires a PUT or POST method" },
+            );
+        }
+
+        if ( $method->payload_is_required && !$payload ) {
+            die Net::HTTP::Spore::Response->new(
+                599,
+                [],
+                {
+                    error => "this method require a payload, and no payload is provided",
+                }
+            );
+        }
+
         if ($method->has_required_params) {
             foreach my $required ( $method->required_params ) {
                 if ( !grep { $required eq $_ } keys %method_args ) {
diff --git a/t/spore-method/payload.t b/t/spore-method/payload.t
new file mode 100644
index 0000000..07691dc
--- /dev/null
+++ b/t/spore-method/payload.t
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use Test::More;
+
+use Net::HTTP::Spore;
+
+my $api_with_payload = {
+    base_url => 'foo',
+    methods  => {
+        create_user => {
+            method           => 'POST',
+            path             => '/user',
+            payload_required => 1,
+        }
+    }
+};
+