Rest::Client::Builder - Base class to build simple object-oriented REST clients
package Your::API; use base qw(Rest::Client::Builder); use JSON; sub new { my ($class) = @_; my $self; $self = $class->SUPER::new({ on_request => sub { return $self->request(@_); }, }, 'http://hostname/api'); return bless($self, $class); }; sub request { my ($self, $method, $path, $args) = @_; return sprintf("%s %s %s\n", $method, $path, encode_json($args)); } my $api = Your::API->new(); print $api->resource->get({ value => 1 }); # output: GET http://hostname/api/resource {"value":1} print $api->resource(10)->post({ value => 1 }); # output: POST http://hostname/api/resource/10 {"value":1} print $api->resource(10)->subresource('alfa', 'beta')->state->put({ value => 1 }); # output: PUT http://hostname/api/resource/10/subresource/alfa/beta/state {"value":1} print $api->resource(10)->subresource->alfa('beta')->state->put({ value => 1 }); # output: PUT http://hostname/api/resource/10/subresource/alfa/beta/state {"value":1} print $api->resource(10)->subresource->alfa->beta->state->put({ value => 1 }); # output: PUT http://hostname/api/resource/10/subresource/alfa/beta/state {"value":1} print $api->resource(10)->delete(); # output: DELETE http://hostname/api/resource/10
get put post delete patch head
You can pass any additionals arguments to the on_request callback:
sub request { my ($self, $method, $path, $args, $opts) = @_; } my $api = Your::API->new(); $api->resource->get({ value => 1 }, { timeout => 1 });
You can override any methods of any API object:
package Your::API; use base qw(Rest::Client::Builder); use JSON; sub new { my ($class) = @_; my $self; $self = $class->SUPER::new({ on_request => sub { return $self->request(@_); }, }, 'http://hostname/api'); return bless($self, $class); }; sub request { my ($self, $method, $path, $args) = @_; return encode_json($args); } package Your::API::resource::state; sub post { my ($self, $args) = (shift, shift); $args->{force} = 1; return $self->SUPER::post($args, @_); } my $api = Your::API->new(); print $api->resource(1)->state->post(); # output: {"force":1}
WWW::REST REST::Client
You can find documentation for this module with the perldoc command.
perldoc Rest::Client::Builder
You can also look for information at:
Code Repository at GitHub
http://github.com/alexey-komarov/Rest-Client-Builder
GitHub Issue Tracker
http://github.com/alexey-komarov/Rest-Client-Builder/issues
RT, CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rest-Client-Builder
Alexey A. Komarov <alexkom@cpan.org>
2014 Alexey A. Komarov
This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
To install Rest::Client::Builder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Rest::Client::Builder
CPAN shell
perl -MCPAN -e shell install Rest::Client::Builder
For more information on module installation, please visit the detailed CPAN module installation guide.