API-Client
view release on metacpan or search on metacpan
t/API_Client.t view on Meta::CPAN
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;
use Data::Object::Class;
extends 'API::Client';
sub auth {
['admin', 'secret']
}
sub headers {
[['Accept', '*/*']]
}
sub base {
['https://httpbin.org/get']
}
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(
t/API_Client.t view on Meta::CPAN
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) {
( run in 0.720 second using v1.01-cache-2.11-cpan-39bf76dae61 )