|
NAMEMcBain::WithPSGI - Load a McBain API as a RESTful PSGI web serviceSYNOPSIS# 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; DESCRIPTION"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 }". SUPPORTED HTTP METHODSThis 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. CAVEATS AND CONSIDERATIONSThe 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"] } METHODS EXPORTED TO YOUR APINone.METHODS REQUIRED BY MCBAINinit( $target )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.generate_env( $psgi_env )Receives the PSGI env hash-ref and creates McBain's standard env hash-ref from it.generate_res( $env, $res )Converts the result of an API method to JSON, and returns a standard PSGI response array-ref.handle_exception( $err )Formats exceptions into JSON and returns a standard PSGI array-ref.CONFIGURATION AND ENVIRONMENTNo configuration files are required.DEPENDENCIES"McBain::WithPSGI" depends on the following CPAN modules:
INCOMPATIBILITIES WITH OTHER MODULESNone reported.BUGS AND LIMITATIONSPlease 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>.SUPPORTYou can find documentation for this module with the perldoc command.perldoc McBain::WithPSGI You can also look for information at:
AUTHORIdo Perlmuter <ido@ido50.net>LICENSE AND COPYRIGHTCopyright (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. DISCLAIMER OF WARRANTYBECAUSE 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.
Visit the GSP FreeBSD Man Page Interface. |