« blib/lib/Lemonldap/NG/Portal/UserDB/REST.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Plugins/CrowdSec.pm »
| 1 | package Lemonldap::NG::Portal::Lib::REST; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 8 | ▶ | use Lemonldap::NG::Common::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 8 | ▶ | use JSON qw(from_json to_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 8 | our $VERSION = '2.0.6'; | |||||||||
| 9 | |||||||||||
| 10 | has ua => ( | ||||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | lazy => 1, | ||||||||||
| 13 | default => sub { | ||||||||||
| 14 | 0 | ▶ | return Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 15 | } | ||||||||||
| 16 | 8 | ); | |||||||||
| 17 | |||||||||||
| 18 | sub restCall { | ||||||||||
| 19 | 0 | ▶ | my ( $self, $url, $content ) = @_; | ||||||||
|
Subroutine
restCall: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 20 | 0 | $self->logger->debug("REST: trying to call $url with:"); | |||||||||
| 21 | 0 | eval { | |||||||||
| 22 | 0 | foreach ( keys %$content ) { | |||||||||
| 23 | $self->logger->debug( | ||||||||||
| 24 | 0 | ▶ | " $_: " . ( /password/ ? '****' : $content->{$_} ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | } | ||||||||||
| 26 | }; | ||||||||||
| 27 | 0 | my $hreq = HTTP::Request->new( POST => $url ); | |||||||||
| 28 | 0 | $hreq->header( 'Content-Type' => 'application/json' ); | |||||||||
| 29 | 0 | $hreq->content( to_json($content) ); | |||||||||
| 30 | 0 | my $resp = $self->ua->request($hreq); | |||||||||
| 31 | 0 | ▶ | die $resp->status_line unless $resp->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | |||||||||||
| 33 | 0 | my $res = eval { from_json( $resp->content ) }; | |||||||||
| 34 | 0 | ▶ | die "Bad REST response: $@" if ($@); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | ▶ | if ( ref($res) ne "HASH" ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | die "Bad REST response: expecting a JSON HASH, got " . ref($res); | |||||||||
| 37 | } | ||||||||||
| 38 | 0 | return $res; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 8 | 1; | |||||||||