view release on metacpan or search on metacpan
details on enabling it in your environment.
## Installing with the CPAN shell
Alternatively, if your CPAN shell is set up, you should just be able to do:
% cpan API::Client
## Manual installation
As a last resort, you can manually install it. Download the tarball, untar it,
install configure prerequisites (see below), then build it:
% perl Makefile.PL
% make && make test
Then install it:
% 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
This distribution may require additional modules to be installed after running
Makefile.PL.
Look for prerequisites in the following phases:
* to run make, PHASE = build
* to use the module code itself, PHASE = runtime
* to run tests, PHASE = test
They can all be found in the "PHASE_requires" key of MYMETA.yml or the
"{prereqs}{PHASE}{requires}" key of MYMETA.json.
## Documentation
API-Client documentation is available as POD.
You can run `perldoc` from a shell to read the documentation:
% perldoc API::Client
For more information on installing Perl modules via CPAN, please see:
https://www.cpan.org/modules/INSTALL.html
software and to any other program whose authors commit to using it.
You can use it for your programs, too.
When we speak of free software, we are referring to freedom, not
price. Specifically, the General Public License is designed to make
sure that you have the freedom to give away or sell copies of free
software, that you receive source code or can get it if you want it,
that you can change the software or use pieces of it in new free
programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of a such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must tell them their rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any program or other work which
contains a notice placed by the copyright holder saying it may be
distributed under the terms of this General Public License. The
"Program", below, refers to any such program or work, and a "work based
on the Program" means either the Program or any work containing the
Program or a portion of it, either verbatim or with modifications. Each
licensee is addressed as "you".
1. You may copy and distribute verbatim copies of the Program's source
code as you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this
General Public License and to the absence of any warranty; and give any
other recipients of the Program a copy of this General Public License
along with the Program. You may charge a fee for the physical act of
transferring a copy.
exchange for a fee.
Mere aggregation of another independent work with the Program (or its
derivative) on a volume of a storage or distribution medium does not bring
the other work under the scope of these terms.
3. You may copy and distribute the Program (or a portion or derivative of
it, under Paragraph 2) in object code or executable form under the terms of
Paragraphs 1 and 2 above provided that you also do one of the following:
a) accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of
Paragraphs 1 and 2 above; or,
b) accompany it with a written offer, valid for at least three
years, to give any third party free (except for a nominal charge
for the cost of distribution) a complete machine-readable copy of the
corresponding source code, to be distributed under the terms of
Paragraphs 1 and 2 above; or,
c) accompany it with the information you received as to where the
corresponding source code may be obtained. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form alone.)
Source code for a work means the preferred form of the work for making
modifications to it. For an executable file, complete source code means
all the source code for all modules it contains; but, as a special
exception, it need not include source code for modules which are standard
libraries that accompany the operating system on which the executable
file runs, or for standard header files or definitions files that
accompany that operating system.
4. You may not copy, modify, sublicense, distribute or transfer the
Program except as expressly provided under this General Public License.
Any attempt otherwise to copy, modify, sublicense, distribute or transfer
the Program is void, and will automatically terminate your rights to use
the Program under this License. However, parties who have received
copies, or rights to use copies, from you under this General Public
License will not have their licenses terminated so long as such parties
remain in full compliance.
5. By copying, distributing or modifying the Program (or any work based
on the Program) you indicate your acceptance of this license to do so,
and all its terms and conditions.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these
terms and conditions. You may not impose any further restrictions on the
recipients' exercise of the rights granted herein.
7. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of the license which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
the license, you may choose any version ever published by the Free Software
Foundation.
8. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "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
The hypothetical commands `show w' and `show c' should show the
appropriate parts of the General Public License. Of course, the
commands you use may be called something other than `show w' and `show
c'; they could even be mouse-clicks or menu items--whatever suits your
program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
program `Gnomovision' (a program to direct compilers to make passes
at assemblers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
That's all there is to it!
--- The Artistic License 1.0 ---
This software is Copyright (c) 2019 by Al Newkirk.
This is free software, licensed under:
- "Reasonable copying fee" is whatever you can justify on the basis of media
cost, duplication charges, time of people involved, and so on. (You will
not be required to justify it to the Copyright Holder, but only to the
computing community at large as a market that must bear the fee.)
- "Freely Available" means that no fee is charged for the item itself, though
there may be fees involved in handling the item. It also means that
recipients of the item may redistribute it under the same conditions they
received it.
1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.
2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:
4. You may distribute the programs of this Package in object code or executable
form, provided that you do at least ONE of the following:
a) distribute a Standard Version of the executables and library files,
together with instructions (in the manual page or equivalent) on where to
get the Standard Version.
b) accompany the distribution with the machine-readable source of the Package
with your modifications.
c) accompany any non-standard executables with their corresponding Standard
Version executables, giving the non-standard executables non-standard
names, and clearly documenting the differences in manual pages (or
equivalent), together with instructions on where to get the Standard
Version.
d) make other distribution arrangements with the Copyright Holder.
5. You may charge a reasonable copying fee for any distribution of this
Package. You may charge any fee you choose for support of this Package. You
may not charge a fee for this Package itself. However, you may distribute this
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : 2
},
"name" : "API-Client",
"prereqs" : {
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {
"Data::Object::Class" : "2.02",
"Data::Object::ClassHas" : "2.01",
"Data::Object::Role::Buildable" : "0.03",
"Data::Object::Role::Stashable" : "2.01",
"Data::Object::Role::Throwable" : "2.01",
"FlightRecorder" : "0.03",
"Mojolicious" : "8.35",
"perl" : "5.014",
"routines" : "0",
"strict" : "0",
"warnings" : "0"
}
},
"test" : {
"requires" : {
"Data::Object::Class" : "2.02",
"Data::Object::ClassHas" : "2.01",
"Data::Object::Role::Buildable" : "0.03",
"Data::Object::Role::Stashable" : "2.01",
"Data::Object::Role::Throwable" : "2.01",
"FlightRecorder" : "0.03",
"Mojolicious" : "8.35",
"Test::Auto" : "0.10",
"perl" : "5.014",
"routines" : "0",
"strict" : "0",
"warnings" : "0"
}
}
},
"release_status" : "stable",
"resources" : {
"repository" : {
"type" : "git",
"url" : "git://github.com/iamalnewkirk/API-Client.git",
"web" : "https://github.com/iamalnewkirk/API-Client"
}
},
"version" : "0.12",
"x_authority" : "cpan:AWNCORP",
"x_contributors" : [
"Al Newkirk <anewkirk@ana.io>"
],
"x_generated_by_perl" : "v5.26.1",
"x_serialization_backend" : "Cpanel::JSON::XS version 4.19",
"x_spdx_expression" : "Artistic-1.0-Perl OR GPL-1.0-or-later"
}
---
abstract: 'HTTP API Thin-Client Abstraction'
author:
- 'Al Newkirk <awncorp@cpan.org>'
build_requires:
Data::Object::Class: '2.02'
Data::Object::ClassHas: '2.01'
Data::Object::Role::Buildable: '0.03'
Data::Object::Role::Stashable: '2.01'
Data::Object::Role::Throwable: '2.01'
FlightRecorder: '0.03'
Mojolicious: '8.35'
Test::Auto: '0.10'
perl: '5.014'
routines: '0'
strict: '0'
warnings: '0'
configure_requires:
ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.014, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: API-Client
requires:
Data::Object::Class: '2.02'
Data::Object::ClassHas: '2.01'
Data::Object::Role::Buildable: '0.03'
Data::Object::Role::Stashable: '2.01'
Data::Object::Role::Throwable: '2.01'
FlightRecorder: '0.03'
Mojolicious: '8.35'
perl: '5.014'
routines: '0'
strict: '0'
warnings: '0'
resources:
repository: git://github.com/iamalnewkirk/API-Client.git
version: '0.12'
x_authority: cpan:AWNCORP
x_contributors:
- 'Al Newkirk <anewkirk@ana.io>'
x_generated_by_perl: v5.26.1
x_serialization_backend: 'YAML::Tiny version 1.73'
x_spdx_expression: 'Artistic-1.0-Perl OR GPL-1.0-or-later'
HTTP API Thin-Client Abstraction
SYNOPSIS
package main;
use API::Client;
my $client = API::Client->new(url => 'https://httpbin.org');
# $client->resource('post');
# $client->update(json => {...});
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.
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.
INTEGRATES
This package integrates behaviors from:
Data::Object::Role::Buildable
Data::Object::Role::Stashable
Types::Standard
SCENARIOS
This package supports the following scenarios:
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
"resource" to create a new object instance representing the API
endpoint you wish to issue a request against.
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 "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.
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.
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}
);
[$tx1, $tx2]
This example illustrates how you might delete a new API resource.
fetching
# given: synopsis
my $tx1 = $client->resource('get')->fetch(
query => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('get')->dispatch(
method => 'get',
query => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might fetch an API resource.
subclassing
package Hookbin;
use Data::Object::Class;
extends 'API::Client';
sub auth {
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
my $tx1 = $client->resource('patch')->patch(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('patch')->dispatch(
method => 'patch',
json => {active => 1}
);
[$tx1, $tx2]
An HTTP request is only issued when the "dispatch" method is called,
directly or indirectly. Those calls return a Mojo::Transaction object
which provides access to the request and response objects.
updating
# given: synopsis
my $tx1 = $client->resource('put')->update(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('put')->dispatch(
method => 'put',
json => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might update a new API resource.
ATTRIBUTES
This package has the following attributes:
debug
debug(Bool)
This attribute is read-only, accepts (Bool) values, and is optional.
This attribute is read-only, accepts (Str) values, and is optional.
METHODS
This package implements the following methods:
create
create(Any %args) : InstanceOf["Mojo::Transaction"]
The create method issues a POST request to the API resource represented
by the object.
create example #1
# given: synopsis
$client->resource('post')->create(
json => {active => 1}
);
delete
delete(Any %args) : InstanceOf["Mojo::Transaction"]
The delete method issues a DELETE request to the API resource
represented by the object.
delete example #1
# given: synopsis
$client->resource('delete')->delete;
dispatch
dispatch(Str :$method = 'get', Any %args) : InstanceOf["Mojo::Transaction"]
The dispatch method issues a request to the API resource represented by
the object.
dispatch example #1
# given: synopsis
$client->resource('get')->dispatch;
dispatch example #2
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', body => 'active=1'
);
dispatch example #3
# given: synopsis
$client->resource('get')->dispatch(
method => 'get', query => {active => 1}
);
dispatch example #4
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', json => {active => 1}
);
dispatch example #5
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', form => {active => 1}
);
dispatch example #6
# given: synopsis
$client->resource('put')->dispatch(
method => 'put', json => {active => 1}
);
dispatch example #7
# given: synopsis
$client->resource('patch')->dispatch(
method => 'patch', json => {active => 1}
);
dispatch example #8
# given: synopsis
$client->resource('delete')->dispatch(
method => 'delete', json => {active => 1}
);
fetch
fetch(Any %args) : InstanceOf["Mojo::Transaction"]
The fetch method issues a GET request to the API resource represented
by the object.
fetch example #1
# given: synopsis
$client->resource('get')->fetch;
patch
patch(Any %args) : InstanceOf["Mojo::Transaction"]
The patch method issues a PATCH request to the API resource represented
by the object.
patch example #1
# given: synopsis
$client->resource('patch')->patch(
json => {active => 1}
);
prepare
prepare(Object $ua, Object $tx, Any %args) : Object
The prepare method acts as a before hook triggered before each request
where you can modify the transactor objects.
$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.
resource example #1
# given: synopsis
$client->resource('status', 200);
serialize
serialize() : HashRef
The serialize method serializes and returns the object as a hashref.
serialize example #1
# given: synopsis
$client->serialize;
update
update(Any %args) : InstanceOf["Mojo::Transaction"]
The update method issues a PUT request to the API resource represented
by the object.
update example #1
# given: synopsis
$client->resource('put')->update(
json => {active => 1}
);
AUTHOR
Al Newkirk, awncorp@cpan.org
LICENSE
Copyright (C) 2011-2019, Al Newkirk, et al.
HTTP API Thin-Client Abstraction
# SYNOPSIS
package main;
use API::Client;
my $client = API::Client->new(url => 'https://httpbin.org');
# $client->resource('post');
# $client->update(json => {...});
# 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.
# 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.
# INTEGRATES
This package integrates behaviors from:
[Data::Object::Role::Buildable](https://metacpan.org/pod/Data::Object::Role::Buildable)
[Data::Object::Role::Stashable](https://metacpan.org/pod/Data::Object::Role::Stashable)
[Types::Standard](https://metacpan.org/pod/Types::Standard)
# SCENARIOS
This package supports the following scenarios:
## 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 ["resource"](#resource) to
create a new object instance representing the API endpoint you wish to issue a
request against.
## 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 ["resource"](#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.
## 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.
## 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}
);
[$tx1, $tx2]
This example illustrates how you might delete a new API resource.
## fetching
# given: synopsis
my $tx1 = $client->resource('get')->fetch(
query => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('get')->dispatch(
method => 'get',
query => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might fetch an API resource.
## subclassing
package Hookbin;
use Data::Object::Class;
extends 'API::Client';
sub auth {
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
my $tx1 = $client->resource('patch')->patch(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('patch')->dispatch(
method => 'patch',
json => {active => 1}
);
[$tx1, $tx2]
An HTTP request is only issued when the ["dispatch"](#dispatch) method is called, directly
or indirectly. Those calls return a [Mojo::Transaction](https://metacpan.org/pod/Mojo::Transaction) object which provides
access to the `request` and `response` objects.
## updating
# given: synopsis
my $tx1 = $client->resource('put')->update(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('put')->dispatch(
method => 'put',
json => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might update a new API resource.
# ATTRIBUTES
This package has the following attributes:
## debug
debug(Bool)
This attribute is read-only, accepts `(Bool)` values, and is optional.
This attribute is read-only, accepts `(Str)` values, and is optional.
# METHODS
This package implements the following methods:
## create
create(Any %args) : InstanceOf["Mojo::Transaction"]
The create method issues a `POST` request to the API resource represented by
the object.
- create example #1
# given: synopsis
$client->resource('post')->create(
json => {active => 1}
);
## delete
delete(Any %args) : InstanceOf["Mojo::Transaction"]
The delete method issues a `DELETE` request to the API resource represented by
the object.
- delete example #1
# given: synopsis
$client->resource('delete')->delete;
## dispatch
dispatch(Str :$method = 'get', Any %args) : InstanceOf["Mojo::Transaction"]
The dispatch method issues a request to the API resource represented by the
object.
- dispatch example #1
# given: synopsis
$client->resource('get')->dispatch;
- dispatch example #2
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', body => 'active=1'
);
- dispatch example #3
# given: synopsis
$client->resource('get')->dispatch(
method => 'get', query => {active => 1}
);
- dispatch example #4
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', json => {active => 1}
);
- dispatch example #5
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', form => {active => 1}
);
- dispatch example #6
# given: synopsis
$client->resource('put')->dispatch(
method => 'put', json => {active => 1}
);
- dispatch example #7
# given: synopsis
$client->resource('patch')->dispatch(
method => 'patch', json => {active => 1}
);
- dispatch example #8
# given: synopsis
$client->resource('delete')->dispatch(
method => 'delete', json => {active => 1}
);
## fetch
fetch(Any %args) : InstanceOf["Mojo::Transaction"]
The fetch method issues a `GET` request to the API resource represented by the
object.
- fetch example #1
# given: synopsis
$client->resource('get')->fetch;
## patch
patch(Any %args) : InstanceOf["Mojo::Transaction"]
The patch method issues a `PATCH` request to the API resource represented by
the object.
- patch example #1
# given: synopsis
$client->resource('patch')->patch(
json => {active => 1}
);
## prepare
prepare(Object $ua, Object $tx, Any %args) : Object
The prepare method acts as a `before` hook triggered before each request where
you can modify the transactor objects.
$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.
- resource example #1
# given: synopsis
$client->resource('status', 200);
## serialize
serialize() : HashRef
The serialize method serializes and returns the object as a `hashref`.
- serialize example #1
# given: synopsis
$client->serialize;
## update
update(Any %args) : InstanceOf["Mojo::Transaction"]
The update method issues a `PUT` request to the API resource represented by
the object.
- update example #1
# given: synopsis
$client->resource('put')->update(
json => {active => 1}
);
# AUTHOR
Al Newkirk, `awncorp@cpan.org`
# LICENSE
Copyright (C) 2011-2019, Al Newkirk, et al.
# This file is generated by Dist::Zilla::Plugin::CPANFile v6.014
# Do not edit this file directly. To change prereqs, edit the `dist.ini` file.
requires "Data::Object::Class" => "2.02";
requires "Data::Object::ClassHas" => "2.01";
requires "Data::Object::Role::Buildable" => "0.03";
requires "Data::Object::Role::Stashable" => "2.01";
requires "Data::Object::Role::Throwable" => "2.01";
requires "FlightRecorder" => "0.03";
requires "Mojolicious" => "8.35";
requires "perl" => "5.014";
requires "routines" => "0";
requires "strict" => "0";
requires "warnings" => "0";
on 'test' => sub {
requires "Data::Object::Class" => "2.02";
requires "Data::Object::ClassHas" => "2.01";
requires "Data::Object::Role::Buildable" => "0.03";
requires "Data::Object::Role::Stashable" => "2.01";
requires "Data::Object::Role::Throwable" => "2.01";
requires "FlightRecorder" => "0.03";
requires "Mojolicious" => "8.35";
requires "Test::Auto" => "0.10";
requires "perl" => "5.014";
requires "routines" => "0";
requires "strict" => "0";
requires "warnings" => "0";
};
on 'configure' => sub {
requires "ExtUtils::MakeMaker" => "0";
};
lib/API/Client.pm view on Meta::CPAN
}
method delete(Any %args) {
return $self->dispatch(%args, method => 'delete');
}
method dispatch(Str :$method = 'get', Any %args) {
my $log = $self->logger->info("@{[uc($method)]} @{[$self->url->to_string]}");
my $result = $self->execute(%args, method => $method);
$log->end;
return $result;
}
method fetch(Any %args) {
return $self->dispatch(%args, method => 'get');
}
method patch(Any %args) {
return $self->dispatch(%args, method => 'patch');
lib/API/Client.pm view on Meta::CPAN
$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;
$url->path->merge(
join '/', '', @{$self->url->path->parts}, @segments
);
}
lib/API/Client.pm view on Meta::CPAN
$ua->on(prepare => fun ($ua, $tx) {
$self->prepare($ua, $tx, %args);
});
# client timeouts
$ua->max_redirects(0);
$ua->connect_timeout($self->timeout);
$ua->request_timeout($self->timeout);
# 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) {
my $log = $self->logger;
my $msg = join " ", "attempt", ("#".($i+1)), ": $method", $url->to_string;
$log->debug("req: $msg")->data({
request => $req->to_string =~ s/\s*$/\n\n\n/r
});
$log->debug("res: $msg")->data({
response => $res->to_string =~ s/\s*$/\n\n\n/r
});
# output to the console where applicable
$log->info("res: $msg [@{[$res->code]}]");
$log->output if $self->debug;
}
# no retry necessary
last if $ok;
}
# throw exception if fatal is truthy
if ($req && $res && $self->fatal && !$ok) {
my $code = $res->code;
$self->stash(tx => $tx);
$self->throw([$code, uc "${code}_http_response"]);
}
# return transaction
return $tx;
}
1;
=encoding utf8
lib/API/Client.pm view on Meta::CPAN
=cut
=head1 SYNOPSIS
package main;
use API::Client;
my $client = API::Client->new(url => 'https://httpbin.org');
# $client->resource('post');
# $client->update(json => {...});
=cut
=head1 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
lib/API/Client.pm view on Meta::CPAN
=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
=head1 INTEGRATES
This package integrates behaviors from:
L<Data::Object::Role::Buildable>
lib/API/Client.pm view on Meta::CPAN
=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}
);
[$tx1, $tx2]
This example illustrates how you might delete a new API resource.
=cut
=head2 fetching
# given: synopsis
my $tx1 = $client->resource('get')->fetch(
query => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('get')->dispatch(
method => 'get',
query => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might fetch an API resource.
=cut
=head2 subclassing
package Hookbin;
use Data::Object::Class;
extends 'API::Client';
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
=head2 transacting
# given: synopsis
my $tx1 = $client->resource('patch')->patch(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('patch')->dispatch(
method => 'patch',
json => {active => 1}
);
[$tx1, $tx2]
An HTTP request is only issued when the L</dispatch> method is called, directly
or indirectly. Those calls return a L<Mojo::Transaction> object which provides
access to the C<request> and C<response> objects.
=cut
=head2 updating
# given: synopsis
my $tx1 = $client->resource('put')->update(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('put')->dispatch(
method => 'put',
json => {active => 1}
);
[$tx1, $tx2]
This example illustrates how you might update a new API resource.
=cut
=head1 ATTRIBUTES
This package has the following attributes:
=cut
=head2 debug
lib/API/Client.pm view on Meta::CPAN
=head1 METHODS
This package implements the following methods:
=cut
=head2 create
create(Any %args) : InstanceOf["Mojo::Transaction"]
The create method issues a C<POST> request to the API resource represented by
the object.
=over 4
=item create example #1
# given: synopsis
$client->resource('post')->create(
json => {active => 1}
);
=back
=cut
=head2 delete
delete(Any %args) : InstanceOf["Mojo::Transaction"]
The delete method issues a C<DELETE> request to the API resource represented by
the object.
=over 4
=item delete example #1
# given: synopsis
$client->resource('delete')->delete;
=back
=cut
=head2 dispatch
dispatch(Str :$method = 'get', Any %args) : InstanceOf["Mojo::Transaction"]
The dispatch method issues a request to the API resource represented by the
object.
=over 4
=item dispatch example #1
# given: synopsis
$client->resource('get')->dispatch;
=back
=over 4
=item dispatch example #2
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', body => 'active=1'
);
=back
=over 4
=item dispatch example #3
# given: synopsis
$client->resource('get')->dispatch(
method => 'get', query => {active => 1}
);
=back
=over 4
=item dispatch example #4
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', json => {active => 1}
);
=back
=over 4
=item dispatch example #5
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', form => {active => 1}
);
=back
=over 4
=item dispatch example #6
# given: synopsis
$client->resource('put')->dispatch(
method => 'put', json => {active => 1}
);
=back
=over 4
=item dispatch example #7
# given: synopsis
$client->resource('patch')->dispatch(
method => 'patch', json => {active => 1}
);
=back
=over 4
=item dispatch example #8
# given: synopsis
$client->resource('delete')->dispatch(
method => 'delete', json => {active => 1}
);
=back
=cut
=head2 fetch
fetch(Any %args) : InstanceOf["Mojo::Transaction"]
The fetch method issues a C<GET> request to the API resource represented by the
object.
=over 4
=item fetch example #1
# given: synopsis
$client->resource('get')->fetch;
=back
=cut
=head2 patch
patch(Any %args) : InstanceOf["Mojo::Transaction"]
The patch method issues a C<PATCH> request to the API resource represented by
the object.
=over 4
=item patch example #1
# given: synopsis
$client->resource('patch')->patch(
json => {active => 1}
);
=back
=cut
=head2 prepare
prepare(Object $ua, Object $tx, Any %args) : Object
lib/API/Client.pm view on Meta::CPAN
);
=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>.
lib/API/Client.pm view on Meta::CPAN
$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>
t/API_Client.t view on Meta::CPAN
=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;
use API::Client;
my $client = API::Client->new(url => 'https://httpbin.org');
# $client->resource('post');
# $client->update(json => {...});
=cut
=libraries
Types::Standard
=cut
t/API_Client.t view on Meta::CPAN
+=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}
);
# is equivalent to
my $tx2 = $client->resource('get')->dispatch(
method => 'get',
query => {active => 1}
);
[$tx1, $tx2]
=cut
=scenario creating
This example illustrates how you might create a new API resource.
=example 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]
=cut
=scenario updating
This example illustrates how you might update a new API resource.
=example updating
# given: synopsis
my $tx1 = $client->resource('put')->update(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('put')->dispatch(
method => 'put',
json => {active => 1}
);
[$tx1, $tx2]
=cut
=scenario deleting
This example illustrates how you might delete a new API resource.
=example 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}
);
[$tx1, $tx2]
=cut
=scenario transacting
An HTTP request is only issued when the L</dispatch> method is called, directly
or indirectly. Those calls return a L<Mojo::Transaction> object which provides
access to the C<request> and C<response> objects.
=example transacting
# given: synopsis
my $tx1 = $client->resource('patch')->patch(
json => {active => 1}
);
# is equivalent to
my $tx2 = $client->resource('patch')->dispatch(
method => 'patch',
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
package Hookbin;
t/API_Client.t view on Meta::CPAN
}
package main;
my $hookbin = Hookbin->new;
=cut
=method dispatch
The dispatch method issues a request to the API resource represented by the
object.
=signature dispatch
dispatch(Str :$method = 'get', Any %args) : InstanceOf["Mojo::Transaction"]
=example-1 dispatch
# given: synopsis
$client->resource('get')->dispatch;
=example-2 dispatch
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', body => 'active=1'
);
=example-3 dispatch
# given: synopsis
$client->resource('get')->dispatch(
method => 'get', query => {active => 1}
);
=example-4 dispatch
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', json => {active => 1}
);
=example-5 dispatch
# given: synopsis
$client->resource('post')->dispatch(
method => 'post', form => {active => 1}
);
=example-6 dispatch
# given: synopsis
$client->resource('put')->dispatch(
method => 'put', json => {active => 1}
);
=example-7 dispatch
# given: synopsis
$client->resource('patch')->dispatch(
method => 'patch', json => {active => 1}
);
=example-8 dispatch
# given: synopsis
$client->resource('delete')->dispatch(
method => 'delete', json => {active => 1}
);
=cut
=method create
The create method issues a C<POST> request to the API resource represented by
the object.
=signature create
create(Any %args) : InstanceOf["Mojo::Transaction"]
=example-1 create
# given: synopsis
$client->resource('post')->create(
json => {active => 1}
);
=cut
=method delete
The delete method issues a C<DELETE> request to the API resource represented by
the object.
=signature delete
delete(Any %args) : InstanceOf["Mojo::Transaction"]
=example-1 delete
# given: synopsis
$client->resource('delete')->delete;
=cut
=method fetch
The fetch method issues a C<GET> request to the API resource represented by the
object.
=signature fetch
fetch(Any %args) : InstanceOf["Mojo::Transaction"]
=example-1 fetch
# given: synopsis
$client->resource('get')->fetch;
=cut
=method patch
The patch method issues a C<PATCH> request to the API resource represented by
the object.
=signature patch
patch(Any %args) : InstanceOf["Mojo::Transaction"]
=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.
t/API_Client.t view on Meta::CPAN
$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
t/API_Client.t view on Meta::CPAN
=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) {
my $test = testauto(__FILE__);
my $subs = $test->standard;
$subs->synopsis(fun($tryable) {
ok my $result = $tryable->result;
$result
});
$subs->scenario('building', fun($tryable) {
require Scalar::Util;
ok my $result = $tryable->result;
my $get = $result->[0];
my $head = $result->[1];
my $patch = $result->[2];
isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($head);
isnt Scalar::Util::refaddr($get), Scalar::Util::refaddr($patch);
isnt Scalar::Util::refaddr($head), Scalar::Util::refaddr($patch);
is $get->req->method, 'get';
is $head->req->method, 'head';
is $patch->req->method, 'patch';
});
$subs->scenario('chaining', fun($tryable) {
require Scalar::Util;
ok my $result = $tryable->result;
my $users = $result->[0];
my $user = $result->[1];
my $new_user = $result->[2];
isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($user);
isnt Scalar::Util::refaddr($users), Scalar::Util::refaddr($new_user);
isnt Scalar::Util::refaddr($user), Scalar::Util::refaddr($new_user);
is $users->url->to_string, 'https://httpbin.org/users';
is $user->url->to_string, 'https://httpbin.org/users/c09e91a';
is $new_user->url->to_string, 'https://httpbin.org/users/c09e91a';
});
$subs->scenario('fetching', fun($tryable) {
ok my $result = $tryable->result;
;
});
$subs->scenario('creating', fun($tryable) {
ok my $result = $tryable->result;
;
});
$subs->scenario('updating', fun($tryable) {
ok my $result = $tryable->result;
;
});
$subs->scenario('deleting', fun($tryable) {
ok my $result = $tryable->result;
;
});
$subs->scenario('transacting', fun($tryable) {
ok my $result = $tryable->result;
;
});
$subs->scenario('subclassing', fun($tryable) {
ok my $result = $tryable->result;
ok $result->isa('Hookbin');
ok $result->isa('API::Client');
is_deeply $result->auth, ['admin', 'secret'];
is_deeply $result->headers, [['Accept', '*/*']];
is_deeply $result->base, ['https://httpbin.org/get'];
is $result->url->to_string, 'https://httpbin.org/get';
is $result->name, 'Hookbin (0.01)';
});
$subs->example(-1, 'create', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'post';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
$subs->example(-1, 'delete', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'delete';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, undef;
is_deeply $json->{form}, {};
is $json->{data}, '';
$result
});
$subs->example(-1, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'get';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{args}, {};
$result
});
$subs->example(-2, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'post';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is $json->{data}, "active=1";
$result
});
$subs->example(-3, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'get';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{args}, {active => 1};
$result
});
$subs->example(-4, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'post';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
$subs->example(-5, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'post';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is $json->{data}, "active=1";
$result
});
$subs->example(-6, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'put';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
$subs->example(-7, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'patch';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
$subs->example(-8, 'dispatch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'delete';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
$subs->example(-1, 'fetch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'get';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, undef;
is_deeply $json->{form}, undef;
is $json->{data}, undef;
$result
});
$subs->example(-1, 'patch', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'patch';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$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\)/;
is $result->retries, 0;
is $result->timeout, 10;
is $result->url->to_string, 'https://httpbin.org/status/200';
$result
});
$subs->example(-1, 'serialize', '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\)/;
is $result->{retries}, 0;
is $result->{timeout}, 10;
is $result->{url}, 'https://httpbin.org';
$result
});
$subs->example(-1, 'update', 'method', fun($tryable) {
ok my $result = $tryable->result;
my $req = $result->req;
is lc($req->method), 'put';
my $res = $result->res;
is $res->code, 200;
my $json = $res->json;
is $json->{headers}{'Host'}, 'httpbin.org';
is $json->{headers}{'Content-Type'}, 'application/json';
is_deeply $json->{json}, {active => 1};
$result
});
}
skip 'Unable to connect to HTTPBin' if $skip_tests;
}
ok 1 and done_testing;