API-Client
view release on metacpan or search on metacpan
lib/API/Client.pm view on Meta::CPAN
The thin API client library is advantageous as it has complete API coverage and
can easily adapt to changes in the API with minimal effort. As a thin-client
superclass, this module does not map specific HTTP requests to specific
routines, nor does it provide parameter validation, pagination, or other
conventions found in typical API client implementations; Instead, it simply
provides a simple and consistent mechanism for dynamically generating HTTP
requests. Additionally, this module has support for debugging and retrying API
calls as well as throwing exceptions when 4xx and 5xx server response codes are
returned.
=cut
=head1 INTEGRATES
This package integrates behaviors from:
L<Data::Object::Role::Buildable>
L<Data::Object::Role::Stashable>
L<Data::Object::Role::Throwable>
=cut
=head1 LIBRARIES
This package uses type constraints from:
L<Types::Standard>
=cut
=head1 SCENARIOS
This package supports the following scenarios:
=cut
=head2 building
# given: synopsis
my $resource = $client->resource('get');
# GET /get
my $get = $client->resource('get')->dispatch;
# HEAD /head
my $head = $client->resource('head')->dispatch(
method => 'head'
);
# PATCH /patch
my $patch = $client->resource('patch')->dispatch(
method => 'patch'
);
[$get, $head, $patch]
Building up an HTTP request is extremely easy, simply call the L</resource> to
create a new object instance representing the API endpoint you wish to issue a
request against.
=cut
=head2 chaining
# given: synopsis
# https://httpbin.org/users
my $users = $client->resource('users');
# https://httpbin.org/users/c09e91a
my $user = $client->resource('users', 'c09e91a');
# https://httpbin.org/users/c09e91a
my $new_user = $users->resource('c09e91a');
[$users, $user, $new_user]
Because each call to L</resource> returns a new object instance configured with
a path (resource locator) based on the supplied parameters, reuse and request
isolation are made simple, i.e., you will only need to configure the client
once in your application.
=cut
=head2 creating
# given: synopsis
my $tx1 = $client->resource('post')->create(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('post')->dispatch(
method => 'post',
json => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might create a new API resource.
=cut
=head2 deleting
# given: synopsis
my $tx1 = $client->resource('delete')->delete(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('delete')->dispatch(
method => 'delete',
json => {active => 1}
lib/API/Client.pm view on Meta::CPAN
=back
=cut
=head2 prepare
prepare(Object $ua, Object $tx, Any %args) : Object
The prepare method acts as a C<before> hook triggered before each request where
you can modify the transactor objects.
=over 4
=item prepare example #1
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->prepare(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
=back
=cut
=head2 process
process(Object $ua, Object $tx, Any %args) : Object
The process method acts as an C<after> hook triggered after each response where
you can modify the transactor objects.
=over 4
=item process example #1
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->process(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
=back
=cut
=head2 resource
resource(Str @segments) : Object
The resource method returns a new instance of the object for the API resource
endpoint specified.
=over 4
=item resource example #1
# given: synopsis
$client->resource('status', 200);
=back
=cut
=head2 serialize
serialize() : HashRef
The serialize method serializes and returns the object as a C<hashref>.
=over 4
=item serialize example #1
# given: synopsis
$client->serialize;
=back
=cut
=head2 update
update(Any %args) : InstanceOf["Mojo::Transaction"]
The update method issues a C<PUT> request to the API resource represented by
the object.
=over 4
=item update example #1
# given: synopsis
$client->resource('put')->update(
json => {active => 1}
);
=back
=cut
=head1 AUTHOR
Al Newkirk, C<awncorp@cpan.org>
=head1 LICENSE
Copyright (C) 2011-2019, Al Newkirk, et al.
( run in 0.740 second using v1.01-cache-2.11-cpan-0bd6704ced7 )