2017-02-23 22:46:00 +01:00
|
|
|
package Lemonldap::NG::Portal::Lib::REST;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use Lemonldap::NG::Common::UserAgent;
|
|
|
|
use JSON qw(from_json to_json);
|
|
|
|
|
2017-02-28 21:53:19 +01:00
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
2017-02-23 22:46:00 +01:00
|
|
|
has ua => (
|
|
|
|
is => 'rw',
|
2017-03-27 18:51:18 +02:00
|
|
|
lazy => 1,
|
2017-02-23 22:46:00 +01:00
|
|
|
default => sub {
|
|
|
|
return Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
sub restCall {
|
2017-03-03 18:25:03 +01:00
|
|
|
my ( $self, $url, $content ) = @_;
|
2017-02-23 22:46:00 +01:00
|
|
|
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) );
|
2017-02-23 22:46:00 +01:00
|
|
|
my $resp = $self->ua->request($hreq);
|
|
|
|
unless ( $resp->is_success ) {
|
|
|
|
die $resp->status_line;
|
|
|
|
}
|
2017-09-28 14:52:14 +02:00
|
|
|
my $res = eval { from_json( $resp->content, { allow_nonref => 1 } ) };
|
2017-03-03 18:25:03 +01:00
|
|
|
die "Bad REST response: $@" if ($@);
|
2017-02-23 22:46:00 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|