blib/lib/Perinci/Access/Lite.pm

filter: Statementstmt 71.8% 97 / 135 Branchbran 42.6% 29 / 68 Conditioncond 48.7% 20 / 41 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 57.4% 162 / 282 CC 52 SCAR 53.3
CC
52
Cov
60%
request
264.3
__package_exists
3.1
new
2.0
CRAP 206.0
↑ Summary
1 package Perinci::Access::Lite;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Perinci::AccessUtil qw(strip_riap_stuffs_from_res);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2022-10-28'; # DATE
11 2 our $DIST = 'Perinci-Access-Lite'; # DIST
12 2 our $VERSION = '0.150'; # VERSION
13  
14 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
15 1 my ($class, %args) = @_;
16 1 ▶ $args{riap_version} //= 1.1;
Condition50%1/2
$args{"riap_version"} //= "1.1"
l!l
01
MC/DC0%0/1
$args{"riap_version"} //= "1.1"
$args{"riap_version"}
Truth table50%1/2
A
A= $args{"riap_version"}
Aresult
11
00
17 1 bless \%args, $class;
18 }
19  
20 # copy-pasted from SHARYANTO::Package::Util
21 3 ▶sub __package_exists {
Subroutine __package_exists: called CC=3 SCAR=11.3
22 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 my $pkg = shift;
25  
26 3 ▶ return unless $pkg =~ /\A\w+(::\w+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg =~ /\A\w+(::\w+)*\z/ 0 3 3
27 3 ▶ if ($pkg =~ s/::(\w+)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ s/::(\w+)\z//) { } 3 0 3
28 3 return !!${$pkg . "::"}{$1 . "::"};
29 } else {
30 0 return !!$::{$pkg . "::"};
31 }
32 }
33  
34 7 ▶sub request {
Subroutine request: called CC=49 SCAR=55.8
Pod documented
35 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 7 my ($self, $action, $url, $extra) = @_;
38  
39 #say "D:request($action => $url)";
40  
41 7 ▶ $extra //= {};
Condition50%1/2
$extra //= {}
l!l
70
MC/DC0%0/1
$extra //= {}
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
42  
43 7 ▶ my $v = $extra->{v} // 1.1;
Condition50%1/2
$extra->{"v"} // "1.1"
l!l
07
MC/DC0%0/1
$extra->{"v"} // "1.1"
$extra->{"v"}
Truth table50%1/2
A
A= $extra->{"v"}
Aresult
11
00
44 7 ▶ if ($v ne '1.1' && $v ne '1.2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($v ne "1.1" and $v ne "1.2") 0 7 7
Condition33%1/3
$v ne "1.1" and $v ne "1.2"
!ll&&!rl&&r
700
MC/DC0%0/2
$v ne "1.1" and $v ne "1.2"
$v ne "1.1" $v ne "1.2"
Truth table33%1/3
A and B
A= $v ne "1.1"B= $v ne "1.2"
ABresult
0X0
100
111
45 0 return [501, "Riap protocol not supported, must be 1.1 or 1.2"];
46 }
47  
48 7 my $res;
49 7 ▶ if ($url =~ m!\A(?:pl:)?/(\w+(?:/\w+)*)/(\w*)\z!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($url =~ m[\A(?:pl:)?/(\w+(?:/\w+)*)/(\w*)\z]) { } 7 0 7
elsif ($url =~ m[\Ahttps?:/(/?)]i) { } 0 0 0
50 7 my ($mod_uripath, $func) = ($1, $2);
51 7 (my $pkg = $mod_uripath) =~ s!/!::!g;
52 7 my $mod_pm = "$mod_uripath.pm";
53  
54 7 my $pkg_exists;
55  
56 LOAD:
57 {
58 7 ▶ last if exists $INC{$mod_pm};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{$mod_pm} 4 3 7
59 3 $pkg_exists = __package_exists($pkg);
60 # special names
61 3 ▶ last LOAD if $pkg =~ /\A(main)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /\A(main)\z/ 0 3 3
62 0 ▶ last if $pkg_exists && defined(${"$pkg\::VERSION"});
Branch: 1 branch
BranchTrueFalseTotal
if $pkg_exists and defined ${"${pkg}::VERSION";} 0 3 3
Condition33%1/3
$pkg_exists and defined ${"${pkg}::VERSION";}
!ll&&!rl&&r
300
MC/DC0%0/2
$pkg_exists and defined ${"${pkg}::VERSION";}
$pkg_exists defined ${"${pkg}::VERSION";}
Truth table33%1/3
A and B
A= $pkg_existsB= defined ${"${pkg}::VERSION";}
ABresult
0X0
100
111
63 #say "D:Loading $pkg ...";
64 3 eval { require $mod_pm };
65 3 ▶ return [500, "Can't load module $pkg: $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
66 }
67  
68 6 ▶ if ($action eq 'list') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'list') { } 2 4 6
elsif ($action eq 'info') { } 0 4 4
elsif ($action eq 'meta' or $action eq 'call') { } 4 0 4
69 2 ▶ return [501, "Action 'list' not implemented for ".
Branch: 1 branch
BranchTrueFalseTotal
if length $func 1 1 2
70 "non-package entities"]
71 if length($func);
72 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 my $spec = \%{"$pkg\::SPEC"};
74 1 return [200, "OK (list)", [grep {/\A\w+\z/} sort keys %$spec]];
75 } elsif ($action eq 'info') {
76 0 ▶ my $data = {
Branch: 3 branches
BranchTrueFalseTotal
!length($func) ? : 0 0 0
$func =~ /\A\w+\z/ ? : 0 0 0
$func =~ /\A[\@\$\%]/ ? : 0 0 0
77 uri => "$mod_uripath/$func",
78 type => (!length($func) ? "package" :
79 $func =~ /\A\w+\z/ ? "function" :
80 $func =~ /\A[\@\$\%]/ ? "variable" :
81 "?"),
82 };
83 0 return [200, "OK (info)", $data];
84 ▶ } elsif ($action eq 'meta' || $action eq 'call') {
Condition66%2/3
$action eq "meta" or $action eq "call"
l!l&&r!l&&!r
220
MC/DC0%0/2
$action eq "meta" or $action eq "call"
$action eq "meta" $action eq "call"
Truth table66%2/3
A or B
A= $action eq "meta"B= $action eq "call"
ABresult
1X1
011
000
85 4 ▶ return [501, "Action 'call' not implemented for package entity"]
Branch: 1 branch
BranchTrueFalseTotal
if not length $func and $action eq "call" 1 3 4
Condition100%3/3
not length $func and $action eq "call"
!ll&&!rl&&r
211
MC/DC100%2/2
not length $func and $action eq "call"
not length $func $action eq "call"
Truth table100%3/3
not(A) and B
A= not length $funcB= $action eq "call"
ABresult
0X0
100
111
86 if !length($func) && $action eq 'call';
87 3 my $meta;
88 {
89 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
90 3 ▶ if (length $func) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $func) { } 2 1 3
91 2 ▶ $meta = ${"$pkg\::SPEC"}{$func}
Branch: 2 branches
BranchTrueFalseTotal
$meta = do { "${pkg}::SPEC" }->{$func} or return [500, "No metadata for '${url}' (" . ($pkg_exists ? "package '${pkg}' exists, perhaps you mentioned '${pkg}' somewhere without actually loading the module, or perhaps '${func}' is a typo?" : "package '${pkg}' doesn't exist, perhaps '${mod_uripath}' or '${func}' is a typo?") . ')'] 2 0 2
$pkg_exists ? : 0 0 0
92 or return [
93 500, "No metadata for '$url' (".
94 ($pkg_exists ? "package '$pkg' exists, perhaps you mentioned '$pkg' somewhere without actually loading the module, or perhaps '$func' is a typo?" :
95 "package '$pkg' doesn't exist, perhaps '$mod_uripath' or '$func' is a typo?") .
96 ")"];
97 } else {
98 1 ▶ $meta = ${"$pkg\::SPEC"}{':package'} // {v=>1.1};
Condition50%1/2
do { "${pkg}::SPEC" }->{":package"} // {"v", "1.1"}
l!l
10
MC/DC0%0/1
do { "${pkg}::SPEC" }->{":package"} // {"v", "1.1"}
do { "${pkg}::SPEC" }->{":package"}
Truth table50%1/2
A
A= do { "${pkg}::SPEC" }->{":package"}
Aresult
11
00
99 }
100 3 ▶ $meta->{entity_v} //= ${"$pkg\::VERSION"};
Condition66%2/3
$meta->{"entity_v"} //= ${"${pkg}::VERSION";}
l!l&&r!l&&!r
120
MC/DC0%0/2
$meta->{"entity_v"} //= ${"${pkg}::VERSION";}
$meta->{"entity_v"} ${"${pkg}::VERSION";}
Truth table66%2/3
A //= B
A= $meta->{"entity_v"}B= ${"${pkg}::VERSION";}
ABresult
1X1
011
000
101 3 ▶ $meta->{entity_date} //= ${"$pkg\::DATE"};
Condition66%2/3
$meta->{"entity_date"} //= ${"${pkg}::DATE";}
l!l&&r!l&&!r
120
MC/DC0%0/2
$meta->{"entity_date"} //= ${"${pkg}::DATE";}
$meta->{"entity_date"} ${"${pkg}::DATE";}
Truth table66%2/3
A //= B
A= $meta->{"entity_date"}B= ${"${pkg}::DATE";}
ABresult
1X1
011
000
102 }
103  
104 3 require Perinci::Sub::Normalize;
105 3 $meta = Perinci::Sub::Normalize::normalize_function_metadata($meta);
106 3 ▶ if ($action eq 'meta') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "meta") 2 1 3
107 2 $meta->{_orig_args_as} = $meta->{args_as};
108 2 $meta->{args_as} = 'hash';
109 2 $meta->{_orig_result_naked} = $meta->{result_naked};
110 2 $meta->{result_naked} = 0;
111 2 return [200, "OK ($action)", $meta];
112 }
113  
114 # form args (and add special args)
115 1 ▶ my $args = { %{$extra->{args} // {}} }; # shallow copy
Condition50%1/2
$extra->{"args"} // {}
l!l
10
MC/DC0%0/1
$extra->{"args"} // {}
$extra->{"args"}
Truth table50%1/2
A
A= $extra->{"args"}
Aresult
11
00
116 1 ▶ if ($meta->{features} && $meta->{features}{progress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"features"} and $meta->{"features"}{"progress"}) 0 1 1
Condition33%1/3
$meta->{"features"} and $meta->{"features"}{"progress"}
!ll&&!rl&&r
100
MC/DC0%0/2
$meta->{"features"} and $meta->{"features"}{"progress"}
$meta->{"features"} $meta->{"features"}{"progress"}
Truth table33%1/3
A and B
A= $meta->{"features"}B= $meta->{"features"}{"progress"}
ABresult
0X0
100
111
117 0 require Progress::Any;
118 0 $args->{-progress} = Progress::Any->get_indicator;
119 }
120  
121 # convert args
122 1 ▶ my $aa = $meta->{args_as} // 'hash';
Condition50%1/2
$meta->{"args_as"} // "hash"
l!l
01
MC/DC0%0/1
$meta->{"args_as"} // "hash"
$meta->{"args_as"}
Truth table50%1/2
A
A= $meta->{"args_as"}
Aresult
11
00
123 1 my @args;
124 1 ▶ if ($aa =~ /array/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aa =~ /array/) { } 0 1 1
elsif ($aa eq 'hashref') { } 0 1 1
125 0 require Perinci::Sub::ConvertArgs::Array;
126 0 my $convres = Perinci::Sub::ConvertArgs::Array::convert_args_to_array(
127 args => $args, meta => $meta,
128 );
129 0 ▶ return $convres unless $convres->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $convres->[0] == 200 0 0 0
130 0 ▶ if ($aa =~ /ref/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aa =~ /ref/) { } 0 0 0
131 0 @args = ($convres->[2]);
132 } else {
133 0 @args = @{ $convres->[2] };
134 }
135 } elsif ($aa eq 'hashref') {
136 0 @args = ({ %$args });
137 } else {
138 # hash
139 1 @args = %$args;
140 }
141  
142 # call!
143 {
144 2 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 $res = &{"$pkg\::$func"}(@args);
146 }
147  
148 # add envelope
149 1 ▶ if ($meta->{result_naked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"result_naked"}) 0 1 1
150 0 $res = [200, "OK (envelope added by ".__PACKAGE__.")", $res];
151 }
152  
153 # add hint that result is binary
154 1 ▶ if (defined $res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->[2]) 1 0 1
155 1 ▶ if ($meta->{result} && $meta->{result}{schema} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"result"} and $meta->{"result"}{"schema"} and $meta->{"result"}{"schema"}[0] eq "buf") 0 1 1
Condition33%2/6
$meta->{"result"} and $meta->{"result"}{"schema"} and $meta->{"result"}{"schema"}[0] eq "buf"
!ll&&!rl&&r
010
$meta->{"result"} and $meta->{"result"}{"schema"}
!ll&&!rl&&r
001
MC/DC0%0/3
$meta->{"result"} and $meta->{"result"}{"schema"} and $meta->{"result"}{"schema"}[0] eq "buf"
$meta->{"result"} $meta->{"result"}{"schema"} $meta->{"result"}{"schema"}[0] eq "buf"
Truth table25%1/4
A and B and C
A= $meta->{"result"}B= $meta->{"result"}{"schema"}C= $meta->{"result"}{"schema"}[0] eq "buf"
ABCresult
0XX0
10X0
1100
1111
156 $meta->{result}{schema}[0] eq 'buf') {
157 0 $res->[3]{'x.hint.result_binary'} = 1;
158 }
159 }
160  
161 } else {
162 0 return [501, "Unknown/unsupported action '$action'"];
163 }
164 } elsif ($url =~ m!\Ahttps?:/(/?)!i) {
165 0 my $is_unix = !$1;
166 0 my $ht;
167 0 require JSON;
168 0 state $json = JSON->new->allow_nonref;
169 0 ▶ if ($is_unix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_unix) { } 0 0 0
170 0 require HTTP::Tiny::UNIX;
171 0 $ht = HTTP::Tiny::UNIX->new;
172 } else {
173 0 require HTTP::Tiny;
174 0 $ht = HTTP::Tiny->new;
175 }
176 my %headers = (
177 "x-riap-v" => $self->{riap_version},
178 0 "x-riap-action" => $action,
179 "x-riap-fmt" => "json",
180 "content-type" => "application/json",
181 );
182 0 ▶ my $args = $extra->{args} // {};
Condition0%0/2
$extra->{"args"} // {}
l!l
00
MC/DC0%0/1
$extra->{"args"} // {}
$extra->{"args"}
Truth table0%0/2
A
A= $extra->{"args"}
Aresult
11
00
183 0 for (keys %$extra) {
184 0 ▶ next if /\Aargs\z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\Aargs\z/ 0 0 0
185 0 $headers{"x-riap-$_"} = $extra->{$_};
186 }
187 0 my $htres = $ht->post(
188 $url, {
189 headers => \%headers,
190 content => $json->encode($args),
191 });
192 return [500, "Network error: $htres->{status} - $htres->{reason}"]
193 0 ▶ if $htres->{status} != 200;
Branch: 1 branch
BranchTrueFalseTotal
if $htres->{"status"} != 200 0 0 0
194 return [500, "Server error: didn't return JSON (".$htres->{headers}{'content-type'}.")"]
195 0 ▶ unless $htres->{headers}{'content-type'} eq 'application/json';
Branch: 1 branch
BranchTrueFalseTotal
unless $htres->{"headers"}{"content-type"} eq "application/json" 0 0 0
196 return [500, "Server error: didn't return Riap 1.1 response (".$htres->{headers}{'x-riap-v'}.")"]
197 0 ▶ unless $htres->{headers}{'x-riap-v'} =~ /\A1\.1(\.\d+)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $htres->{"headers"}{"x-riap-v"} =~ /\A1\.1(\.\d+)?\z/ 0 0 0
198 0 $res = $json->decode($htres->{content});
199 } else {
200 0 return [501, "Unsupported scheme or bad URL '$url'"];
201 }
202  
203 1 strip_riap_stuffs_from_res($res);
204 }
205  
206 2 1;
207 # ABSTRACT: A lightweight Riap client library
208  
209 __END__
↑ Summary