| PX-API documentation | Contained in the PX-API distribution. |
PX::API::Response - A Peekshows Web Services API response.
use PX::API;
my $px = PX::API->new({
api_key => '13243432434', #Your api key
secret => 's33cr3tttt', #Your api secret
});
my $response = $px->call('px.test.echo',{
arg1 => 'val1',
arg2 => 'val2',
});
A response object from the Peekshows Web Services API. PX::API::Response
is a subclass of HTTP::Response allowing for access to any response
parameters. Module::Pluggable::Object is used to allow an extensible
'plugin' style method for loading response modules.
Along with the response parameters available from HTTP::Response,
the following parameters are added for API responses.
successSet to a 1 or 0 to signify response success or error respectively.
responseThe actual content of the response returned from the API call.
err_codeThe error code returned from the API call, if an error occurred.
err_stringA description of the error returned from the API call, if an error occurred.
A response plugin is simply a module that transforms the API response
format into a usable perl object. Obviously this is not always necessary
and the original structure is always available via $response-{_content}>.
Response plugins are required to have to following methods available via its public api:
format()The format() method is used by PX::API::Response to match a plugin
with the format argument sent to the Peekshows API. This method simply
needs to return the name of the plugin format. ie: 'rest', 'json'
parse($content)The parse() method is called upon completion of an API call and is
passed the _content returned from the call. This method need only
return the perl object which was created from parsing the content.
Anthony Decena <anthony@1bci.com>
Copyright (c) 2007, Anthony Decena <anthony@1bci.com>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
| PX-API documentation | Contained in the PX-API distribution. |
package PX::API::Response; use warnings; use strict; use Carp; use version; our $VERSION = qv('0.0.3'); use HTTP::Response; our @ISA = qw(HTTP::Response); use Module::Pluggable::Fast name => 'response_plugins', search => [ qw/PX::API::Response/ ]; sub new { my $class = shift; my $args = shift; my $self = HTTP::Response->new(); return bless $self, $class; } sub _init { my $self = shift; my $args = shift; my $format = $args->{'format'} || "rest"; my @classes = $self->response_plugins; foreach my $c(@classes) { if ($c->format eq $format) { $self->{'parser'} = $c; last; } } return $self; } sub fault { my ($self,$err_code,$err_string) = @_; $self->{success} = 0; $self->{err_code} = $err_code; $self->{err_string} = $err_string; } sub success { my ($self,$ref) = @_; $self->{success} = 1; $self->{response} = $ref; } 1; __END__