Zapp/Task/Request.pm

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