NAME
Perinci::Access::Manual::Examples - Collection of examples
VERSION
This document describes version 0.45 of Perinci::Access::Manual::Examples (from Perl distribution Perinci-Access), released on 2017-07-10.
DESCRIPTION
(Client) Simplest usage
(Client) Meta action
(Client) List action
(Client) (HTTP)
(Client) (Pipe)
(Client) (Socket)
(Client) (Server) Dealing with binary data
The choice as JSON as the network transport protocol (because it is the lowest common denominator across languages like JavaScript, PHP, Python, Ruby, Perl) makes dealing with binary data requires an extra step or two. The Riap client libraries are equipped with some features to make this simpler and more convenient.
First, to make a function that accepts binary data (in its arguments), you need to specify the argument type as buf
. To return binary data as result, you need to specify result
's schema type as buf
. Example:
package MyLib;
our %SPEC;
$SPEC{gzip} = {
v => 1.1,
summary => 'Gzip some data',
args => {
data => {
summary => 'Data to compress',
schema => 'buf*',
req => 1,
},
},
};
sub gzip {
require IO::Compress::Gzip;
my %args = @_;
my $compressed;
IO::Compress::Gzip::gzip($args{data} => $compressed)
or return [500, "Compression failed"];
[200, "OK", $compressed];
}
If you use this function via Riap in-process, there's nothing to worry about since there is no round-trip to JSON. You can just:
my $res = Perinci::Access->new->request(call => "/MyLib/gzip",
{args=>{data=>"some data"}});
If you are using this function over HTTP or oher network protocol where JSON is involved, you will need to encode the argument:
use MIME::Base64;
my $res = Perinci::Access->new(riap_version=>1.2)->
request(call => "http://localhost:5000/api/MyLib/gzip",
{args=>{"data:base64"=>encode_base64("some data","")}});
Note that you also need to specify Riap version 1.2, as this is the first version that specifies argument encoding. Also note that the result returned by the server is actually base64-encoded to be safe to pass through JSON, e.g.:
[200, "OK", "base64 data...", {"riap.v"=>1.2, "riap.result_encoding"=>"base64"}]
But the Riap client library decodes this automatically for you, so you don't have to see it.
(Client) Debugging
(Server) (HTTP)
(Server) (Pipe)
(Server) Debugging
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Perinci-Access.
SOURCE
Source repository is at https://github.com/perlancar/perl-Perinci-Access.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Access
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2017, 2015, 2014, 2013, 2012 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.