At
view release on metacpan or search on metacpan
lib/At/UserAgent.pm view on Meta::CPAN
use HTTP::Tiny;
field $agent : param
= HTTP::Tiny->new( agent => 'At.pm/Tiny', default_headers => { 'Content-Type' => 'application/json', Accept => 'application/json' } );
method get( $url, $req = {} ) {
$req //= {};
$req->{headers}{DPoP} = $self->_generate_dpop_proof( $url, 'GET', $req->{skip_ath} ) if $self->token_type eq 'DPoP';
my $res
= $agent->get( $url . ( defined $req->{content} && keys %{ $req->{content} } ? '?' . $agent->www_form_urlencode( $req->{content} ) : '' ),
{ defined $req->{headers} ? ( headers => $req->{headers} ) : () } );
$res->{content} = JSON::PP::decode_json( $res->{content} ) if $res->{content} && ( $res->{headers}{'content-type'} // '' ) =~ m[json];
unless ( $res->{success} ) {
my $msg = $res->{reason} // 'Unknown error';
if ( ref $res->{content} eq 'HASH' ) {
my $json = $res->{content};
my $details = $json->{error} // '';
if ( $json->{message} && $json->{message} ne $details ) {
$details .= ( $details ? ': ' : '' ) . $json->{message};
}
$msg .= ": " . $details if $details;
$msg .= " - " . $json->{error_description} if $json->{error_description};
lib/At/UserAgent.pm view on Meta::CPAN
elsif ( ref $req->{content} ) {
$content = JSON::PP::encode_json( $req->{content} );
$req->{headers}{'Content-Type'} = 'application/json';
}
else {
$content = $req->{content};
}
}
my $res = $agent->post( $url,
{ defined $req->{headers} ? ( headers => $req->{headers} ) : (), defined $content ? ( content => $content ) : () } );
$res->{content} = JSON::PP::decode_json( $res->{content} ) if $res->{content} && ( $res->{headers}{'content-type'} // '' ) =~ m[json];
unless ( $res->{success} ) {
my $msg = $res->{reason} // 'Unknown error';
if ( ref $res->{content} eq 'HASH' ) {
my $json = $res->{content};
my $details = $json->{error} // '';
if ( $json->{message} && $json->{message} ne $details ) {
$details .= ( $details ? ': ' : '' ) . $json->{message};
}
$msg .= ": " . $details if $details;
$msg .= " - " . $json->{error_description} if $json->{error_description};
( run in 0.541 second using v1.01-cache-2.11-cpan-524268b4103 )