lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/REST.pm

42 lines
1.0 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Portal::Lib::REST;
use strict;
use Mouse;
use Lemonldap::NG::Common::UserAgent;
use JSON qw(from_json to_json);
our $VERSION = '2.0.6';
2017-02-28 21:53:19 +01:00
has ua => (
is => 'rw',
lazy => 1,
default => sub {
return Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
}
);
sub restCall {
2017-03-03 18:25:03 +01:00
my ( $self, $url, $content ) = @_;
2019-08-22 15:17:51 +02:00
$self->logger->debug("REST: trying to call $url with:");
eval {
foreach ( keys %$content ) {
$self->logger->debug(
" $_: " . ( /password/ ? '****' : $content->{$_} ) );
}
};
my $hreq = HTTP::Request->new( POST => $url );
$hreq->header( 'Content-Type' => 'application/json' );
2017-03-03 18:25:03 +01:00
$hreq->content( to_json($content) );
my $resp = $self->ua->request($hreq);
2020-10-01 21:49:00 +02:00
die $resp->status_line unless $resp->is_success;
my $res = eval { from_json( $resp->content ) };
2017-03-03 18:25:03 +01:00
die "Bad REST response: $@" if ($@);
if ( ref($res) ne "HASH" ) {
die "Bad REST response: expecting a JSON HASH, got " . ref($res);
}
return $res;
}
1;