File: Call.pm

package info (click to toggle)
libjson-rpc-common-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 244 kB
  • sloc: perl: 1,481; makefile: 2
file content (290 lines) | stat: -rw-r--r-- 5,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/perl

package JSON::RPC::Common::Procedure::Call;
$JSON::RPC::Common::Procedure::Call::VERSION = '0.11';
use Moose;
# ABSTRACT: JSON RPC Procedure Call base class.

use Try::Tiny;
use JSON::RPC::Common::TypeConstraints qw(JSONValue);
use JSON::RPC::Common::Procedure::Return;

use Carp qw(croak);

use namespace::clean -except => [qw(meta)];

with qw(JSON::RPC::Common::Message);

has return_class => (
	isa => "ClassName",
	is  => "rw",
	default => "JSON::RPC::Common::Procedure::Return",
);

has error_class => (
	isa => "ClassName",
	is  => "rw",
	default => "JSON::RPC::Common::Procedure::Return::Error",
);

has version => (
	isa => "Str",
	is  => "rw",
	predicate => "has_version",
);

has method => (
	isa => "Str",
	is  => "rw",
	required => 1,
);

has id => (
	isa => JSONValue,
	is  => "rw",
	predicate => "has_id",
);

has params => (
	isa => "Ref",
	is  => "rw",
	predicate => "has_params",
);

sub deflate_version {
	return ();
}
sub deflate_method {
	my $self = shift;
	return ( method => $self->method );
}

sub deflate_id {
	my $self = shift;

	if ( $self->has_id ) {
		return ( id => $self->id );
	} else {
		return ();
	}
}

sub deflate_params {
	my $self = shift;

	if ( $self->has_params ) {
		return ( params => $self->params );
	} else {
		return ();
	}
}

sub deflate {
	my $self = shift;

	return {
		$self->deflate_version,
		$self->deflate_method,
		$self->deflate_id,
		$self->deflate_params,
	};
}

sub is_service { 0 }

sub is_notification {
	my $self = shift;
	return not $self->has_id;
}

sub params_list {
	my $self = shift;
	my $p = $self->params;

	if ( ref $p eq 'HASH' ) {
		return %$p;
	} elsif ( ref $p eq 'ARRAY' ) {
		return @$p;
	} else {
		return $p; # FIXME error?
	}
}

sub call {
	my ( $self, $invocant, @args ) = @_;

	die "No invocant provided" unless blessed($invocant);

	my $method = $self->method;

    my $error;
	my @res = try {
	    $invocant->$method( $self->params_list, @args )
	} catch {
	    $error = $_;
	};

	if ($error) {
	    $self->return_error(message => $error);
	}
	else {
	    $self->return_result(@res);
	}
}

sub create_return {
	my ( $self, @args ) = @_;

	$self->return_class->new(
		error_class => $self->error_class,
		( $self->has_id ? ( id => $self->id ) : () ),
		@args,
	);
}

sub return_error {
	my ( $self, @args ) = @_;

	$self->create_return( error => $self->error_class->new_dwim(@args) );
}

sub return_result {
	my ( $self, @res ) = @_;

	my $res = @res == 1 ? $res[0] : \@res;

	$self->create_return( result => $res );
}

__PACKAGE__->meta->make_immutable;

__PACKAGE__

__END__

=pod

=head1 NAME

JSON::RPC::Common::Procedure::Call - JSON RPC Procedure Call base class.

=head1 VERSION

version 0.11

=head1 SYNOPSIS

	use JSON::RPC::Common::Procedure::Call;

	my $req = JSON::RPC::Common::Procedure::Call->inflate({ ... });

	warn "HALLO JSONRPC VERSION " . $req->version;

=head1 DESCRIPTION

A JSON-RPC Procedure Call (ed: *rolls eys*, what was wrong with "request"?) is
either a notification or a method invocation in JSON-PRC.

See L<http://json-rpc.org/wiki/specification> for more details.

=head1 ATTRIBUTES

All attributes are read only unless otherwise specified.

=over 4

=item version

=item id

The request ID.

Used to correlate a request to a response.

=item method

The name of the method to invoke.

=item params

Returns a reference to the parameters hash or array.

=item return_class

=item error_class

The classes to instantiate the response objects.

These vary per subclass.

=back

=head1 METHODS

=over 4

=item inflate

A factory constructor. Delegates to C<new> on a subclass based on the protocol
version.

This is the recommended constructor.

=item deflate

Flatten to JSON data

=item new

The actual constructor.

Not intended for normal use on this class, you should use a subclass most of
the time.

Calling C<< JSON::RPC::Common::Procedure::Call->new >> will construct a call
with an undefined version, which cannot be deflated (and thus sent over the
wire). This is still useful for testing your own code's RPC hanlding, so this
is not allowed.

=item params_list

Dereferences C<params> regardless of representation.

Returns a list of positionals or a key/value list.

=item return_result $result

=item return_error %error_params

Create a new L<JSON::RPC::Common::Procedure::Return> with or without an error.

=item is_notification

Whether this request is a notification (a method that does not need a response).

=item is_service

Whether this request is a JSON-RPC 1.1 service method (e.g.
C<system.describe>).

This method is always false for 1.0 and 2.0.

=item call $obj

A convenience method to invoke the call on C<$obj> and create a new return with
the return value.

=back

=head1 AUTHOR

Yuval Kogman <nothingmuch@woobling.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Yuval Kogman and others.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut