summary refs log tree commit diff
path: root/lib/MooseX/Net/API/Role
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 /lib/MooseX/Net/API/Role
parentuse MX::Net::API::Parser to handle serialization (diff)
downloadmoosex-net-api-b9d5222b73faf3577833008e32d55d16d5e4818b.tar.gz
move content deserialization to role
Diffstat (limited to '')
-rw-r--r--lib/MooseX/Net/API/Role/Serialization.pm24
1 files changed, 24 insertions, 0 deletions
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) = @_;