e RPC::ExtDirect::Client;
use strict;
use warnings;
no warnings 'uninitialized';
use Carp;
use JSON;
use File::Spec;
use RPC::ExtDirect::Util ();
use RPC::ExtDirect::Config;
use RPC::ExtDirect;
,
# so that die() wouldn't munge it. Easier to do and beats stripping
# that \n any time. JSON or other packages could throw a plain string
# though, so we need to guard against that.
approved format, so no outer eval
my $meta_json = $self->_formalize_metadata($method, $trans);
$fields->{metadata} = $meta_json if $meta_json;
return $fields;
}
### PRIVATE INSTANCE
nses.
Examples of Exceptions are: request JSON is broken and can't be decoded;
called Method dies because of internal error; Result cannot be encoded
in JSON, etc.
=item Event
An asynchronous notif
object
my $user = $env->cookie('user');
# \0 is a shortcut for JSON::false
return { success => \0, error => 'Not authenticated' }
unless $user eq
LE ###
#
# JSON decoding options
#
# DEPRECATED. Use `json_options_deserialize` or `json_options`
# Config options instead.
#
our %JSON_OPTIONS;
### PUBLIC CLASS METHOD ###
#
# Turns JSONified POST
return something.
return $self->_no_events unless @results;
# Polling results are always JSON; no content type needed
my $serialized = $self->_serialize_results(@results);
# And if s
izer = $serializer_class->new(
config => $config,
api => $api,
);
my $json = eval {
$serializer->serialize(
mute_exceptions => 1,
debug
package WWW::TVMaze;
use 5.006;
use strict;
use warnings;
use Mouse;
use LWP::UserAgent;
use JSON::XS;
use DateTime;
use Params::Validate qw(:all);
=head1 NAME
WWW::TVMaze - Interface to TVMaze API
return {};
}
my $data = {};
eval {
$data = decode_json($response->decoded_content);
};
if ($@) {
$self->error('problem decoding json');
return undef;
}
return $data;
}
=head1 AUT
config->debug_api;
my $serializer = $s_class->new( config => $config );
my $api_json = $serializer->serialize(
mute_exceptions => 1,
debug => $debug_api,
,
);
# Compile JavaScript for REMOTING_API
my $js_chunk = "$remoting_var = " . ($api_json || '{}') . ";\n";
# If auto_connect is on, add client side initialization code
$js_chunk
is added only when there's something in it
if ( $polling_api && !$no_polling ) {
$api_json = $serializer->serialize(
mute_exceptions => 1,
debug => $debug
{
status => 200,
content_type => qr|^application/json\b|,
content_length => 249,
comparator => 'cmp_json',
content =>
q|{"action":null,"message":"E
{
status => 200,
content_type => qr|^application/json\b|,
content_length => 78,
comparator => 'cmp_json',
content =>
q|{"action":"Foo","method":"fo
},
},
output => {
status => 200,
content_type => qr|^application/json\b|,
content_length => 304,
comparator => 'cmp_str',
content =>
ialization method in
C<RPC::ExtDirect::Serializer> module. This option only affects
"from Perl to JSON" conversion.
Default: C<undef>, chained to: L</debug>.
=item debug_deserialize
Turn debugging
or deserialization method in
C<RPC::ExtDirect::Serializer> module. This option only affects
"from JSON to Perl" conversion.
Default: C<undef>, chained to: L</debug>.
=item debug_request
Turn debugg
<RPC::ExtDirect::Serializer> when
instantiating new objects to be used to serialize data (Perl to JSON).
Default: C<'RPC::ExtDirect::Serializer'>.
=item serializer_class_api
Class name to be used w
nd
turn it into the barest minimum that I wanted to save in a text file
(after converting it into JSON) that I kept under version
control. Without pruning the repository became unecessarily big and
th
lizer;
use strict;
use warnings;
no warnings 'uninitialized'; ## no critic
use Carp;
use JSON ();
use RPC::ExtDirect::Config;
use RPC::ExtDirect::Util::Accessor;
use RPC::ExtDirect::Util qw
;
return $self;
}
### PUBLIC INSTANCE METHOD ###
#
# Serialize the data passed to it in JSON
#
sub serialize {
my ($self, %arg) = @_;
my $data = delete $arg{data} || [];
#
@serialized) . ']'
;
return $text;
}
### PUBLIC INSTANCE METHOD ###
#
# Turns JSONified POST request(s) into array of instantiated
# RPC::ExtDirect::Request (Exception) objects. Re
accessor => 'json_options',
}, {
setter => 'json_options_serialize',
fallback => 'json_options',
}, {
package => 'RPC::ExtDirect::Deserialize',
var => 'JSON_OPTIONS',
t
ype => 'hash',
setter => 'json_options_deserialize',
fallback => 'json_options',
}, {
accessor => 'router_class',
default => 'RPC::ExtDirect::Router',
}, {
accessor => 'time
instead.
#
our $EXCEPTION_CLASS;
### PUBLIC CLASS METHOD ###
#
# Serialize the passed data into JSON form
#
# DEPRECATED. Use RPC::ExtDirect::Serializer->serializer instance method
# instead.
#
sub
nything that is serializable to JSON. No checks are made
and it is assumed that client side can understand the data format used
with Events.
Note that by default L<JSON> will blow up if you try to fe
global Config
option L<json_options|RPC::ExtDirect::Config/json_options> to include
C<allow_blessed> flag:
my $config = RPC::ExtDirect->get_api->config;
$config->json_options({
allow
f $has_upload;
# Form upload responses are JSON wrapped in HTML, not plain JSON
my $content_type = $has_upload ? 'text/html' : 'application/json';
# We need content length in octets
d;
use strict;
use warnings;
no warnings 'uninitialized'; ## no critic
use Carp;
use JSON;
use RPC::ExtDirect::Config;
use RPC::ExtDirect::Util ();
use RPC::ExtDirect::Util::Accessor;
#
my $def;
# Form handlers are defined like this
# (\1 means JSON::true and doesn't force us to `use JSON`)
if ( $self->formHandler ) {
$def = { name => $name, formHandler
xtDirect::Client will call this method to prepare
# the arguments that are about to be encoded in JSON and passed
# over to the server side.
#
# The difference is that the server side wants an unfolde
return {
success => \1, # JSON::true
};
}
else {
return {
success => \0, # JSON::false
error => "You shall
esides that, Ext.Direct also has several major advantages over similar
protocols like XML-RPC and JSON-RPC:
=over 4
=item *
Built in service discovery mechanism: server side API is published to the
s. For more info,
see L</"FILE UPLOADS">.
Note that any field values in a submitted form will be JSON encoded
by the client side.
=item *
All remoting Methods are called in scalar context. Returnin
->poll($self->cgi);
# Gather variables for HTTP response
my $content_type = 'application/json';
my $http_status = '200 OK';
# And we need content length, too (in octets)
my $con
oads, @field_uploads;
delete $keyword{ $param };
};
};
# Metadata is JSON encoded; decode_metadata lives by side effects!
if ( exists $keyword{metadata} ) {
RP
as the content type that will
override the default C<application/json> type.
Example:
print $cgi->route('text/javascript'); # JSONP
=item method('content/type', 'HTTP status')
Two header value
erpreted as the content type and
HTTP status, respectively.
Example:
print $cgi->poll('text/json', '401 Unauthorized'); # Auth request
=item method(-header => 'value')
Any custom header can be
dispatches the quests, collects results and
returns an HTTP response with results as a serialized JSON stream.
Accepts custom headers as described in L</"HTTP response headers">.
=item C<poll>
Inst
:Util;
use strict;
use warnings;
no warnings 'uninitialized'; ## no critic
use Carp;
use JSON;
use base 'Exporter';
our @EXPORT_OK = qw/
clean_error_message
get_caller_info
pars
ed ) {
# Whoever sends *multiple* metadata fields is going to regret it.
my $meta_json = 'ARRAY' eq ref $meta_encoded ? pop @$meta_encoded
:
oded
;
local $@;
$keywords->{metadata} = eval { JSON::from_json($meta_json) };
if ( $@ ) {
my $error = clean_error_message($@);
Direct::Client::API;
use strict;
use warnings;
no warnings 'uninitialized'; ## no critic
use JSON;
use RPC::ExtDirect::Util::Accessor;
use RPC::ExtDirect::Config;
use RPC::ExtDirect::API;
use b
ts
#
sub _decode_api {
my ($js) = @_;
$js =~ s/^[^{]+//;
my ($api_js) = eval { JSON->new->utf8(1)->decode_prefix($js) };
die "Can't decode API declaration: $@\n" if $@;
di