API-Client

 view release on metacpan or  search on metacpan

INSTALL  view on Meta::CPAN

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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`.

INSTALL  view on Meta::CPAN

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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:
 
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:

LICENSE  view on Meta::CPAN

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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.

LICENSE  view on Meta::CPAN

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                    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.

LICENSE  view on Meta::CPAN

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    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

LICENSE  view on Meta::CPAN

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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:

LICENSE  view on Meta::CPAN

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
  - "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:

LICENSE  view on Meta::CPAN

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
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

META.json  view on Meta::CPAN

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
   "license" : [
      "perl_5"
   ],
   "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",
         "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"
}

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
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:
  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:
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'

README  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    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

README  view on Meta::CPAN

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    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
     
      my $users = $client->resource('users');
     
      my $user = $client->resource('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 'https://metacpan.org/pod/API::Client">API::Client';
     
      sub auth {

README  view on Meta::CPAN

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
      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.

README  view on Meta::CPAN

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    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.

README  view on Meta::CPAN

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
        
        $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.

README.md  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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:
 
 

README.md  view on Meta::CPAN

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
 
# 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
 
    my $users = $client->resource('users');
 
    my $user = $client->resource('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 'https://metacpan.org/pod/API::Client">API::Client';
 
    sub auth {

README.md  view on Meta::CPAN

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    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.

README.md  view on Meta::CPAN

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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.

README.md  view on Meta::CPAN

436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        $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.

cpanfile  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}
 
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

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  $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

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  $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

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
=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

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
=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

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
=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
 
  my $users = $client->resource('users');
 
  my $user = $client->resource('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

508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  sub base {
  }
 
  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

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
=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

858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
  );
 
=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

920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  $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

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
=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

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
+=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
 
  my $users = $client->resource('users');
 
  my $user = $client->resource('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

300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  }
 
  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

468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  $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

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
=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;
 
 
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;



( run in 0.643 second using v1.01-cache-2.11-cpan-87723dcf8b7 )