JSON-API

 view release on metacpan or  search on metacpan

lib/JSON/API.pm  view on Meta::CPAN

    print $api->errstr . "\n";
  }

=head1 DESCRIPTION

This module wraps JSON and LWP::UserAgent to create a flexible utility
for accessing APIs that accept/provide JSON data.

It supports all the options LWP supports, including authentication.

=head1 METHODS

=head2 new

Creates a new JSON::API object for connecting to any API that accepts
and provide JSON data.

Example:

	my $api = JSON::API->new("https://myapp.com:8443/path/to/app",
		user => 'foo',
		pass => 'bar',
		realm => 'my_protected_site',
		agent => 'MySpecialBrowser/1.0',
		cookie_jar => '/tmp/cookie_jar',
	);

Parameters:

=over

=item base_url

The base URL to apply to all requests you send this api, for example:

https://myapp.com:8443/path/to/app

=item parameters

This is a hash of options that can be passed in to an LWP object.
Additionally, the B<user>, B<pass>, and B<realm> may be provided
to configure authentication for LWP. You must provide all three parameters
for authentication to work properly.

Specifying debug => 1 in the parameters hash will also enable debugging output
within JSON::API.

Additionally you can specify predecodehook in the parameters hash with a
reference to a subroutine. The subroutine will then be called with the received
raw content as only parameter before it is decoded. It then can preprocess the
content e.g. alter it to be valid json. An example use case for this is calling
a JSON API that prefixes the json with garbage to prevent CSRF. The pre-decode
hook can then strip the garbage from the raw content before the JSON data is
being decoded.

=back

=head2 get|post|patch|put|del

Perform an HTTP action (GET|POST|PATCH|PUT|DELETE) against the given API. All methods
take the B<path> to the API endpoint as the first parameter. The B<patch()>, B<put()> and
B<post()> methods also accept a second B<data> parameter, which should be a reference
to be serialized into JSON for POST/PATCH/PUTing to the endpoint.

All methods also accept an optional B<apphdr> parameter in the last position, which
is a hashref.  The referenced hash contains header names and values that will be
submitted with the request.  See HTTP::Headers.  This can be used to provide
B<If-Modified> or other headers required by the API.

If called in scalar context, returns the deserialized JSON content returned by
the server. If no content was returned, returns an empty hashref. To check for errors,
call B<errstr> or B<was_success>.

If called in list context, returns a two-value array. The first value will be the
HTTP response code for the request. The second value will either be the deserialized
JSON data. If no data is returned, returns an empty hashref.

=head2 get

Performs an HTTP GET on the given B<path>. B<path> will be appended to the
B<base_url> provided when creating this object. If given a B<data> object,
this will be turned into querystring parameters, with URI encoded values.

  my $obj = $api->get('/objects/1');
  # Automatically add + encode querystring params
  my $obj = $api->get('/objects/1', { param => 'value' });

=head2 patch

Performs an HTTP PATCH on the given B<path>, with the provided B<data>. Like
B<get>, this will append path to the end of the B<base_url>.

  $api->patch('/objects/', $obj);

=head2 put

Performs an HTTP PUT on the given B<path>, with the provided B<data>. Like
B<get>, this will append path to the end of the B<base_url>.

  $api->put('/objects/', $obj);

=head2 post

Performs an HTTP POST on the given B<path>, with the provided B<data>. Like
B<get>, this will append path to the end of the B<base_url>.

  $api->post('/objects/', [$obj1, $obj2]);

=head2 del

Performs an HTTP DELETE on the given B<path>. Like B<get>, this will append
path to the end of the B<base_url>.

  $api->del('/objects/first');

=head2 response

Returns the last C<HTTP::Response>, or undef if none or if the last request
didn't generate one. This can be used to obtain detailed status.

=head2 header

With no argument, C<header> returns a list of the header fields in the last response.



( run in 0.568 second using v1.01-cache-2.11-cpan-524268b4103 )