diff options
author | franck cuny <franck@lumberjaph.net> | 2010-06-02 11:52:22 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-06-02 11:52:22 +0200 |
commit | b9d5222b73faf3577833008e32d55d16d5e4818b (patch) | |
tree | 01bda6b60ad4e54e270e90555c947b45b4f76ad3 /lib/MooseX/Net/API/Role | |
parent | use MX::Net::API::Parser to handle serialization (diff) | |
download | moosex-net-api-b9d5222b73faf3577833008e32d55d16d5e4818b.tar.gz |
move content deserialization to role
Diffstat (limited to '')
-rw-r--r-- | lib/MooseX/Net/API/Role/Serialization.pm | 24 |
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) = @_; |