our
$AUTHORITY
=
'cpan:MPERRY'
;
$App::Presto::Command::headers::VERSION
=
'0.010'
;
sub
install {
my
$self
=
shift
;
my
$config
=
$self
->config;
my
$client
=
$self
->client;
$self
->term->add_commands(
{
authorization
=> {
minargs
=> 0,
maxargs
=> 2,
desc
=>
'GET/Set basic auth username/password'
,
args
=> [
sub
{
'[username]'
},
sub
{
'[password]'
} ],
proc
=>
sub
{
my
(
$username
,
$password
) =
@_
;
if
(
$username
){
$client
->set_header(
Authorization
=>
sprintf
(
'Basic %s'
,
MIME::Base64::encode(
"$username:$password"
,
''
) )
);
}
elsif
(
my
$auth
=
$client
->get_header(
'Authorization'
) ){
$auth
=~ s/Basic //;
my
(
$u
,
$p
) =
split
(/:/, MIME::Base64::decode(
$auth
), 2 );
print
"Username: $u\nPassword: $p\n"
;
}
},
},
type
=> {
minargs
=> 1,
desc
=>
'Set content-type header'
,
args
=>
sub
{ [
'application/json'
,
'application/x-www-form-urlencoded'
] },
proc
=>
sub
{
$client
->set_header(
'Content-Type'
,
shift
) },
},
headers
=> {
maxargs
=> 0,
alias
=>
'header'
,
},
header
=> {
desc
=>
'get/set/list/clear HTTP headers'
,
args
=> [
sub
{
my
%headers
=
$client
->all_headers;
return
[
'--unset'
,
'--clear'
,
keys
%headers
]} ],
proc
=>
sub
{
my
$header
=
shift
;
my
@args
=
@_
;
if
( !
$header
) {
my
%headers
=
$client
->all_headers;
print
$self
->pretty_print( \
%headers
);
}
elsif
(
$header
eq
'--clear'
) {
$client
->clear_headers;
}
elsif
(
$header
eq
'--unset'
) {
$client
->clear_header(
$args
[0]);
}
elsif
(
@args
) {
$header
=~ s/:$//;
my
$value
=
join
' '
,
@args
;
$client
->set_header(
$header
,
$value
);
}
else
{
print
$self
->pretty_print( {
$header
=>
$client
->get_header(
$header
) });
}
},
},
}
);
}
sub
help_categories {
return
{
desc
=>
'Configure various HTTP headers'
,
cmds
=> [
qw(authorization headers type)
],
};
}
1;