blib/lib/HTTP/Simple.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 89.1% 41 / 46 Conditioncond 100% 18 / 18 MC/DCmcdc 100% 12 / 12 Subroutinesub 100% 27 / 27 Podpod 100% 15 / 15 totaltotal 97.3% 217 / 223 CC 30 SCAR 10.0
CC
30
Cov
96%
postfile
5.0
getstore
4.1
getjson
4.0
CRAP 2.7
↑ Summary
1 package HTTP::Simple;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.003';
13  
14 2 my @request_functions = qw(get getjson head getprint getstore mirror postform postjson postfile);
15 2 my @status_functions = qw(is_info is_success is_redirect is_error is_client_error is_server_error);
16 2 our @EXPORT = (@request_functions, @status_functions);
17 2 our %EXPORT_TAGS = (
18 all => [@request_functions, @status_functions],
19 request => \@request_functions,
20 status => \@status_functions,
21 );
22  
23 2 our $UA = HTTP::Tiny->new(agent => "HTTP::Simple/$VERSION");
24 2 our $JSON = JSON::PP->new->utf8->canonical->allow_nonref->convert_blessed;
25  
26 sub get {
27 3 ▶ my ($url) = @_;
Subroutine get: called CC=3 SCAR=11.0
Pod documented
28 3 my $res = $UA->get($url);
29 3 ▶ return $res->{content} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 1 2 3
30 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
31 1 croak "$res->{status} $res->{reason}";
32 }
33  
34 sub getjson {
35 7 ▶ my ($url) = @_;
Subroutine getjson: called CC=4 SCAR=13.9
Pod documented
36 7 my $res = $UA->get($url);
37 7 ▶ return ref($JSON) ? $JSON->decode($res->{content}) : _load_function($JSON, 'decode_json')->($res->{content}) if $res->{success};
Branch: 2 branches
BranchTrueFalseTotal
if $res->{"success"} 5 2 7
ref $HTTP::Simple::JSON ? : 1 4 5
38 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
39 1 croak "$res->{status} $res->{reason}";
40 }
41  
42 sub head {
43 3 ▶ my ($url) = @_;
Subroutine head: called CC=3 SCAR=11.0
Pod documented
44 3 my $res = $UA->head($url);
45 3 ▶ return $res->{headers} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 1 2 3
46 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
47 1 croak "$res->{status} $res->{reason}";
48 }
49  
50 sub getprint {
51 3 ▶ my ($url) = @_;
Subroutine getprint: called CC=2 SCAR=6.9
Pod documented
52 3 ▶ my $res = $UA->get($url, {data_callback => sub { print $_[0] }});
Subroutine __ANON__: called CC=1 SCAR=0.0
53 3 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 2 3
54 2 return $res->{status};
55 }
56  
57 sub getstore {
58 2 ▶ my ($url, $file) = @_;
Subroutine getstore: called CC=4 SCAR=14.1
Pod documented
59 2 my $temp = File::Temp->new(DIR => dirname $file);
60 2 ▶ my $res = $UA->get($url, {data_callback => sub { print {$temp} $_[0] }});
Subroutine __ANON__: called CC=1 SCAR=0.0
61 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 0 2 2
62 2 ▶ close $temp or croak "Failed to close $temp: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $temp or croak("Failed to close ${temp}: $!") 0 2 2
63 2 ▶ rename $temp->filename, $file or croak "Failed to rename $temp to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp->filename, $file or croak("Failed to rename $temp to ${file}: $!") 0 2 2
64 2 $temp->unlink_on_destroy(0);
65 2 return $res->{status};
66 }
67  
68 sub mirror {
69 3 ▶ my ($url, $file) = @_;
Subroutine mirror: called CC=3 SCAR=11.0
Pod documented
70 3 my $res = $UA->mirror($url, $file);
71 3 ▶ return $res->{status} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 1 2 3
72 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
73 1 croak "$res->{status} $res->{reason}";
74 }
75  
76 sub postform {
77 3 ▶ my ($url, $form) = @_;
Subroutine postform: called CC=3 SCAR=11.0
Pod documented
78 3 my $res = $UA->post_form($url, $form);
79 3 ▶ return $res->{content} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 1 2 3
80 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
81 1 croak "$res->{status} $res->{reason}";
82 }
83  
84 sub postjson {
85 4 ▶ my ($url, $data) = @_;
Subroutine postjson: called CC=4 SCAR=13.9
Pod documented
86 4 my %options;
87 4 $options{headers} = {'Content-Type' => 'application/json; charset=UTF-8'};
88 4 ▶ $options{content} = ref($JSON) ? $JSON->encode($data) : _load_function($JSON, 'encode_json')->($data);
Branch: 1 branch
BranchTrueFalseTotal
ref $HTTP::Simple::JSON ? : 3 1 4
89 4 my $res = $UA->post($url, \%options);
90 4 ▶ return $res->{content} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 2 2 4
91 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
92 1 croak "$res->{status} $res->{reason}";
93 }
94  
95 sub postfile {
96 5 ▶ my ($url, $file, $content_type) = @_;
Subroutine postfile: called CC=5 SCAR=16.1
Pod documented
97 5 ▶ open my $fh, '<:raw', $file or croak "Failed to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or croak("Failed to open ${file}: $!") 0 5 5
98 5 my %options;
99 5 ▶ $options{headers} = {'Content-Type' => $content_type} if defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type 0 5 5
100 5 my $chunk = 131072;
101 5 ▶ $options{content} = sub { my $buffer; sysread $fh, $buffer, $chunk; $buffer };
Subroutine __ANON__: called CC=1 SCAR=0.0
102 5 my $res = $UA->post($url, \%options);
103 5 ▶ return $res->{content} if $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"success"} 3 2 5
104 2 ▶ croak $res->{content} if $res->{status} == 599;
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} == 599 1 1 2
105 1 croak "$res->{status} $res->{reason}";
106 }
107  
108 104 ▶sub is_info { !!($_[0] >= 100 && $_[0] < 200) }
Subroutine is_info: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 100 && $_[0] < 200
!ll&&!rl&&r
31100
MC/DC100%2/2
$_[0] >= 100 && $_[0] < 200
$_[0] >= 100 $_[0] < 200
Truth table100%3/3
A && B
A= $_[0] >= 100B= $_[0] < 200
ABresult
0X0
100
111
109 104 ▶sub is_success { !!($_[0] >= 200 && $_[0] < 300) }
Subroutine is_success: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 200 && $_[0] < 300
!ll&&!rl&&r
31100
MC/DC100%2/2
$_[0] >= 200 && $_[0] < 300
$_[0] >= 200 $_[0] < 300
Truth table100%3/3
A && B
A= $_[0] >= 200B= $_[0] < 300
ABresult
0X0
100
111
110 104 ▶sub is_redirect { !!($_[0] >= 300 && $_[0] < 400) }
Subroutine is_redirect: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 300 && $_[0] < 400
!ll&&!rl&&r
31100
MC/DC100%2/2
$_[0] >= 300 && $_[0] < 400
$_[0] >= 300 $_[0] < 400
Truth table100%3/3
A && B
A= $_[0] >= 300B= $_[0] < 400
ABresult
0X0
100
111
111 205 ▶sub is_error { !!($_[0] >= 400 && $_[0] < 600) }
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 400 && $_[0] < 600
!ll&&!rl&&r
41200
MC/DC100%2/2
$_[0] >= 400 && $_[0] < 600
$_[0] >= 400 $_[0] < 600
Truth table100%3/3
A && B
A= $_[0] >= 400B= $_[0] < 600
ABresult
0X0
100
111
112 104 ▶sub is_client_error { !!($_[0] >= 400 && $_[0] < 500) }
Subroutine is_client_error: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 400 && $_[0] < 500
!ll&&!rl&&r
31100
MC/DC100%2/2
$_[0] >= 400 && $_[0] < 500
$_[0] >= 400 $_[0] < 500
Truth table100%3/3
A && B
A= $_[0] >= 400B= $_[0] < 500
ABresult
0X0
100
111
113 104 ▶sub is_server_error { !!($_[0] >= 500 && $_[0] < 600) }
Subroutine is_server_error: called CC=2 SCAR=6.9
Pod documented
Condition100%3/3
$_[0] >= 500 && $_[0] < 600
!ll&&!rl&&r
31100
MC/DC100%2/2
$_[0] >= 500 && $_[0] < 600
$_[0] >= 500 $_[0] < 600
Truth table100%3/3
A && B
A= $_[0] >= 500B= $_[0] < 600
ABresult
0X0
100
111
114  
115 sub _load_function {
116 5 ▶ my ($module, $function) = @_;
Subroutine _load_function: called CC=2 SCAR=7.0
117 5 my $code = $module->can($function);
118 5 ▶ return $code if defined $code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 3 2 5
119 2 (my $path = $module) =~ s{::}{/}g;
120 2 require "$path.pm";
121 0 return $module->can($function);
122 }
123  
124 2 1;
125  
126 =head1 NAME
127  
128 HTTP::Simple - Simple procedural interface to HTTP::Tiny
129  
130 =head1 SYNOPSIS
131  
132 perl -MHTTP::Simple -e'getprint(shift)' 'https://example.com'
133  
134 use HTTP::Simple;
135  
136 my $content = get 'https://example.com';
137  
138 if (mirror('https://example.com', '/path/to/file.html') == 304) { ... }
139  
140 if (is_success(getprint 'https://example.com')) { ... }
141  
142 postform('https://example.com', {foo => ['bar', 'baz']});
143  
144 postjson('https://example.com', [{bar => 'baz'}]);
145  
146 postfile('https://example.com', '/path/to/file.png');
147  
148 =head1 DESCRIPTION
149  
150 This module is a wrapper of L<HTTP::Tiny> that provides simplified functions
151 for performing HTTP requests in a similar manner to L<LWP::Simple>, but with
152 slightly more useful error handling. For full control of the request process
153 and response handling, use L<HTTP::Tiny> directly.
154  
155 L<IO::Socket::SSL> is required for HTTPS requests with L<HTTP::Tiny>.
156  
157 Request methods that return the body content of the response will return a byte
158 string suitable for directly printing, but that may need to be
159 L<decoded|Encode/decode> for text operations.
160  
161 The L<HTTP::Tiny> object used by these functions to make requests can be
162 accessed as C<$HTTP::Simple::UA> (for example, to configure the timeout, or
163 replace it with a compatible object like L<HTTP::Tinyish>).
164  
165 The JSON encoder used by the JSON functions defaults to a L<JSON::PP> instance,
166 and can be accessed as C<$HTTP::Simple::JSON>. If replaced with a new object,
167 it should have UTF-8 encoding/decoding enabled (usually the C<utf8> option). If
168 it is set to a string, it will be used as a module name that is expected to
169 have C<decode_json> and C<encode_json> functions.
170  
171 =head1 FUNCTIONS
172  
173 All functions are exported by default. Functions can also be requested
174 individually or with the tags C<:request>, C<:status>, or C<:all>.
175  
176 =head2 get
177  
178 my $contents = get($url);
179  
180 Retrieves the document at the given URL with a GET request and returns it as a
181 byte string. Throws an exception on connection or HTTP errors.
182  
183 =head2 getjson
184  
185 my $data = getjson($url);
186  
187 Retrieves the JSON document at the given URL with a GET request and decodes it
188 from JSON to a Perl structure. Throws an exception on connection, HTTP, or JSON
189 errors.
190  
191 =head2 head
192  
193 my $headers = head($url);
194  
195 Retrieves the headers at the given URL with a HEAD request and returns them as
196 a hash reference. Header field names are normalized to lower case, and values
197 may be an array reference if the header is repeated. Throws an exception on
198 connection or HTTP errors.
199  
200 =head2 getprint
201  
202 my $status = getprint($url);
203  
204 Retrieves the document at the given URL with a GET request and prints it as it
205 is received. Returns the HTTP status code. Throws an exception on connection
206 errors.
207  
208 =head2 getstore
209  
210 my $status = getstore($url, $path);
211  
212 Retrieves the document at the given URL with a GET request and stores it to the
213 given file path. Returns the HTTP status code. Throws an exception on
214 connection or filesystem errors.
215  
216 =head2 mirror
217  
218 my $status = mirror($url, $path);
219  
220 Retrieves the document at the given URL with a GET request and mirrors it to
221 the given file path, using the C<If-Modified-Since> headers to short-circuit if
222 the file exists and is new enough, and the C<Last-Modified> header to set its
223 modification time. Returns the HTTP status code. Throws an exception on
224 connection, HTTP, or filesystem errors.
225  
226 =head2 postform
227  
228 my $contents = postform($url, $form);
229  
230 Sends a POST request to the given URL with the given hash or array reference of
231 form data serialized to C<application/x-www-form-urlencoded>. Returns the
232 response body as a byte string. Throws an exception on connection or HTTP
233 errors.
234  
235 =head2 postjson
236  
237 my $contents = postjson($url, $data);
238  
239 Sends a POST request to the given URL with the given data structure encoded to
240 JSON. Returns the response body as a byte string. Throws an exception on
241 connection, HTTP, or JSON errors.
242  
243 =head2 postfile
244  
245 my $contents = postfile($url, $path);
246 my $contents = postfile($url, $path, $content_type);
247  
248 Sends a POST request to the given URL, streaming the contents of the given
249 file. The content type is passed as C<application/octet-stream> if not
250 specified. Returns the response body as a byte string. Throws an exception on
251 connection, HTTP, or filesystem errors.
252  
253 =head2 is_info
254  
255 my $bool = is_info($status);
256  
257 Returns true if the status code indicates an informational response (C<1xx>).
258  
259 =head2 is_success
260  
261 my $bool = is_success($status);
262  
263 Returns true if the status code indicates a successful response (C<2xx>).
264  
265 =head2 is_redirect
266  
267 my $bool = is_redirect($status);
268  
269 Returns true if the status code indicates a redirection response (C<3xx>).
270  
271 =head2 is_error
272  
273 my $bool = is_error($status);
274  
275 Returns true if the status code indicates an error response (C<4xx> or C<5xx>).
276  
277 =head2 is_client_error
278  
279 my $bool = is_client_error($status);
280  
281 Returns true if the status code indicates a client error response (C<4xx>).
282  
283 =head2 is_server_error
284  
285 my $bool = is_server_error($status);
286  
287 Returns true if the status code indicates a server error response (C<5xx>).
288  
289 =head1 BUGS
290  
291 Report any issues on the public bugtracker.
292  
293 =head1 AUTHOR
294  
295 Dan Book <dbook@cpan.org>
296  
297 =head1 COPYRIGHT AND LICENSE
298  
299 This software is Copyright (c) 2019 by Dan Book.
300  
301 This is free software, licensed under:
302  
303 The Artistic License 2.0 (GPL Compatible)
304  
305 =head1 SEE ALSO
306  
307 L<LWP::Simple>, L<HTTP::Tinyish>, L<ojo>
↑ Summary