summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-24 11:06:31 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-24 11:06:31 +0200
commitec48e1ecc95c17e792b4f576c585275d4e871e4d (patch)
tree7d1c71bccaf62b27a9d9e478b06b6470fd96f6ca
parentparams can be strict or no strict (diff)
downloadmoosex-net-api-ec48e1ecc95c17e792b4f576c585275d4e871e4d.tar.gz
add strict and alter what params_in_url does
-rw-r--r--lib/MooseX/Net/API/Meta/Method.pm60
-rw-r--r--lib/MooseX/Net/API/Role/Request.pm17
-rw-r--r--t/01_basic.t35
-rw-r--r--t/lib/TestAPI.pm16
4 files changed, 87 insertions, 41 deletions
diff --git a/lib/MooseX/Net/API/Meta/Method.pm b/lib/MooseX/Net/API/Meta/Method.pm
index 8643152..ddd84f9 100644
--- a/lib/MooseX/Net/API/Meta/Method.pm
+++ b/lib/MooseX/Net/API/Meta/Method.pm
@@ -10,43 +10,22 @@ use MooseX::Types::Moose qw/Str Int ArrayRef/;
 
 extends 'Moose::Meta::Method';
 
-subtype UriPath => as 'Str' => where { $_ =~ m!^/! } =>
-  message {"path must start with /"};
+subtype UriPath
+    => as 'Str'
+    => where { $_ =~ m!^/! }
+    => message {"path must start with /"};
 
 enum Method => qw(HEAD GET POST PUT DELETE);
 
-has method => (
-    is       => 'ro',
-    isa      => 'Method',
-    required => 1
-);
-has path => (
-    is       => 'ro',
-    isa      => 'UriPath',
-    required => 1,
-    coerce   => 1
-);
-has description => (
-    is        => 'ro',
-    isa       => 'Str',
-    predicate => 'has_description'
-);
-has params_in_url => (
-    is        => 'ro',
-    isa       => 'Bool',
-    predicate => 'has_params_in_url',
-    default   => 0
-);
-has strict => (
+has path   => (is => 'ro', isa => 'UriPath', required => 1, coerce => 1);
+has method => (is => 'ro', isa => 'Method', required => 1);
+has description => (is => 'ro', isa => 'Str',  predicate => 'has_description');
+has strict      => (is => 'ro', isa => 'Bool', default   => 1,);
+has authentication => (
     is => 'ro',
     isa => 'Bool',
-    default => 1,
-);
-has authentication => (
-    is        => 'ro',
-    isa       => 'Bool',
     predicate => 'has_authentication',
-    default   => 0
+    default => 0
 );
 has expected => (
     traits     => ['Array'],
@@ -66,6 +45,15 @@ has params => (
     auto_deref => 1,
     handles    => {find_request_parameter => 'first',}
 );
+has params_in_url => (
+    traits     => ['Array'],
+    is         => 'ro',
+    isa        => ArrayRef [Str],
+    required   => 0,
+    default    => sub { [] },
+    auto_deref => 0,
+    handles => {find_request_url_parameters => 'first'}
+);
 has required => (
     traits     => ['Array'],
     is         => 'ro',
@@ -106,7 +94,7 @@ before wrap => sub {
         foreach my $required ( @{ $args{required} } ) {
             die MooseX::Net::API::Error->new( reason =>
                     "$required is required but is not declared in params" )
-                if ( !grep { $_ eq $required } @{ $args{params} } );
+                if ( !grep { $_ eq $required } @{ $args{params} }, @{$args{params_in_url}} );
         }
     }
 };
@@ -176,7 +164,9 @@ sub _check_params_before_run {
 
     # check if there is no undeclared param
     foreach my $arg (keys %$args) {
-        if (!$self->find_request_parameter(sub {/$arg/})) {
+        if (   !$self->find_request_parameter(sub {/$arg/})
+            && !$self->find_request_url_parameters(sub {/$arg/}))
+        {
             die MooseX::Net::API::Error->new(
                 reason => "'$arg' is not declared as a param");
         }
@@ -208,10 +198,10 @@ sub _build_path {
             $path =~ s/(?:\$|:)$match/$value/;
         }
         if ($max_iter > $i) {
-            $path =~ s/(?:\/)?(?:\$|:)(\w+)$//;
+            $path =~ s/(?:\/)?(?:$|:)(?:.*)$//;
         }
     }
-    $path =~ s/(?:\/)?(?:\$|\\:)(\w+)$//;
+    $path =~ s/(?:\/)?(?:$|:)(?:.*)$//;
     return $path;
 }
 
diff --git a/lib/MooseX/Net/API/Role/Request.pm b/lib/MooseX/Net/API/Role/Request.pm
index 5adb43c..13221d3 100644
--- a/lib/MooseX/Net/API/Role/Request.pm
+++ b/lib/MooseX/Net/API/Role/Request.pm
@@ -28,18 +28,23 @@ sub http_request {
 
     my $request;
 
-    if ( $method =~ /^(?:GET|DELETE)$/ || $params_in_url ) {
+    if ($method =~ /^(?:GET|DELETE)$/) {
         $uri->query_form(%$args);
-        $request = HTTP::Request->new( $method => $uri );
+        $request = HTTP::Request->new($method => $uri);
     }
-    elsif ( $method =~ /^(?:POST|PUT)$/ ) {
-        $request = HTTP::Request->new( $method => $uri );
+    elsif ($method =~ /^(?:POST|PUT)$/) {
+        my $params = {};
+        foreach my $key (@$params_in_url) {
+            $params->{$key} = $args->{$key} if exists $args->{$key};
+        }
+        $uri->query_form(%$params) if $params;
+
+        $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" );
+        die MooseX::Net::API::Error->new(reason => "$method is not defined");
     }
 
     $request->header(
diff --git a/t/01_basic.t b/t/01_basic.t
index d21e930..0958c36 100644
--- a/t/01_basic.t
+++ b/t/01_basic.t
@@ -93,4 +93,39 @@ $api->api_useragent->add_handler(
 
 ($content, $res) = $api->delete_user(name => 'eris');
 is $res->code, 204, 'code as expected';
+
+# unstrict parameters
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        my $res = HTTP::Response->new(200);
+        $res;
+    }
+);
+
+($content, $res) = $api->unstrict_users(
+    name         => 'eris',
+    last_name    => 'foo',
+    random_stuff => 'bar'
+);
+is $res->code, 200, 'code as expected';
+is $res->request->uri,
+  'http://exemple.com/users/unstrict.json?random_stuff=bar&name=eris&last_name=foo',
+  'url is ok with no declared parameters';
+
+# params in url and body
+$api->api_useragent->remove_handler('request_send');
+$api->api_useragent->add_handler(
+    'request_send' => sub {
+        my $request = shift;
+        my $res = HTTP::Response->new(200);
+        $res;
+    }
+);
+
+($content, $res) = $api->params_users(name => 'eris', bod => '01/01/1970');
+is $res->code, 200, 'code as expected';
+is $res->request->uri, 'http://exemple.com/users.json?bod=01%2F01%2F1970', 'url is ok';
+
 done_testing;
diff --git a/t/lib/TestAPI.pm b/t/lib/TestAPI.pm
index a2aed55..45d3cb2 100644
--- a/t/lib/TestAPI.pm
+++ b/t/lib/TestAPI.pm
@@ -46,4 +46,20 @@ net_api_method delete_user => (
     expected => [qw/204/],
 );
 
+net_api_method unstrict_users => (
+    method   => 'GET',
+    path     => '/users/unstrict',
+    strict   => 0,
+    params   => [qw/name/],
+    required => [qw/name/],
+);
+
+net_api_method params_users => (
+    method        => 'POST',
+    path          => '/users/',
+    params        => [qw/name/],
+    params_in_url => [qw/bod/],
+    required      => [qw/bod name/],
+);
+
 1;