Zapp/Task/Request.pm

filter: Statementstmt 17.9% 7 / 39 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 2 / 4 Podpod 50.0% 1 / 2 totaltotal 13.3% 10 / 75 CC 14 SCAR 51.5
CC
14
Cov
11%
run
210.0
schema
2.0
BEGIN
1.0
CRAP 173.2
« Zapp/Controller/Plan.pm ↑ Summary Zapp/Task/Script.pm »
1 package Zapp::Task::Request;
2 # ABSTRACT: Make an HTTP Request
3  
4 #pod =head1 DESCRIPTION
5 #pod
6 #pod This task makes an HTTP request for a web site.
7 #pod
8 #pod =head2 Output
9 #pod
10 #pod is_success - True if the response was successful
11 #pod code - The response code (like 200 or 404)
12 #pod message - The HTTP message (like "OK" or "Not Found")
13 #pod json - If the response was JSON, the decoded JSON structure
14 #pod file - If the response was a file, the path to the file
15 #pod body - If the response was anything else, the response body
16 #pod headers - The headers from the response as a hash
17 #pod content_type - The Content-Type header
18 #pod location - The Location header
19 #pod
20 #pod =head1 SEE ALSO
21 #pod
22 #pod =cut
23  
24 1 ▶use Mojo::Base 'Zapp::Task', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Mojo::JSON qw( false true encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ▶sub schema( $class ) {
Subroutine schema: not called CC=1 SCAR=6.9
Pod undocumented
28 return {
29 0 input => {
30 type => 'object',
31 required => [qw( url )],
32 properties => {
33 url => {
34 type => 'string',
35 },
36 method => {
37 type => 'string',
38 enum => [qw( GET POST PUT DELETE PATCH OPTIONS PROPFIND )],
39 default => 'GET',
40 },
41 auth => {
42 type => 'object',
43 properties => {
44 type => {
45 type => 'string',
46 enum => [ '', qw( bearer )],
47 },
48 token => {
49 type => 'string',
50 },
51 },
52 additionalProperties => false,
53 },
54 content_type => {
55 type => 'string',
56 enum => ['', qw( application/octet-stream application/json )],
57 default => '',
58 },
59 # XXX: Query params / Form params
60 # XXX: Request headers
61 # XXX: Cookies (must be easily passed between requests,
62 # or automatically saved to the context)
63 # XXX: JSON subclass that parses JSON responses
64 # XXX: DOM subclass that extracts DOM text/attrs
65 },
66 additionalProperties => false,
67 },
68 output => {
69 type => 'object',
70 properties => {
71 is_success => {
72 type => 'boolean',
73 },
74 code => {
75 type => 'integer',
76 minValue => 100,
77 maxValue => 599,
78 },
79 message => {
80 type => 'string',
81 },
82 body => {
83 type => 'string',
84 },
85 headers => {
86 type => 'object',
87 properties => {
88 content_type => {
89 type => 'string',
90 },
91 },
92 additionalProperties => false,
93 },
94 },
95 additionalProperties => false,
96 },
97 };
98 }
99  
100 ▶sub run( $self, $input ) {
Subroutine run: not called CC=14 SCAR=53.5
Pod documented
101 # XXX: Need a way to see what type of input was given so we can
102 # decide what to do with it if we need to. Then we wouldn't have to
103 # have different input fields for different types...
104 0 my $body;
105 0 ▶ if ( $input->{content_type} eq 'application/octet-stream' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($input->{'content_type'} eq 'application/octet-stream') { } 0 0 0
elsif ($input->{'content_type'} eq 'application/json') { } 0 0 0
106 0 $body = Mojo::Asset::File->new( path => $input->{body}{file} )->slurp;
107 }
108 elsif ( $input->{content_type} eq 'application/json' ) {
109 0 $body = $input->{body}{json};
110 0 ▶ $body = encode_json( $body ) if ref $body;
Branch: 1 branch
BranchTrueFalseTotal
if ref $body 0 0 0
111 }
112  
113 0 my $ua = $self->app->ua;
114 0 ▶ $ua->max_redirects( $input->{max_redirects} // 10 );
Condition0%0/2
$input->{"max_redirects"} // 10
l!l
00
MC/DC0%0/1
$input->{"max_redirects"} // 10
$input->{"max_redirects"}
Truth table0%0/2
A
A= $input->{"max_redirects"}
Aresult
11
00
115 #; $self->app->log->debug( 'input: ' . $self->app->dumper( $input ) );
116 0 my %headers;
117 0 ▶ if ( $input->{auth} && $input->{auth}{type} eq 'bearer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"auth"} and $input->{"auth"}{"type"} eq "bearer") 0 0 0
Condition0%0/3
$input->{"auth"} and $input->{"auth"}{"type"} eq "bearer"
!ll&&!rl&&r
000
MC/DC0%0/2
$input->{"auth"} and $input->{"auth"}{"type"} eq "bearer"
$input->{"auth"} $input->{"auth"}{"type"} eq "bearer"
Truth table0%0/3
A and B
A= $input->{"auth"}B= $input->{"auth"}{"type"} eq "bearer"
ABresult
0X0
100
111
118 0 $headers{ Authorization } = join ' ', 'Bearer', $input->{auth}{token};
119 }
120 0 ▶ if ( $input->{content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"content_type"}) 0 0 0
121 0 $headers{ 'Content-Type' } = $input->{content_type};
122 }
123  
124 my $tx = $ua->build_tx(
125 $input->{method} => $input->{url},
126 \%headers,
127 0 ▶ $input->{content_type} ? ( $body ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$input->{'content_type'} ? : 0 0 0
128 );
129 0 $tx = $ua->start( $tx );
130 # ; use Data::Dumper;
131 # ; say Dumper $tx->res;
132 0 ▶ my $method = !$tx->res->error ? 'finish' : 'fail';
Branch: 1 branch
BranchTrueFalseTotal
!$tx->res->error ? : 0 0 0
133  
134 0 my @res_body;
135 0 ▶ if ( $tx->res->headers->content_type eq 'application/octet-stream' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tx->res->headers->content_type eq 'application/octet-stream') { } 0 0 0
elsif ($tx->res->headers->content_type =~ m[^application/json]u) { } 0 0 0
elsif ($tx->res->headers->content_type =~ m[^text/]u) { } 0 0 0
136 0 my ( $filename ) = grep !!$_, reverse split m{/}, $input->{url};
137 0 ▶ if ( $tx->res->headers->content_disposition =~ m{filename\*?=['"]?([^'"]+)['"]?} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->headers->content_disposition =~ /filename\*?=['"]?([^'"]+)['"]?/u) 0 0 0
138 0 $filename = $1;
139 }
140 0 my $path = $self->app->static->paths->[0];
141 0 my $file = Mojo::File->new( $path, 'task', 'request', $self->id, $filename );
142 0 $file->dirname->make_path;
143 0 $file->spurt( $tx->res->body );
144 0 @res_body = (
145 file => '/' . $file->to_rel( $self->app->home->child( 'public' ) ),
146 );
147 #; $self->app->log->debug( 'File content: ' . $file->slurp );
148 }
149 elsif ( $tx->res->headers->content_type =~ m{^application/json} ) {
150 0 @res_body = ( json => $tx->res->json );
151 }
152 elsif ( $tx->res->headers->content_type =~ m{^text/} ) {
153 0 @res_body = ( body => $tx->res->body );
154 }
155  
156 $self->$method({
157 is_success => !defined $tx->res->error,
158 (
159 0 map { $_ => $tx->res->$_ }
160 qw( code message )
161 ),
162 # XXX: Use $tx->res->headers->to_hash after Formula has a way to
163 # look up hash keys with special characters
164 headers => {
165 0 map { $_ => $tx->res->headers->$_ }
166 0 grep { $tx->res->headers->$_ }
167 qw( content_type location )
168 },
169 @res_body,
170 });
171 }
172  
173 1 1;
174  
175 =pod
176  
177 =head1 NAME
178  
179 Zapp::Task::Request - Make an HTTP Request
180  
181 =head1 VERSION
182  
183 version 0.004
184  
185 =head1 DESCRIPTION
186  
187 This task makes an HTTP request for a web site.
188  
189 =head2 Output
190  
191 is_success - True if the response was successful
192 code - The response code (like 200 or 404)
193 message - The HTTP message (like "OK" or "Not Found")
194 json - If the response was JSON, the decoded JSON structure
195 file - If the response was a file, the path to the file
196 body - If the response was anything else, the response body
197 headers - The headers from the response as a hash
198 content_type - The Content-Type header
199 location - The Location header
200  
201 =head1 SEE ALSO
202  
203 =head1 AUTHOR
204  
205 Doug Bell <preaction@cpan.org>
206  
207 =head1 COPYRIGHT AND LICENSE
208  
209 This software is copyright (c) 2021 by Doug Bell.
210  
211 This is free software; you can redistribute it and/or modify it under
212 the same terms as the Perl 5 programming language system itself.
213  
214 =cut
215  
216 __DATA__
« Zapp/Controller/Plan.pm ↑ Summary Zapp/Task/Script.pm »