Blockchain/Client/Websocket.pm

filter: Statementstmt 80.0% 60 / 75 Branchbran 33.3% 2 / 6 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 73.9% 17 / 23 Podpod 100% 7 / 7 totaltotal 70.6% 89 / 126 CC 8 SCAR 10.9
CC
8
Cov
72%
shutdown
12.0
__ANON__
6.0
websocket_client
3.1
CRAP 3.0
« Blockchain/Transaction.pm ↑ Summary Blockchain/Client/RPC.pm »
1 package Net::Async::Blockchain::Client::Websocket;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.002';
7  
8 =head1 NAME
9  
10 Net::Async::Blockchain::Client::Websocket - Async websocket Client.
11  
12 =head1 SYNOPSIS
13  
14 my $loop = IO::Async::Loop->new();
15  
16 $loop->add(my $ws_source = Ryu::Async->new());
17  
18 $loop->add(
19 my $client = Net::Async::Blockchain::Client::Websocket->new(
20 endpoint => "ws://127.0.0.1:8546",
21 )
22 );
23  
24 $client->eth_subscribe('newHeads')->each(sub {print shift->{hash}})->get;
25  
26 =head1 DESCRIPTION
27  
28 Auto load the commands as the method parameters for the websocket calls returning them asynchronously.
29  
30 =over 4
31  
32 =back
33  
34 =cut
35  
36 4 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Protocol::WebSocket::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 ▶use Net::Async::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 4 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 =head2 source
50  
51 Create an L<Ryu::Source> instance, if it is already defined just return
52 the object
53  
54 =over 4
55  
56 =back
57  
58 L<Ryu::Source>
59  
60 =cut
61  
62 sub source : method {
63 1 ▶ my ($self) = @_;
Subroutine source: called CC=2 SCAR=7.4
Pod documented
64 1 ▶ return $self->{source} //= do {
Condition33%1/3
$self->{"source"} //= do {
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"source"} //= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu->source }
$self->{"source"} do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu->source }
Truth table33%1/3
A //= B
A= $self->{"source"}B= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu->source }
ABresult
1X1
011
000
65 1 $self->add_child(my $ryu = Ryu::Async->new);
66 1 $ryu->source;
67 };
68 }
69  
70 =head2 endpoint
71  
72 Websocket endpoint
73  
74 =over 4
75  
76 =back
77  
78 URL containing the port if needed
79  
80 =cut
81  
82 2 ▶sub endpoint : method { shift->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
83  
84 =head2 latest_subscription
85  
86 Latest subscription sent from this module
87  
88 =cut
89  
90 0 ▶sub latest_subscription : method { shift->{latest_subscription} }
Subroutine latest_subscription: not called CC=1 SCAR=6.9
Pod documented
91  
92 =head2 websocket_client
93  
94 Create an L<Net::Async::WebSocket::Client> instance, if it is already defined just return
95 the object
96  
97 =over 4
98  
99 =back
100  
101 L<Net::Async::WebSocket::Client>
102  
103 =cut
104  
105 sub websocket_client : method {
106 2 ▶ my ($self) = @_;
Subroutine websocket_client: called CC=2 SCAR=11.3
Pod documented
107  
108 2 ▶ return $self->{websocket_client} //= do {
Condition66%2/3
$self->{"websocket_client"} //= do {
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"websocket_client"} //= do { $self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub { my($self, undef, $frame) = @_; $self->source->emit(decode_json_utf8($frame)); } ), "on_ping_frame", $self->$curry::weak(sub { my($self) = @_; $self->websocket_client->send_pong_frame->on_fail(sub { my $error = shift(); warn "Fail to send the pong frame, error: $error"; } )->retain; } ), "on_closed", $self->$curry::weak(sub { my $self = shift(); my $error = "Connection closed by peer"; $self->source->fail($error) unless $self->source->completed->is_ready; $self->shutdown($error); } ), "close_on_read_eof", 1)); $client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0); $client }
$self->{"websocket_client"} do { $self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub { my($self, undef, $frame) = @_; $self->source->emit(decode_json_utf8($frame)); } ), "on_ping_frame", $self->$curry::weak(sub { my($self) = @_; $self->websocket_client->send_pong_frame->on_fail(sub { my $error = shift(); warn "Fail to send the pong frame, error: $error"; } )->retain; } ), "on_closed", $self->$curry::weak(sub { my $self = shift(); my $error = "Connection closed by peer"; $self->source->fail($error) unless $self->source->completed->is_ready; $self->shutdown($error); } ), "close_on_read_eof", 1)); $client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0); $client }
Truth table66%2/3
A //= B
A= $self->{"websocket_client"}B= do { $self->add_child(my $client = "Net::Async::WebSocket::Client"->new("on_text_frame", $self->$curry::weak(sub { my($self, undef, $frame) = @_; $self->source->emit(decode_json_utf8($frame)); } ), "on_ping_frame", $self->$curry::weak(sub { my($self) = @_; $self->websocket_client->send_pong_frame->on_fail(sub { my $error = shift(); warn "Fail to send the pong frame, error: $error"; } )->retain; } ), "on_closed", $self->$curry::weak(sub { my $self = shift(); my $error = "Connection closed by peer"; $self->source->fail($error) unless $self->source->completed->is_ready; $self->shutdown($error); } ), "close_on_read_eof", 1)); $client->{"framebuffer"} = "Protocol::WebSocket::Frame"->new("max_payload_size", 0); $client }
ABresult
1X1
011
000
109 $self->add_child(
110 my $client = Net::Async::WebSocket::Client->new(
111 on_text_frame => $self->$curry::weak(
112 sub {
113 0 ▶ my ($self, undef, $frame) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 $self->source->emit(decode_json_utf8($frame));
115 }
116 ),
117 on_ping_frame => $self->$curry::weak(
118 sub {
119 0 ▶ my ($self) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 $self->websocket_client->send_pong_frame->on_fail(
121 sub {
122 0 ▶ my $error = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 0 warn "Fail to send the pong frame, error: $error";
124 0 })->retain();
125 }
126 ),
127 on_closed => $self->$curry::weak(
128 sub {
129 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 my $error = "Connection closed by peer";
131 0 ▶ $self->source->fail($error) unless $self->source->completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->source->completed->is_ready 0 0 0
132 0 $self->shutdown($error);
133 }
134 1 ),
135 close_on_read_eof => 1,
136 ));
137  
138 1 $client->{framebuffer} = Protocol::WebSocket::Frame->new(max_payload_size => 0);
139 1 $client;
140 };
141 }
142  
143 =head2 configure
144  
145 Any additional configuration that is not described on L<IO::Async::Notifier>
146 must be included and removed here.
147  
148 =over 4
149  
150 =item * C<endpoint>
151  
152 =back
153  
154 =cut
155  
156 sub configure {
157 2 ▶ my ($self, %params) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
158  
159 2 for my $k (qw(endpoint on_shutdown)) {
160 4 ▶ $self->{$k} = delete $params{$k} if exists $params{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$k} 3 1 4
161 }
162  
163 2 $self->SUPER::configure(%params);
164 }
165  
166 =head2 _request
167  
168 Prepare the data to be sent to the websocket and call the request
169  
170 =over 4
171  
172 =item * C<method>
173  
174 =item * C<@_> - any parameter required by the RPC call
175  
176 =back
177  
178 L<Ryu::Source>
179  
180 =cut
181  
182 1 ▶async sub _request {
Subroutine _request: called CC=1 SCAR=0.0
183 1 my ($self, $method, @params) = @_;
184  
185 1 my $url = URI->new($self->endpoint);
186  
187 # this is the client request
188 1 my $request_call = {
189 id => 1,
190 method => $method,
191 jsonrpc => '2.0',
192 params => [@params],
193 };
194  
195 1 await $self->websocket_client->connect(
196 url => $self->endpoint,
197 req => Protocol::WebSocket::Request->new(origin => $url->host),
198 );
199  
200 1 await $self->websocket_client->send_text_frame(encode_json_utf8($request_call));
201  
202 1 return $self->source;
203 4 }
204  
205 =head2 shutdown
206  
207 run the configured shutdown action if any
208  
209 =over 4
210  
211 =item * C<error> error message
212  
213 =back
214  
215 =cut
216  
217 sub shutdown { ## no critic
218 0 ▶ my ($self, $error) = @_;
Subroutine shutdown: not called CC=3 SCAR=24.8
Pod documented
219  
220 0 ▶ if (my $code = $self->{on_shutdown} || $self->can("on_shutdown")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->{"on_shutdown"} || $self->can("on_shutdown")) 0 0 0
Condition0%0/3
$self->{'on_shutdown'} || $self->can('on_shutdown')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'on_shutdown'} || $self->can('on_shutdown')
$self->{'on_shutdown'} $self->can('on_shutdown')
Truth table0%0/3
A || B
A= $self->{'on_shutdown'}B= $self->can('on_shutdown')
ABresult
1X1
011
000
221 0 return $code->($error);
222 }
223 0 return undef;
224 }
225  
226 =head2 eth_subscribe
227  
228 Subscribe to an event
229  
230 =over 4
231  
232 =item * C<method>
233  
234 =item * C<@_> - any parameter required by the RPC call
235  
236 =back
237  
238 =cut
239  
240 sub eth_subscribe {
241 1 ▶ my ($self, $subscription) = @_;
Subroutine eth_subscribe: called CC=1 SCAR=0.0
Pod documented
242 1 $self->{latest_subscription} = $subscription;
243 1 return $self->_request('eth_subscribe', $subscription);
244 }
245  
246 4 1;
« Blockchain/Transaction.pm ↑ Summary Blockchain/Client/RPC.pm »