summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-06-02 11:52:22 +0200
committerfranck cuny <franck@lumberjaph.net>2010-06-02 11:52:22 +0200
commitb9d5222b73faf3577833008e32d55d16d5e4818b (patch)
tree01bda6b60ad4e54e270e90555c947b45b4f76ad3
parentuse MX::Net::API::Parser to handle serialization (diff)
downloadmoosex-net-api-b9d5222b73faf3577833008e32d55d16d5e4818b.tar.gz
move content deserialization to role
-rw-r--r--lib/MooseX/Net/API/Meta/Method.pm34
-rw-r--r--lib/MooseX/Net/API/Role/Serialization.pm24
2 files changed, 33 insertions, 25 deletions
diff --git a/lib/MooseX/Net/API/Meta/Method.pm b/lib/MooseX/Net/API/Meta/Method.pm
index 12001c4..03e0612 100644
--- a/lib/MooseX/Net/API/Meta/Method.pm
+++ b/lib/MooseX/Net/API/Meta/Method.pm
@@ -67,8 +67,8 @@ sub wrap {
 
             $method->_validate_before_execute(\%method_args);
 
-            my $path = $method->build_path(\%method_args);
-            my $local_url = $method->build_uri($self, $path);
+            my $path = $method->_build_path(\%method_args);
+            my $local_url = $method->_build_uri($self, $path);
 
             my $result = $self->http_request(
                 $method->method => $local_url,
@@ -76,7 +76,7 @@ sub wrap {
             );
 
             my $code = $result->code;
-            
+
             if ($method->has_expected
                 && !$method->find_expected_code(sub {/$code/}))
             {
@@ -86,23 +86,7 @@ sub wrap {
                 );
             }
 
-            my $content_type = $self->api_format
-              // $result->header('Content-Type');
-            $content_type =~ s/(;.+)$//;
-
-            my $content;
-            if ($result->is_success && $code != 204) {
-                my @deserialize_order = ($content_type, $self->api_format);
-                $content =
-                  $self->deserialize($result->content, \@deserialize_order);
-
-                if (!$content) {
-                    die MooseX::Net::API::Error->new(
-                        reason     => "can't deserialize content",
-                        http_error => $result,
-                    );
-                }
-            }
+            my $content = $self->get_content($result);;
 
             if ($result->is_success) {
                 if (wantarray) {
@@ -144,12 +128,12 @@ sub _validate_required_before_install {
 
 sub _validate_before_execute {
     my ($self, $args) = @_;
-    for my $method (qw/check_params_before_run check_required_before_run/) {
+    for my $method (qw/_check_params_before_run _check_required_before_run/) {
         $self->$method($args);
     }
 }
 
-sub check_params_before_run {
+sub _check_params_before_run {
     my ($self, $args) = @_;
 
     # check if there is no undeclared param
@@ -161,7 +145,7 @@ sub check_params_before_run {
     }
 }
 
-sub check_required_before_run {
+sub _check_required_before_run {
     my ($self, $args) = @_;
 
     # check if all our params declared as required are present
@@ -173,7 +157,7 @@ sub check_required_before_run {
     }
 }
 
-sub build_path {
+sub _build_path {
     my ($self, $args) = @_;
     my $path = $self->path;
 
@@ -192,7 +176,7 @@ sub build_path {
     return $path;
 }
 
-sub build_uri {
+sub _build_uri {
     my ($method, $self, $path) = @_;
 
     my $local_url     = $self->api_base_url->clone;
diff --git a/lib/MooseX/Net/API/Role/Serialization.pm b/lib/MooseX/Net/API/Role/Serialization.pm
index b36955c..b813b03 100644
--- a/lib/MooseX/Net/API/Role/Serialization.pm
+++ b/lib/MooseX/Net/API/Role/Serialization.pm
@@ -1,7 +1,10 @@
 package MooseX::Net::API::Role::Serialization;
 
+use 5.010;
+
 use Try::Tiny;
 use Moose::Role;
+use MooseX::Net::API::Error;
 
 has serializers => (
     traits     => ['Hash'],
@@ -15,6 +18,27 @@ has serializers => (
     },
 );
 
+sub get_content {
+    my ($self, $result) = @_;
+
+    my $content_type = $self->api_format // $result->header('Content-Type');
+    $content_type =~ s/(;.+)$//;
+
+    my $content;
+    if ($result->is_success && $result->code != 204) {
+        my @deserialize_order = ($content_type, $self->api_format);
+        $content = $self->deserialize($result->content, \@deserialize_order);
+
+        if (!$content) {
+            die MooseX::Net::API::Error->new(
+                reason     => "can't deserialize content",
+                http_error => $result,
+            );
+        }
+    }
+    $content;
+}
+
 sub deserialize {
     my ($self, $content, $list_of_formats) = @_;