|
NAMEJSON::API - Module to interact with a JSON APISYNOPSISuse JSON::API; my $api = JSON::API->new("http://myapp.com/"); my $obj = { name => 'foo', type => 'bar' }; if ($api->put("/add/obj", $obj) { print "Success!\n"; } else { print $api->errstr . "\n"; } DESCRIPTIONThis 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. METHODSnewCreates 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:
get|post|put|delPerform an HTTP action (GET|POST|PUT|DELETE) against the given API. All methods take the path to the API endpoint as the first parameter. The put() and post() methods also accept a second data parameter, which should be a reference to be serialized into JSON for POST/PUTing to the endpoint.All methods also accept an optional 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 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 errstr or 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. getPerforms an HTTP GET on the given path. path will be appended to the base_url provided when creating this object. If given a 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' }); putPerforms an HTTP PUT on the given path, with the provided data. Like get, this will append path to the end of the base_url.$api->put('/objects/', $obj); postPerforms an HTTP POST on the given path, with the provided data. Like get, this will append path to the end of the base_url.$api->post('/objects/', [$obj1, $obj2]); delPerforms an HTTP DELETE on the given path. Like get, this will append path to the end of the base_url.$api->del('/objects/first'); responseReturns the last "HTTP::Response", or undef if none or if the last request didn't generate one. This can be used to obtain detailed status.headerWith no argument, "header" returns a list of the header fields in the last response. If a field name is specified, returns the value(s) of the named field. A multi-valued field will be returned comma-separated in scalar context, or as separate values in list context. See "HTTP::Header".This snippet can be used to dump all the response headers: print "$_ => ", scalar $api->header($_), "\n" foreach ($api->header); errstrReturns the current error string for the last call.was_successReturns whether or not the last request was successful.urlReturns the complete URL of a request, when given a path.EXAMPLESThis is a more advanced example of accessing the GitHub API. It uses a custom request header and conditional GET requests for efficiency. It falls-back to unconditional GET when necessary.This code uses constants and methods from "IO::SOCKET::SSL" and "Storable". Error handling and logging have been omitted for clarity. my $repo = eval { lock_retrieve( "repo.status" ) }; my $api = JSON::API->new( 'https://api.github.com/repos/user/app', agent => "$prog/$VERSION", protocols_allowed => [ qw/https/ ], env_proxy => 1, ssl_opts => { verify_hostname => $vhost || 0, SSL_verify_mode => ( $vhost? SSL_VERIFY_PEER : SSL_VERIFY_NONE ) }, ); my($rc, $tags) = ( $repo && $repo->{tags_etag} )? $api->get( '/tags', undef, { Accept => 'application/vnd.github.v3+json', If_None_Match => $repo->{tags_etag}, } ) : $api->get( '/tags', undef, { Accept => 'application/vnd.github.v3+json' } ); unless( ref $tags && $api->was_success ) { exit( 1 ); } if( $api->can( 'header' ) ) { if( $rc == HTTP_NOT_MODIFIED ) { $tags = $repo->{tags}; } else { $repo ||= {}; $repo->{tags_etag} = $api->header( 'ETag' ); $repo->{tags} = $tags; eval { lock_store( $repo, 'repo.status' ) }; } } REPOSITORY<https://github.com/geofffranks/json-api>AUTHORGeoff Franks <gfranks@cpan.org> COPYRIGHTCopyright 2014, Geoff FranksThis library is licensed under the GNU General Public License 3.0
Visit the GSP FreeBSD Man Page Interface. |