summaryrefslogtreecommitdiff
path: root/lib/Net/HTTP/API/Role/Serialization.pm
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-07-16 16:10:41 +0200
committerfranck cuny <franck@lumberjaph.net>2010-07-16 16:10:41 +0200
commite97449eaa8bd3a408763057f9ca2253d93e2a3d0 (patch)
tree4fd0dc6ed899efe361e314adfc913b9aa6b0a1a4 /lib/Net/HTTP/API/Role/Serialization.pm
parentcheck if auth_method is declared (diff)
downloadnet-http-api-e97449eaa8bd3a408763057f9ca2253d93e2a3d0.tar.gz
rename from mx::net::api to net::http::api
Diffstat (limited to 'lib/Net/HTTP/API/Role/Serialization.pm')
-rw-r--r--lib/Net/HTTP/API/Role/Serialization.pm104
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/Net/HTTP/API/Role/Serialization.pm b/lib/Net/HTTP/API/Role/Serialization.pm
new file mode 100644
index 0000000..37d2603
--- /dev/null
+++ b/lib/Net/HTTP/API/Role/Serialization.pm
@@ -0,0 +1,104 @@
+package Net::HTTP::API::Role::Serialization;
+
+# ABSTRACT: do (de)serialization
+
+use 5.010;
+
+use Try::Tiny;
+use Moose::Role;
+use Net::HTTP::API::Error;
+
+has serializers => (
+ traits => ['Hash'],
+ is => 'rw',
+ isa => 'HashRef[Net::HTTP::API::Parser]',
+ default => sub { {} },
+ auto_deref => 1,
+ handles => {
+ _add_serializer => 'set',
+ _get_serializer => 'get',
+ },
+);
+
+sub get_content {
+ my ($self, $result) = @_;
+
+ return undef unless $result->content;
+
+ 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 Net::HTTP::API::Error->new(
+ reason => "can't deserialize content",
+ http_error => $result,
+ );
+ }
+ }
+ $content;
+}
+
+sub deserialize {
+ my ($self, $content, $list_of_formats) = @_;
+
+ foreach my $format (@$list_of_formats) {
+ my $s = $self->_get_serializer($format)
+ || $self->_load_serializer($format);
+ next unless $s;
+ my $result;
+ try { $result = $s->decode($content) };
+ return $result if $result;
+ }
+}
+
+sub serialize {
+ my ($self, $content) = @_;
+ my $s = $self->_get_serializer($self->api_format)
+ || $self->_load_serializer();
+ my $result = try { $s->encode($content) };
+ return $result if $result;
+}
+
+sub _load_serializer {
+ my $self = shift;
+ my $format = shift || $self->api_format;
+ my $parser = "Net::HTTP::API::Parser::" . uc($format);
+ if (Class::MOP::load_class($parser)) {
+ my $o = $parser->new;
+ $self->_add_serializer($format => $o);
+ return $o;
+ }
+}
+
+1;
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head2 ATTRIBUTES
+
+=over 4
+
+=item B<serializers>
+
+=back
+
+=head2 METHODS
+
+=over 4
+
+=item B<get_content>
+
+=item B<serialize>
+
+=item B<deserialize>
+
+=back
+99: hit eof while in pod documentation (no =cut seen)
+ this can cause trouble with some pod utilities