summary refs log tree commit diff
path: root/lib/MooseX/Net/API/Role
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MooseX/Net/API/Role')
-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) = @_;