blib/lib/App/HTTPTinyUtils.pm

filter: Statementstmt 38.5% 37 / 96 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 34.6% 9 / 26 Podpod 100% 1 / 1 totaltotal 32.4% 47 / 145 CC 12 SCAR 34.4
CC
12
Cov
32%
_http_tiny
110.0
__ANON__
6.0
__ANON__
4.3
CRAP 31.3
↑ Summary
1 package App::HTTPTinyUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Perinci::Sub::Util qw(gen_modified_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2024-12-09'; # DATE
12 1 our $DIST = 'App-HTTPTinyUtils'; # DIST
13 1 our $VERSION = '0.010'; # VERSION
14  
15 1 our %SPEC;
16  
17 0 ▶sub _http_tiny {
Subroutine _http_tiny: not called CC=10 SCAR=47.0
18 0 my ($class, %args) = @_;
19  
20 0 (my $class_pm = "$class.pm") =~ s!::!/!g;
21 0 require $class_pm;
22  
23 0 my $res;
24 0 ▶ my $method = $args{method} // 'GET';
Condition0%0/2
$args{"method"} // "GET"
l!l
00
MC/DC0%0/1
$args{"method"} // "GET"
$args{"method"}
Truth table0%0/2
A
A= $args{"method"}
Aresult
11
00
25 0 for my $i (0 .. $#{ $args{urls} }) {
26 0 my $url = $args{urls}[$i];
27 0 my $is_last_url = $i == $#{ $args{urls} };
28  
29 0 my %opts;
30 0 ▶ if (defined $args{content}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args{'content'}) { } 0 0 0
elsif (not -t STDIN) { } 0 0 0
31 0 $opts{content} = $args{content};
32 ## no critic: InputOutput::ProhibitInteractiveTest
33 } elsif (!(-t STDIN)) {
34 0 local $/;
35 0 $opts{content} = <STDIN>;
36 }
37  
38 0 log_trace "Request: $method $url ...";
39 0 ▶ my $res0 = $class->new(%{ $args{attributes} // {} })
Condition0%0/2
$args{"attributes"} // {}
l!l
00
MC/DC0%0/1
$args{"attributes"} // {}
$args{"attributes"}
Truth table0%0/2
A
A= $args{"attributes"}
Aresult
11
00
40 ->request($method, $url, \%opts);
41 0 my $success = $res0->{success};
42  
43 0 ▶ if ($args{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'raw'}) { } 0 0 0
44 0 $res = [200, "OK", $res0];
45 } else {
46 0 $res = [$res0->{status}, $res0->{reason}, $res0->{content}];
47 0 ▶ print $res0->{content} unless $is_last_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_last_url 0 0 0
48 }
49  
50 0 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
51 0 ▶ last unless $args{ignore_errors};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"ignore_errors"} 0 0 0
52 }
53 }
54 0 $res;
55 }
56  
57 $SPEC{http_tiny} = {
58 v => 1.1,
59 summary => 'Perform request(s) with HTTP::Tiny',
60 args => {
61 urls => {
62 'x.name.is_plural' => 1,
63 'x.name.singular' => 'url',
64 schema => ['array*', of=>'str*'],
65 req => 1,
66 pos => 0,
67 slurpy => 1,
68 },
69 method => {
70 schema => ['str*', match=>qr/\A[A-Z]+\z/],
71 default => 'GET',
72 cmdline_aliases => {
73 0 ▶ delete => {summary => 'Shortcut for --method DELETE', is_flag=>1, code=>sub { $_[0]{method} = 'DELETE' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ get => {summary => 'Shortcut for --method GET' , is_flag=>1, code=>sub { $_[0]{method} = 'GET' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ head => {summary => 'Shortcut for --method HEAD' , is_flag=>1, code=>sub { $_[0]{method} = 'HEAD' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 ▶ post => {summary => 'Shortcut for --method POST' , is_flag=>1, code=>sub { $_[0]{method} = 'POST' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 ▶ put => {summary => 'Shortcut for --method PUT' , is_flag=>1, code=>sub { $_[0]{method} = 'PUT' } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 },
79 },
80 1 attributes => {
81 'x.name.is_plural' => 1,
82 'x.name.singular' => 'attribute',
83 summary => 'Pass attributes to HTTP::Tiny constructor',
84 schema => ['hash*', each_key => 'str*'],
85 },
86 headers => {
87 schema => ['hash*', of=>'str*'],
88 'x.name.is_plural' => 1,
89 'x.name.singular' => 'header',
90 },
91 content => {
92 schema => 'str*',
93 },
94 raw => {
95 schema => 'bool*',
96 },
97 ignore_errors => {
98 summary => 'Ignore errors',
99 description => <<'MARKDOWN',
100  
101 Normally, when given multiple URLs, the utility will exit after the first
102 non-success response. With `ignore_errors` set to true, will just log the error
103 and continue. Will return with the last error response.
104  
105 MARKDOWN
106 schema => 'bool*',
107 cmdline_aliases => {i=>{}},
108 },
109 # XXX option: agent
110 # XXX option: timeout
111 # XXX option: post form
112 },
113 };
114 0 ▶sub http_tiny {
Subroutine http_tiny: not called CC=1 SCAR=6.9
Pod documented
115 0 _http_tiny('HTTP::Tiny', @_);
116 }
117  
118 gen_modified_sub(
119 output_name => 'http_tiny_cache',
120 base_name => 'http_tiny',
121 summary => 'Perform request(s) with HTTP::Tiny::Cache',
122 description => <<'MARKDOWN',
123  
124 Like `http_tiny`, but uses <pm:HTTP::Tiny::Cache> instead of <pm:HTTP::Tiny>.
125 See the documentation of HTTP::Tiny::Cache on how to set cache period.
126  
127 MARKDOWN
128 0 ▶ output_code => sub { _http_tiny('HTTP::Tiny::Cache', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 1 );
130  
131 gen_modified_sub(
132 output_name => 'http_tiny_plugin',
133 base_name => 'http_tiny',
134 summary => 'Perform request(s) with HTTP::Tiny::Plugin',
135 description => <<'MARKDOWN',
136  
137 Like `http_tiny`, but uses <pm:HTTP::Tiny::Plugin> instead of <pm:HTTP::Tiny>.
138 See the documentation of HTTP::Tiny::Plugin for more details.
139  
140 MARKDOWN
141 0 ▶ output_code => sub { _http_tiny('HTTP::Tiny::Plugin', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 1 );
143  
144 gen_modified_sub(
145 output_name => 'http_tiny_retry',
146 base_name => 'http_tiny',
147 summary => 'Perform request(s) with HTTP::Tiny::Retry',
148 description => <<'MARKDOWN',
149  
150 Like `http_tiny`, but uses <pm:HTTP::Tiny::Retry> instead of <pm:HTTP::Tiny>.
151 See the documentation of HTTP::Tiny::Retry for more details.
152  
153 MARKDOWN
154 1 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
155 1 my $meta = shift;
156  
157 $meta->{args}{attributes}{cmdline_aliases} = {
158 retries => {
159 summary => 'Number of retries',
160 0 ▶ code => sub { $_[0]{attributes}{retries} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 },
162 retry_delay => {
163 summary => 'Retry delay',
164 0 ▶ code => sub { $_[0]{attributes}{retry_delay} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 },
166 1 };
167 },
168 0 ▶ output_code => sub { _http_tiny('HTTP::Tiny::Retry', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 1 );
170  
171 gen_modified_sub(
172 output_name => 'http_tiny_customretry',
173 base_name => 'http_tiny',
174 summary => 'Perform request(s) with HTTP::Tiny::CustomRetry',
175 description => <<'MARKDOWN',
176  
177 Like `http_tiny`, but uses <pm:HTTP::Tiny::CustomRetry> instead of
178 <pm:HTTP::Tiny>. See the documentation of HTTP::Tiny::CustomRetry for more
179 details.
180  
181 MARKDOWN
182 1 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=1 SCAR=2.6
183 1 my $meta = shift;
184  
185 $meta->{args}{attributes}{cmdline_aliases} = {
186 retry_strategy => {
187 summary => 'Choose backoff strategy',
188 0 ▶ code => sub { $_[0]{attributes}{retry_strategy} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 # disabled, unrecognized for now
190 0 ▶ _completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 require Complete::Module;
192  
193 0 my %args = @_;
194  
195 Complete::Module::complete_module(
196 word => $args{word},
197 0 ns_prefix => 'Algorithm::Backoff',
198 );
199 },
200 },
201 1 };
202 },
203 0 ▶ output_code => sub { _http_tiny('HTTP::Tiny::CustomRetry', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 1 );
205  
206 gen_modified_sub(
207 output_name => 'http_tiny_plugin_every',
208 base_name => 'http_tiny',
209 summary => 'Perform request(s) with HTTP::Tiny::Plugin every N seconds, log result in a directory',
210 description => <<'MARKDOWN',
211  
212 Like `http_tiny_plugin`, but perform the request every N seconds and log the
213 result in a directory.
214  
215 MARKDOWN
216 1 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 1 my $meta = shift;
218 $meta->{args}{every} = {
219 1 schema => 'duration*',
220 req => 1,
221 };
222 $meta->{args}{dir} = {
223 1 schema => 'dirname*',
224 req => 1,
225 };
226 },
227 0 ▶ output_code => sub {
Subroutine __ANON__: not called CC=2 SCAR=14.6
228 0 require Log::ger::App;
229  
230 0 my %args = @_;
231  
232 0 my $log_dump = Log::ger->get_logger(category => 'Dump');
233  
234 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
235 0 shift @Log::ger::App::IMPORT_ARGS;
236 #log_trace("Existing Log::ger::App import: %s", \@Log::ger::App::IMPORT_ARGS);
237 Log::ger::App->import(
238 @Log::ger::App::IMPORT_ARGS,
239 outputs => {
240 DirWriteRotate => {
241 conf => {
242 path => $args{dir},
243 0 max_files => 10_000,
244 },
245 level => 'off',
246 category_level => {
247 Dump => 'info',
248 },
249 },
250 },
251 extra_conf => {
252 category_level => {
253 Dump => 'off',
254 },
255 },
256 );
257  
258 0 while (1) {
259 0 my $res = _http_tiny('HTTP::Tiny::Plugin', %args);
260 0 ▶ if ($res->[0] !~ /^(200|304)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->[0] =~ /^(200|304)/) { } 0 0 0
261 0 log_warn "Failed: $res->[1], skipped saving to directory";
262 } else {
263 0 $log_dump->info($res->[2]);
264 }
265 0 log_trace "Sleeping %s second(s) ...", $args{every};
266 0 sleep $args{every};
267 }
268 0 [200];
269 },
270 1 );
271  
272 gen_modified_sub(
273 output_name => 'http_tinyish',
274 base_name => 'http_tiny',
275 summary => 'Perform request(s) with HTTP::Tinyish',
276 description => <<'MARKDOWN',
277  
278 Like `http_tiny`, but uses <pm:HTTP::Tinyish> instead of <pm:HTTP::Tiny>.
279 See the documentation of HTTP::Tinyish for more details.
280  
281 Observes `HTTP_TINYISH_PREFERRED_BACKEND` to set
282 `$HTTP::Tinyish::PreferredBackend`. For example:
283  
284 % HTTP_TINYISH_PREFERRED_BACKEND=HTTP::Tinyish::Curl http-tinyish https://foo/
285  
286 MARKDOWN
287 0 ▶ output_code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
288 0 require HTTP::Tinyish;
289 0 ▶ if (defined $ENV{HTTP_TINYISH_PREFERRED_BACKEND}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"HTTP_TINYISH_PREFERRED_BACKEND"}) 0 0 0
290 0 $HTTP::Tinyish::PreferredBackend = $ENV{HTTP_TINYISH_PREFERRED_BACKEND};
291 }
292 0 _http_tiny('HTTP::Tinyish', @_);
293 },
294 1 );
295  
296 1 1;
297 # ABSTRACT: Command-line utilities related to HTTP::Tiny
298  
299 __END__
↑ Summary