Plack::App::unAPI

Serve via unAPI
Download

Plack::App::unAPI Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Jakob Voss
  • Publisher web site:
  • http://search.cpan.org/~voj/

Plack::App::unAPI Tags


Plack::App::unAPI Description

Plack::App::unAPI is a Perl module that implements an unAPI server as PSGI application. The HTTP request is routed to different PSGI applications based on the requested format.A PSGI application is a Perl code reference or an object with a call method that gets an environment variable and returns an array reference with defined structure as HTTP response.unAPI is a tiny HTTP API to query discretely identified resources in different formats. The basic idea of unAPI is having two HTTP GET query parameters: id as resource identifier format to select a formatIf no (or no supported) format is specified, a list of formats is returned as XML document.SYNOPSISCreate < app.psgi > like this: use Plack::App::unAPI; my $app1 = sub { ... }; # PSGI app that serves resource in JSON my $app2 = sub { ... }; # PSGI app that serves resource in XML my $app3 = sub { ... }; # PSGI app that serves resource in plain text unAPI json => , xml => , txt => ;Run for instance by calling plackup yourscript.psgi and retrieve: http://localhost:5000/?id=abc&format=json # calls $app1->($env); http://localhost:5000/?id=abc&format=xml # calls $app2->($env); http://localhost:5000/?id=abc&format=txt # calls $app3->($env); http://localhost:5000/ # returns list of formats http://localhost:5000/?format=xml # returns list of formats http://localhost:5000/?id=abc # returns list of formatsPSGI applications can be created as subclass of Plack::Component or as simple code reference: use Plack::Request; # PSGI application that serves resource in JSON sub get_resource_as_json { my $id = shift; ... return $json; } my $app1 = sub { my $id = Plack::Request->new(shift)->param('id') // ''; my $json = get_resource_as_json( $id ); return defined $json ? , ] : , ]; };To facilitate applications as above, Plack::App::unAPI exports the function wrAPI which can be used like this: use Plack::App::unAPI; unAPI json => wrAPI( \&get_resource_as_json => 'application/json' ), xml => wrAPI( \&get_resource_as_xml => 'application/xml' ), txt => wrAPI( \&get_resource_as_plain => 'text/plain' );Product's homepage


Plack::App::unAPI Related Software