Blockchain/Client/RPC.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 66.6% 4 / 6 Conditioncond 85.7% 6 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 93.4% 86 / 92 CC 8 SCAR 5.7
CC
8
Cov
94%
_request
3.1
configure
3.0
http_client
2.0
CRAP 1.8
« Blockchain/ETH.pm ↑ Summary Blockchain.pm »
1 package Net::Async::Blockchain::Client::RPC;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.001';
7  
8 =head1 NAME
9  
10 Net::Async::Blockchain::Client::RPC - Async RPC Client.
11  
12 =head1 SYNOPSIS
13  
14 Objects of this type would not normally be constructed directly.
15  
16 =head1 DESCRIPTION
17  
18 Centralize all asynchronous RPC calls.
19  
20 =over 4
21  
22 =back
23  
24 =cut
25  
26 5 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 5 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use JSON::MaybeUTF8 qw(encode_json_utf8 decode_json_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use IO::Async::Notifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 use constant {
36 # default value for the Net::Async::HTTP stall_timeout configuration.
37 5 DEFAULT_TIMEOUT => 100,
38 DEFAULT_MAX_CONNECTIONS => 6
39 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 13 ▶sub endpoint { shift->{endpoint} }
Subroutine endpoint: called CC=1 SCAR=0.0
Pod documented
42 13 ▶sub rpc_user { shift->{rpc_user} }
Subroutine rpc_user: called CC=1 SCAR=0.0
Pod documented
43 13 ▶sub rpc_password { shift->{rpc_password} }
Subroutine rpc_password: called CC=1 SCAR=0.0
Pod documented
44 6 ▶sub timeout { shift->{timeout} // DEFAULT_TIMEOUT }
Subroutine timeout: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"timeout"} // 100
l!l
24
MC/DC100%1/1
+(shift())->{"timeout"} // 100
+(shift())->{"timeout"}
Truth table100%2/2
A
A= +(shift())->{"timeout"}
Aresult
11
00
45  
46 =head2 max_connections
47  
48 L<https://metacpan.org/pod/Net::Async::HTTP#max_connections_per_host-=%3E-INT>
49  
50 =over 4
51  
52 =back
53  
54 returns the configured max_connections value or DEFAULT_MAX_CONNECTIONS
55  
56 =cut
57  
58 3 ▶sub max_connections { shift->{max_connections} // DEFAULT_MAX_CONNECTIONS }
Subroutine max_connections: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"max_connections"} // 6
l!l
12
MC/DC100%1/1
+(shift())->{"max_connections"} // 6
+(shift())->{"max_connections"}
Truth table100%2/2
A
A= +(shift())->{"max_connections"}
Aresult
11
00
59  
60 =head2 http_client
61  
62 Create an L<Net::Async::HTTP> instance, if it is already defined just return
63 the object
64  
65 =over 4
66  
67 =back
68  
69 L<Net::Async::HTTP>
70  
71 =cut
72  
73 sub http_client {
74 13 ▶ my ($self) = @_;
Subroutine http_client: called CC=2 SCAR=7.0
Pod documented
75  
76 13 ▶ return $self->{http_client} //= do {
Condition66%2/3
$self->{"http_client"} //= do {
l!l&&r!l&&!r
1030
MC/DC0%0/2
$self->{"http_client"} //= do { $self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections)); $http_client }
$self->{"http_client"} do { $self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections)); $http_client }
Truth table66%2/3
A //= B
A= $self->{"http_client"}B= do { $self->add_child(my $http_client = "Net::Async::HTTP"->new("decode_content", 1, "stall_timeout", $self->timeout, "timeout", $self->timeout, "max_connections_per_host", $self->max_connections)); $http_client }
ABresult
1X1
011
000
77 3 $self->add_child(
78 my $http_client = Net::Async::HTTP->new(
79 decode_content => 1,
80 stall_timeout => $self->timeout,
81 timeout => $self->timeout,
82 max_connections_per_host => $self->max_connections,
83 ));
84  
85 3 $http_client;
86 };
87 }
88  
89 =head2 configure
90  
91 Any additional configuration that is not described on L<IO::Async::Notifier>
92 must be included and removed here.
93  
94 =over 4
95  
96 =item * C<endpoint>
97  
98 =item * C<timeout> connection timeout (seconds)
99  
100 =item * C<rpc_user> RPC user. (optional, default: undef)
101  
102 =item * C<rpc_password> RPC password. (optional, default: undef)
103  
104 =back
105  
106 =cut
107  
108 sub configure {
109 3 ▶ my ($self, %params) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
110  
111 3 for my $k (qw(endpoint rpc_user rpc_password timeout max_connections)) {
112 15 ▶ $self->{$k} = delete $params{$k} if exists $params{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$k} 4 11 15
113 }
114  
115 3 $self->SUPER::configure(%params);
116 }
117  
118 =head2 _request
119  
120 Use any argument as the method parameter for the client RPC call
121  
122 =over 4
123  
124 =item * C<method>
125  
126 =item * C<params> (any parameter required by the RPC call)
127  
128 =back
129  
130 L<Future>
131  
132 =cut
133  
134 13 ▶async sub _request {
Subroutine _request: called CC=3 SCAR=11.4
135 13 my ($self, $method, @params) = @_;
136  
137 13 my $obj = {
138 id => 1,
139 method => $method,
140 params => [@params],
141 };
142 13 my @post_params = ($self->endpoint, encode_json_utf8($obj), content_type => 'application/json');
143 # for ETH based, we don't require user+password. Check to send user+password if exists.
144 13 ▶ push @post_params, (user => $self->rpc_user) if $self->rpc_user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rpc_user 0 13 13
145 13 ▶ push @post_params, (pass => $self->rpc_password) if $self->rpc_password;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rpc_password 0 13 13
146  
147 13 my $response = await $self->http_client->POST(@post_params);
148 0 return decode_json_utf8($response->decoded_content)->{result};
149 5 }
150  
151 5 1;
« Blockchain/ETH.pm ↑ Summary Blockchain.pm »