1MojoX::JSON::RPC::ClienUts(e3r)Contributed Perl DocumentMaotjiooXn::JSON::RPC::Client(3)
2
3
4
6 MojoX::JSON::RPC::Client - JSON RPC client
7
9 use MojoX::JSON::RPC::Client;
10
11 my $client = MojoX::JSON::RPC::Client->new;
12 my $url = 'http://www.example.com/jsonrpc/API';
13 my $callobj = {
14 id => 1,
15 method => 'sum',
16 params => [ 17, 25 ]
17 };
18
19 my $res = $client->call($url, $callobj);
20
21 if($res) {
22 if ($res->is_error) { # RPC ERROR
23 print 'Error : ', $res->error_message;
24 }
25 else {
26 print $res->result;
27 }
28 }
29 else {
30 my $tx_res = $client->tx->res; # Mojo::Message::Response object
31 print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
32 }
33
34 Non-blocking:
35
36 $client->call($url, $callobj, sub {
37 # With callback
38 my $res = pop;
39
40 # ... process result ...
41
42 Mojo::IOLoop->stop;
43 });
44
45 Mojo::IOLoop->start;
46
47 Easy access:
48
49 my $proxy = $client->prepare($uri, ['sum', 'echo']);
50
51 print $proxy->sum(10, 23);
52
54 A JSON-RPC client.
55
57 MojoX::JSON::RPC::Client implements the following attributes.
58
59 "id"
60 Id used for JSON-RPC requests. Used when no id is provided as request
61 parameter.
62
63 "ua"
64 Mojo::UserAgent object.
65
66 "json"
67 Mojo::JSON object for encoding and decoding.
68
69 "version"
70 JSON-RPC version. Defaults to 2.0.
71
72 "content_type"
73 Content type. Defaults to application/json.
74
75 "tx"
76 Mojo::Transaction object of last request.
77
79 MojoX::JSON::RPC::Client inherits all methods from Mojo::Base and
80 implements the following new ones.
81
82 "new"
83 Creates new MojoX::JSON::RPC::Client object.
84
85 my $client = MojoX::JSON::RPC::Client->new;
86
87 "call"
88 Execute JSON-RPC call. Returns MojoX::JSON::RPC::CLient::ReturnObject
89 if RPC call is executed correctly.
90
91 my $client = MojoX::JSON::RPC::Client->new;
92 my $url = 'http://www.example.com/jsonrpc/API';
93 my $callobj = {
94 id => 1,
95 method => 'sum',
96 params => [ 17, 25 ]
97 };
98
99 my $res = $client->call($url, $callobj);
100 if($res) {
101 if ($res->is_error) { # RPC error
102 print 'Error : ', $res->error_message;
103 }
104 else {
105 print $res->result;
106 }
107 }
108 else {
109 my $tx_res = $client->tx->res; # Mojo::Message::Response object
110 print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
111 }
112
113 Make non-blocking call:
114
115 $client->call($url, $callobj, sub {
116 # With callback
117 my $res = pop;
118
119 # ... process result ...
120
121 Mojo::IOLoop->stop;
122 });
123
124 Mojo::IOLoop->start;
125
126 "prepare"
127 Prepares a proxy object that allows RPC methods to be called more
128 easily.
129
130 my $proxy = $client->prepare($uri, ['sum', 'echo']);
131
132 my $res = $proxy->sum(1, 2);
133
134 print $proxy->echo("Echo this!");
135
136 Register services from multiple urls at once:
137
138 my $proxy = $client->prepare($uri1, 'sum', $uri2, [ 'echo', 'ping' ]);
139
140 my $res = $proxy->sum(1, 2);
141
142 print $proxy->echo("Echo this!");
143
144 my $ping_res = $proxy->ping;
145
147 This object is returned by "call".
148
149 "result"
150 RPC result.
151
152 "is_error"
153 Returns a boolean indicating whether an error code has been set.
154
155 "error_code"
156 RPC error code.
157
158 "error_message"
159 RPC error message.
160
161 "error_data"
162 RPC error data.
163
165 MojoX::JSON::RPC
166
167
168
169perl v5.32.0 2020-07-28 MojoX::JSON::RPC::Client(3)