Zapp
view release on metacpan or search on metacpan
lib/Zapp/Task/Request.pm view on Meta::CPAN
minValue => 100,
maxValue => 599,
},
message => {
type => 'string',
},
body => {
type => 'string',
},
headers => {
type => 'object',
properties => {
content_type => {
type => 'string',
},
},
additionalProperties => false,
},
},
additionalProperties => false,
},
};
}
sub run( $self, $input ) {
# XXX: Need a way to see what type of input was given so we can
# decide what to do with it if we need to. Then we wouldn't have to
# have different input fields for different types...
my $body;
if ( $input->{content_type} eq 'application/octet-stream' ) {
$body = Mojo::Asset::File->new( path => $input->{body}{file} )->slurp;
}
elsif ( $input->{content_type} eq 'application/json' ) {
$body = $input->{body}{json};
$body = encode_json( $body ) if ref $body;
}
my $ua = $self->app->ua;
$ua->max_redirects( $input->{max_redirects} // 10 );
#; $self->app->log->debug( 'input: ' . $self->app->dumper( $input ) );
my %headers;
if ( $input->{auth} && $input->{auth}{type} eq 'bearer' ) {
$headers{ Authorization } = join ' ', 'Bearer', $input->{auth}{token};
}
if ( $input->{content_type} ) {
$headers{ 'Content-Type' } = $input->{content_type};
}
my $tx = $ua->build_tx(
$input->{method} => $input->{url},
\%headers,
$input->{content_type} ? ( $body ) : (),
);
$tx = $ua->start( $tx );
# ; use Data::Dumper;
# ; say Dumper $tx->res;
my $method = !$tx->res->error ? 'finish' : 'fail';
my @res_body;
if ( $tx->res->headers->content_type eq 'application/octet-stream' ) {
my ( $filename ) = grep !!$_, reverse split m{/}, $input->{url};
if ( $tx->res->headers->content_disposition =~ m{filename\*?=['"]?([^'"]+)['"]?} ) {
$filename = $1;
}
my $path = $self->app->static->paths->[0];
my $file = Mojo::File->new( $path, 'task', 'request', $self->id, $filename );
$file->dirname->make_path;
$file->spurt( $tx->res->body );
@res_body = (
file => '/' . $file->to_rel( $self->app->home->child( 'public' ) ),
);
#; $self->app->log->debug( 'File content: ' . $file->slurp );
}
elsif ( $tx->res->headers->content_type =~ m{^application/json} ) {
@res_body = ( json => $tx->res->json );
}
elsif ( $tx->res->headers->content_type =~ m{^text/} ) {
@res_body = ( body => $tx->res->body );
}
$self->$method({
is_success => !defined $tx->res->error,
(
map { $_ => $tx->res->$_ }
qw( code message )
),
# XXX: Use $tx->res->headers->to_hash after Formula has a way to
# look up hash keys with special characters
headers => {
map { $_ => $tx->res->headers->$_ }
grep { $tx->res->headers->$_ }
qw( content_type location )
},
@res_body,
});
}
1;
=pod
=head1 NAME
Zapp::Task::Request - Make an HTTP Request
=head1 VERSION
version 0.005
=head1 DESCRIPTION
This task makes an HTTP request for a web site.
=head2 Output
is_success - True if the response was successful
code - The response code (like 200 or 404)
message - The HTTP message (like "OK" or "Not Found")
json - If the response was JSON, the decoded JSON structure
file - If the response was a file, the path to the file
body - If the response was anything else, the response body
( run in 0.682 second using v1.01-cache-2.11-cpan-71847e10f99 )