NAME
UserAgent::Any::JSON – Specialization of UserAgent::Any for JSON APIs.
SYNOPSIS
my $json_client = UserAgent::Any::JSON::New->new(LWP::UserAgent->new(%options));
my $res_data = $json_client->get($url, \%url_params, \%req_data, \%headers);
DESCRIPTION
UserAgent::Any::JSON
is a generic client (user agent) for JSON based API, built on top of UserAgent::Any. As such, it supports synchronous and asynchronous calls and can use many different user agent libraries.
See the UserAgent::Any documentation for the list of supported user agents, their semantics, and limitations.
Constructor
my $json_client = UserAgent::Any::JSON->new($underlying_ua);
Builds a new UserAgent::Any::JSON
object wrapping the given underlying user agent. The wrapped object must be an instance of a supported user agent.
Alternatively, you can pass named arguments to the constructor, mainly to specify the JSON encoder and decoder to use:
my $json_client = UserAgent::Any::JSON->new(ua => $underlying_ua,
json => $json_converter);
The given $json_converter
must be an object with an encode()
and a decode()
method, acting like the equivalent methods in the JSON class. The default is just a JSON->new
object.
User agent methods
get, head, delete
my $res_data = $json_client->get($url, %headers);
$json_client->get_cb($url, %headers)->($cb);
my $promise = $json_client->get_p($url, %headers);
Execute a GET
HTTP request to the given url. The arguments are exactly the same as for the equivalent method in UserAgent::Any. The only difference is that the response is returned as a UserAgent::Any::JSON::Response object (either directly in the synchronous method or passed to the callback or the promise in the asynchronous methods).
The head
and delete
methods have the exact same behavior, but using the HEAD
and DELETE
HTTP verbs.
See the documentation of get() in UserAgent::Any for the details on the asynchronous behavior of the callback and promise versions of this method (and of all the other methods of this class).
post, patch, put
my $res_data = $json_client->post($url, %headers, \%body);
$json_client->post_cb($url, %headers, \%body)->($cb);
my $promise = $json_client->post_p($url, %headers, \%body);
This is similar to the get
method but using the POST
, PATCH
, and PUT
HTTP verbs. In addition, like the similar methods in UserAgent::Any they take a last parameter for the request body. Here, this parameter should be a reference to a Perl data-structure that will be JSON encoded with the JSON::to_json
function.
AUTHOR
Mathias Kende <mathias@cpan.org>
COPYRIGHT AND LICENSE
Copyright 2024 Mathias Kende
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.