API-Client
view release on metacpan or search on metacpan
t/API_Client.t view on Meta::CPAN
my $client = API::Client->new(url => 'https://httpbin.org');
# $client->resource('post');
# $client->update(json => {...});
=cut
=libraries
Types::Standard
=cut
=integrates
Data::Object::Role::Buildable
Data::Object::Role::Stashable
Data::Object::Role::Throwable
=cut
=attributes
debug: ro, opt, Bool
fatal: ro, opt, Bool
logger: ro, opt, InstanceOf["FlightRecorder"]
name: ro, opt, Str
retries: ro, opt, Int
timeout: ro, opt, Int
url: ro, opt, InstanceOf["Mojo::URL"]
user_agent: ro, opt, InstanceOf["Mojo::UserAgent"]
version: ro, opt, Str
=cut
=description
This package provides an abstraction and method for rapidly developing HTTP API
clients. While this module can be used to interact with APIs directly,
API::Client was designed to be consumed (subclassed) by higher-level
purpose-specific API clients.
+=head1 THIN CLIENT
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
=scenario building
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.
=example 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]
=cut
=scenario chaining
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.
=example 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]
=cut
=scenario fetching
This example illustrates how you might fetch an API resource.
=example fetching
# given: synopsis
my $tx1 = $client->resource('get')->fetch(
query => {active => 1}
);
t/API_Client.t view on Meta::CPAN
=example-1 patch
# given: synopsis
$client->resource('patch')->patch(
json => {active => 1}
);
=cut
=method prepare
The prepare method acts as a C<before> hook triggered before each request where
you can modify the transactor objects.
=signature prepare
prepare(Object $ua, Object $tx, Any %args) : Object
=example-1 prepare
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->prepare(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
=cut
=method process
The process method acts as an C<after> hook triggered after each response where
you can modify the transactor objects.
=signature process
process(Object $ua, Object $tx, Any %args) : Object
=example-1 process
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->process(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
=cut
=method resource
The resource method returns a new instance of the object for the API resource
endpoint specified.
=signature resource
resource(Str @segments) : Object
=example-1 resource
# given: synopsis
$client->resource('status', 200);
=cut
=method serialize
The serialize method serializes and returns the object as a C<hashref>.
=signature serialize
serialize() : HashRef
=example-1 serialize
# given: synopsis
$client->serialize;
=cut
=method update
The update method issues a C<PUT> request to the API resource represented by
the object.
=signature update
update(Any %args) : InstanceOf["Mojo::Transaction"]
=example-1 update
# given: synopsis
$client->resource('put')->update(
json => {active => 1}
);
=cut
package main;
use Mojo::UserAgent;
SKIP: {
my $skip_tests = do {
my $tx = Mojo::UserAgent->new->get('https://httpbin.org/anything');
!eval{$tx->result->is_success};
};
unless ($skip_tests) {
( run in 0.808 second using v1.01-cache-2.11-cpan-0068ddc7af1 )