/
Object-JRPC.pm6
39 lines (30 loc) · 964 Bytes
/
Object-JRPC.pm6
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
use Cro::HTTP::Server;
use Cro::HTTP::Router;
use Cro::RPC::JSON;
class JRPC-Actor is export {
method foo ( Int :$a, Str :$b ) is json-rpc {
return "$b and $a";
}
method by-request ( Cro::RPC::JSON::Request $req ) is json-rpc {
{ param-count => $req.params.elems }
}
proto method bar (|) is json-rpc { * }
multi method bar ( Str :$a! ) { "single named Str param" }
multi method bar ( Int $i, Num $n, Str $s ) { "Int, Num, Str positionals" }
multi method bar ( *%options ) { [ "slurpy hash:", %options ] }
method fail (|) is json-rpc {
X::Cro::RPC::JSON::InvalidParams.new( msg => "I always fail" ).throw;
}
method mortal (|) is json-rpc {
die "Simulate... well... something";
}
method non-json (|) { "I won't be called!" }
}
sub routes is export {
route {
post -> "api" {
my $actor = JRPC-Actor.new;
json-rpc $actor;
}
}
}