NAME
Dancer::Plugin::RESTModel - REST model class for Dancer apps
SYNOPSIS
set the REST endpoint in your Dancer configuration file:
plugins:
RESTModel:
MyData:
server: http://localhost:5000
type: application/json
clientattrs:
timeout: 5
then use it from any of your routes/controllers:
use Dancer ':syntax';
use Dancer::Plugin::RESTModel;
get '/' => sub {
my $res = model('MyData')->post( 'foo/bar/baz', { meep => 'moop' } );
my $code = $res->code; # e.g. 200
my $data = $res->data;
...
};
DESCRIPTION
This plugin lets you talk to a REST server as a separate model from within your Dancer app. It is useful for keeping your API decoupled from your app while still being able to manage it through the configuration file.
It is a thin wrapper over Role::REST::Client.
INTERFACE
model()
The exported model()
function returns a REST Model object which provides the standard HTTP 1.1 verbs as methods:
post
get
put
delete
options
head
All methods take these parameters:
url - the REST service being accessed
data - The data structure to send (hashref, arrayref). The data will be encoded according to the value of the type attribute
args - optional hashref with arguments to augment the way the call is handled. It currently provides the 'deserializer' key to change the deserializer if you know that the response's content-type is incorrect, and also the 'preserve_headers' which, if set to true, will keep the headers between calls:
my $res = model('MyData')->post( 'users/123', { foo => 'bar' }, { deserializer => 'application/yaml', preserve_headers => 1 }, );
The third parameter, args, is an optional hashref which lets you give extra information to the object. It currently provides
CONFIGURATION AND ENVIRONMENT
server
type
user_agent
httpheaders
persistent_headers
clientattrs
BUGS AND LIMITATIONS
Please report any bugs or feature requests to bug-dancer-plugin-restmodel@rt.cpan.org
, or through the web interface at http://rt.cpan.org.
SEE ALSO
AUTHOR
Breno G. de Oliveira <garu@cpan.org>
LICENCE AND COPYRIGHT
Copyright (c) 2013-2014, Breno G. de Oliveira <garu@cpan.org>
. 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.
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.