NAME
Spica - the HTTP client for dealing with complex WEB API.
SYNOPSIS
my $spica = Spica->new(
host => 'example.com',
spec => 'Example::Spec',
);
my $iterator = $spica->fetch(client => 'list' => +{key => $value});
DESCRIPTION
Spica provides an interface to common WEB API many. It is the HTTP Client that combines the flexibility and scalability of a O/R Mapper and Model of Backbone.js.
SIMPLEST CASE
create Spica's instance. arguments host must be required. fetch returned object is Spica::Receiver::Iterator.
my $spica = Spica->new(
host => 'example.com'
);
my $iterator = $spica->fetch('/users', +{
rows => 20,
});
THE BASIC USAGE
create specifiction class.
see Spica::Spec for docs on defining spec class.
package Your::API::Spec;
use Spica::Spec::Declare;
client {
name 'example';
endpoint list => '/users' => [];
columns qw( id name message );
};
1;
in your script.
use Spica;
my $spica = Spica->new(
host => 'example.com',
spec => 'Your::API::Spec',
);
# fetching WEB API.
my $iterator = $spca->fetch('example', 'list', +{});
while (my $user = $iterator->next) {
say $user->name;
}
ARCHITECTURE
Spica iclasses are comprised of following distinct components:
CLIENT
client is a class with information about how to receipt of the request parameter data for WEB API.
client uses Spica::Spec::Iterator the receipt of data and GET request as the initial value, but I can cope with a wide range of API specification by extending in spec.
SPEC
The spec is a simple class that describes specifictions of the WEB API.
spec is a simple class that describes the specifications of the WEB API. You can extend the client by changing the receiver class you can specify the HTTP request other than GET request.
package Your::Spec;
use Spica::Spec::Declare;
client {
name 'example';
endpoint 'name1', '/path/to', [qw(column1 column2)];
endpoint 'name2', '/path/to/{replace}, [qw(replace_column column)];
endpoint 'name3', +{
method => 'POST',
path => '/path/to',
requires => [qw(column1 column2)],
};
columns qw(
column1
column2
);
}
... and other clients ...
PARSER
parser is a class for to be converted to a format that can be handled in Perl format that the API provides.
You can use an API of its own format if you extend the Spica::Parser
package Your::Parser;
use parent qw(Spica::Parser);
use Data::MessagePack;
sub parser {
my $self = shift;
return $self->{parser} ||= Data::MessagePack->new;
}
sub parse {
my ($self, $body) = @_;
return $self->parser->unpack($body);
}
1;
in your script
my $spica = Spica->new(%args);
$spica->parser('Your::Parser');
RECEIVER
receiver is a class for easier handling more data received from the WEB API.
receiver This contains the Spica::Receiver::Row and Spica::Receiver::Iterator.
METHODS
Spica provides a number of methods to all your classes,
$spica = Spica->new(%args)
Creates a new Spica instance.
my $spica = Spica->new(
host => 'example.com',
spec => 'Your::Spec',
);
Arguments can be:
-
schemeThis is the URI scheme of WEB API. By default,
httpis used. -
host:StrThis is the URI hostname of WEB API. This argument is always required.
-
port:IntThis is the URI port of WEB API. By default,
80is used. -
agent:StrThis is the Fetcher agent name of Spica. By default,
Spica $VERSIONis used. -
default_param:HashRefYou can specify the parameters common to request the WEB API.
-
default_headers:ArrayRefYou can specify the headers common to request the WEB API.
-
specspecexpecs the name of the class that inheritsSpiac::Spec. By default,specis not used. -
parserparserexpects the name of the class that inheritsSpica::Parser. By default,Spica::Parser::JSONis used. -
is_suppress_object_creationSpecifies the receiver object creation mode. By default this value is
false. If you specifies this to atruevalue, no row object will be created when a receive on WEB API results. -
no_throw_http_exceptionSpecifies the mode that does not throw the exception of HTTP. by default this value is
false.
$iterator = $spica->fetch(@args);
Request to the WEB API, to build the object. I have the interface of the following three:
$spica->fetch($client_name, $endpoint_name, $param)
It is the access method basic.
Arguments can be:
-
client_name: StrEnter the name of the client that you have defined in
spec. -
endpoint_name: StrEnter the name of
endpointthat is defined in theclient. -
param: HashRefSpecified in
HashRefthe content and query parameters required to request. I will specify the HashRef empty if there are no parameters.
$spica->fetch($client_name, $param)
You can omit the endpoint_name of fetch If you specify a string of default to name of .
Arguments can be:
client_name: Strparam: HashRef
$spica->fetch($path, $param)
You can request by specifying to fetch the spec.
Arguments can be:
path: Strparam: HashRef
SEE ALSO
AUTHOR
mizuki_r ry.mizuki@gmail.com
REPOSITORY
git clone git@github.com:rymizuki/p5-Spica.git
LICENCE AND COPYRIGHT
Copyright (c) 2013, the Spica "AUTHOR". 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.