App-HTTPTinyUtils
view release on metacpan or search on metacpan
lib/App/HTTPTinyUtils.pm view on Meta::CPAN
sub _http_tiny {
my ($class, %args) = @_;
(my $class_pm = "$class.pm") =~ s!::!/!g;
require $class_pm;
my $res;
my $method = $args{method} // 'GET';
for my $i (0 .. $#{ $args{urls} }) {
my $url = $args{urls}[$i];
my $is_last_url = $i == $#{ $args{urls} };
my %opts;
if (defined $args{content}) {
$opts{content} = $args{content};
## no critic: InputOutput::ProhibitInteractiveTest
} elsif (!(-t STDIN)) {
local $/;
$opts{content} = <STDIN>;
}
log_trace "Request: $method $url ...";
my $res0 = $class->new(%{ $args{attributes} // {} })
->request($method, $url, \%opts);
my $success = $res0->{success};
if ($args{raw}) {
$res = [200, "OK", $res0];
} else {
$res = [$res0->{status}, $res0->{reason}, $res0->{content}];
print $res0->{content} unless $is_last_url;
}
unless ($success) {
last unless $args{ignore_errors};
}
}
$res;
}
$SPEC{http_tiny} = {
v => 1.1,
summary => 'Perform request(s) with HTTP::Tiny',
args => {
urls => {
'x.name.is_plural' => 1,
'x.name.singular' => 'url',
schema => ['array*', of=>'str*'],
req => 1,
pos => 0,
slurpy => 1,
},
method => {
schema => ['str*', match=>qr/\A[A-Z]+\z/],
default => 'GET',
cmdline_aliases => {
delete => {summary => 'Shortcut for --method DELETE', is_flag=>1, code=>sub { $_[0]{method} = 'DELETE' } },
get => {summary => 'Shortcut for --method GET' , is_flag=>1, code=>sub { $_[0]{method} = 'GET' } },
head => {summary => 'Shortcut for --method HEAD' , is_flag=>1, code=>sub { $_[0]{method} = 'HEAD' } },
post => {summary => 'Shortcut for --method POST' , is_flag=>1, code=>sub { $_[0]{method} = 'POST' } },
put => {summary => 'Shortcut for --method PUT' , is_flag=>1, code=>sub { $_[0]{method} = 'PUT' } },
},
},
attributes => {
'x.name.is_plural' => 1,
'x.name.singular' => 'attribute',
summary => 'Pass attributes to HTTP::Tiny constructor',
schema => ['hash*', each_key => 'str*'],
},
headers => {
schema => ['hash*', of=>'str*'],
'x.name.is_plural' => 1,
'x.name.singular' => 'header',
},
content => {
schema => 'str*',
},
raw => {
schema => 'bool*',
},
ignore_errors => {
summary => 'Ignore errors',
description => <<'MARKDOWN',
Normally, when given multiple URLs, the utility will exit after the first
non-success response. With `ignore_errors` set to true, will just log the error
and continue. Will return with the last error response.
MARKDOWN
schema => 'bool*',
cmdline_aliases => {i=>{}},
},
# XXX option: agent
# XXX option: timeout
# XXX option: post form
},
};
sub http_tiny {
_http_tiny('HTTP::Tiny', @_);
}
gen_modified_sub(
output_name => 'http_tiny_cache',
base_name => 'http_tiny',
summary => 'Perform request(s) with HTTP::Tiny::Cache',
description => <<'MARKDOWN',
Like `http_tiny`, but uses <pm:HTTP::Tiny::Cache> instead of <pm:HTTP::Tiny>.
See the documentation of HTTP::Tiny::Cache on how to set cache period.
MARKDOWN
output_code => sub { _http_tiny('HTTP::Tiny::Cache', @_) },
);
gen_modified_sub(
output_name => 'http_tiny_plugin',
base_name => 'http_tiny',
summary => 'Perform request(s) with HTTP::Tiny::Plugin',
description => <<'MARKDOWN',
Like `http_tiny`, but uses <pm:HTTP::Tiny::Plugin> instead of <pm:HTTP::Tiny>.
( run in 0.725 second using v1.01-cache-2.11-cpan-39bf76dae61 )