API-Client
view release on metacpan or search on metacpan
% make install
On Windows platforms, you should use `dmake` or `nmake`, instead of `make`.
If your perl is system-managed, you can create a local::lib in your home
directory to install modules to. For details, see the local::lib documentation:
https://metacpan.org/pod/local::lib
The prerequisites of this distribution will also have to be installed manually. The
prerequisites are listed in one of the files: `MYMETA.yml` or `MYMETA.json` generated
by running the manual build process described above.
## Configure Prerequisites
This distribution requires other modules to be installed before this
distribution's installer can be run. They can be found under the
"configure_requires" key of META.yml or the
"{prereqs}{configure}{requires}" key of META.json.
## Other Prerequisites
sub base {
['https://httpbin.org/get']
}
package main;
my $hookbin = Hookbin->new;
This package was designed to be subclassed and provides hooks into the
client building and request dispatching processes. Specifically, there
are three useful hooks (i.e. methods, which if present are used to
build up the client object and requests), which are, the auth hook,
which should return a Tuple[Str, Str] which is used to configure the
basic auth header, the base hook which should return a Tuple[Str] which
is used to configure the base URL, and the headers hook, which should
return a ArrayRef[Tuple[Str, Str]] which are used to configure the HTTP
request headers.
transacting
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->prepare(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
process
process(Object $ua, Object $tx, Any %args) : Object
The process method acts as an after hook triggered after each response
where you can modify the transactor objects.
process example #1
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->process(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
resource
resource(Str @segments) : Object
The resource method returns a new instance of the object for the API
resource endpoint specified.
sub base {
['https://httpbin.org/get']
}
package main;
my $hookbin = Hookbin->new;
This package was designed to be subclassed and provides hooks into the client
building and request dispatching processes. Specifically, there are three
useful hooks (i.e. methods, which if present are used to build up the client
object and requests), which are, the `auth` hook, which should return a
`Tuple[Str, Str]` which is used to configure the basic auth header, the
`base` hook which should return a `Tuple[Str]` which is used to configure the
base URL, and the `headers` hook, which should return a
`ArrayRef[Tuple[Str, Str]]` which are used to configure the HTTP request
headers.
## transacting
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->prepare(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
## process
process(Object $ua, Object $tx, Any %args) : Object
The process method acts as an `after` hook triggered after each response where
you can modify the transactor objects.
- process example #1
# given: synopsis
require Mojo::UserAgent;
require Mojo::Transaction::HTTP;
$client->process(
Mojo::UserAgent->new,
Mojo::Transaction::HTTP->new
);
## resource
resource(Str @segments) : Object
The resource method returns a new instance of the object for the API resource
endpoint specified.
lib/API/Client.pm view on Meta::CPAN
}
method prepare(Object $ua, Object $tx, Any %args) {
$self->set_auth($ua, $tx, %args);
$self->set_headers($ua, $tx, %args);
$self->set_identity($ua, $tx, %args);
return $self;
}
method process(Object $ua, Object $tx, Any %args) {
return $self;
}
method resource(Str @segments) {
my $url;
if (@segments) {
$url = $self->url->clone;
lib/API/Client.pm view on Meta::CPAN
# transaction
my ($ok, $tx, $req, $res);
# times to retry failures
my $retries = $self->retries;
# transaction retry loop
for (my $i = 0; $i < ($retries || 1); $i++) {
# execute transaction
$tx = $ua->start($ua->build_tx($method, $url, $headers, @args));
$self->process($ua, $tx, %args);
# transaction objects
$req = $tx->req;
$res = $tx->res;
# determine success/failure
$ok = $res->code ? $res->code !~ /(4|5)\d\d/ : 0;
# log activity
if ($req && $res) {
lib/API/Client.pm view on Meta::CPAN
sub base {
['https://httpbin.org/get']
}
package main;
my $hookbin = Hookbin->new;
This package was designed to be subclassed and provides hooks into the client
building and request dispatching processes. Specifically, there are three
useful hooks (i.e. methods, which if present are used to build up the client
object and requests), which are, the C<auth> hook, which should return a
C<Tuple[Str, Str]> which is used to configure the basic auth header, the
C<base> hook which should return a C<Tuple[Str]> which is used to configure the
base URL, and the C<headers> hook, which should return a
C<ArrayRef[Tuple[Str, Str]]> which are used to configure the HTTP request
headers.
=cut
lib/API/Client.pm view on Meta::CPAN
$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
t/API_Client.t view on Meta::CPAN
=cut
=includes
method: create
method: delete
method: dispatch
method: fetch
method: patch
method: prepare
method: process
method: resource
method: serialize
method: update
=cut
=synopsis
package main;
t/API_Client.t view on Meta::CPAN
json => {active => 1}
);
[$tx1, $tx2]
=cut
=scenario subclassing
This package was designed to be subclassed and provides hooks into the client
building and request dispatching processes. Specifically, there are three
useful hooks (i.e. methods, which if present are used to build up the client
object and requests), which are, the C<auth> hook, which should return a
C<Tuple[Str, Str]> which is used to configure the basic auth header, the
C<base> hook which should return a C<Tuple[Str]> which is used to configure the
base URL, and the C<headers> hook, which should return a
C<ArrayRef[Tuple[Str, Str]]> which are used to configure the HTTP request
headers.
=example subclassing
t/API_Client.t view on Meta::CPAN
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.
t/API_Client.t view on Meta::CPAN
$result
});
$subs->example(-1, 'prepare', 'method', fun($tryable) {
ok my $result = $tryable->result;
$result
});
$subs->example(-1, 'process', 'method', fun($tryable) {
ok my $result = $tryable->result;
$result
});
$subs->example(-1, 'resource', 'method', fun($tryable) {
ok my $result = $tryable->result;
is $result->debug, 0;
is $result->fatal, 0;
like $result->name, qr/API::Client \(\d.\d\d\)/;
( run in 0.312 second using v1.01-cache-2.11-cpan-8d75d55dd25 )