GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
McBain::WithPSGI(3) User Contributed Perl Documentation McBain::WithPSGI(3)

McBain::WithPSGI - Load a McBain API as a RESTful PSGI web service

        # write your API as you normally would, and create
        # a simple psgi file:

        #!/usr/bin/perl -w

        use warnings;
        use strict;
        use MyAPI -withPSGI;

        my $app = MyAPI->to_app;

"McBain::WithPSGI" turns your McBain API into a RESTful PSGI web service based on Plack, thus making "McBain" a web application framework.

The created web service will be a JSON-in JSON-out service. Requests to your application are expected to have a "Content-Type" of "application/json; charset=UTF-8". The JSON body of a request will be the payload. The results of the API will be formatted into JSON as well.

Note that if an API method does not return a hash-ref, this runner module will automatically turn it into a hash-ref to ensure that conversion into JSON will be possible. The created hash-ref will have one key - holding the method's name, with whatever was returned from the method as its value. For example, if method "GET:/divide" in topic "/math" returns an integer (say 7), then the client will get the JSON "{ "GET:/math/divide": 7 }".

This runner support all methods natively supported by McBain. That is: "GET", "PUT", "POST", "DELETE" and "OPTIONS". To add support for "HEAD" requests, use Plack::Middleware::Head.

The "OPTIONS" method is special. It returns a list of all HTTP methods allowed by a specific route (in the "Allow" header). The response body will be the same hash-ref returned by "McBain" for "OPTIONS" requests, JSON encoded.

The HTTP protocol does not allow "GET" requests to have content, so your "GET" routes will not be able to receive parameters from a request's JSON body as all other methods do. If your "GET" routes must receive parameters (for example, you might have a route that returns a list of objects with support for pagination), "McBain::WithPSGI" supports parameters from the query string. They will be validated like all parameters, and they can be used in non-"GET" requests too. Note that they take precedence over body parameters.

The downside to this is that the parameters cannot be complex structures, though if the query string defines a certain key several times, its generated value will be an array reference. For example, let's look at the following route:

        get '/params_from_query' => (
                params => {
                        some_string => { required => 1 },
                        some_array => { array => 1, min_length => 2 }
                },
                cb => sub {
                        my ($api, $params) = @_;
                        return $params;
                }
        );

This route isn't particularly interesting, as it simply returns the parameters it receives. It does, however, enforce the existence of the "some_string" parameter, and expects "some_array" to be an array reference of at least 2 items. A request to "/params_from_query?some_string=this_is_my_string&some_array=Hello&some_array=World" will yield the following result:

        { "some_string": "this_is_my_string", "some_array": ["Hello", "World"] }

None.

Makes the root topic of your API inherit Plack::Component, so that it officially becomes a Plack app. This will provide your API with the "to_app()" method.

Receives the PSGI env hash-ref and creates McBain's standard env hash-ref from it.

Converts the result of an API method to JSON, and returns a standard PSGI response array-ref.

Formats exceptions into JSON and returns a standard PSGI array-ref.

No configuration files are required.

"McBain::WithPSGI" depends on the following CPAN modules:
  • Carp
  • JSON::MaybeXS
  • Plack

None reported.

Please report any bugs or feature requests to "bug-McBain-WithPSGI@rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=McBain-WithPSGI>.

You can find documentation for this module with the perldoc command.

        perldoc McBain::WithPSGI

You can also look for information at:

  • RT: CPAN's request tracker

    <http://rt.cpan.org/NoAuth/Bugs.html?Dist=McBain-WithPSGI>

  • AnnoCPAN: Annotated CPAN documentation

    <http://annocpan.org/dist/McBain-WithPSGI>

  • CPAN Ratings

    <http://cpanratings.perl.org/d/McBain-WithPSGI>

  • Search CPAN

    <http://search.cpan.org/dist/McBain-WithPSGI/>

Ido Perlmuter <ido@ido50.net>

Copyright (c) 2013-2015, Ido Perlmuter "ido@ido50.net".

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either version 5.8.1 or any later version. See perlartistic and perlgpl.

The full text of the license can be found in the LICENSE file included with this module.

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

2015-01-03 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.