« blib/lib/Lemonldap/NG/Portal/2F/Register/Base.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Main/Menu.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.23.0'; | |||||||||||
| 9 | |||||||||||||
| 10 | has ua => ( | ||||||||||||
| 11 | is => 'rw', | ||||||||||||
| 12 | lazy => 1, | ||||||||||||
| 13 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 14 | 0 | return Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} ); | |||||||||||
| 15 | } | ||||||||||||
| 16 | 8 | ); | |||||||||||
| 17 | |||||||||||||
| 18 | 9 | ▶ | has namedCalls => ( is => 'rw', default => sub { {} } ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 11 | ▶ | sub initNamedCallFromConf { | ||||||||||
|
Subroutine
initNamedCallFromConf: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||
| 21 | 11 | my ( $self, $name_conf, $name_internal, $mandatory ) = @_; | |||||||||||
| 22 | |||||||||||||
| 23 | 11 | my $name_internal_url = "${name_internal}Url"; | |||||||||||
| 24 | 11 | my $name_internal_attrs = "${name_internal}Attrs"; | |||||||||||
| 25 | |||||||||||||
| 26 | 11 | ▶ | if ( !$self->conf->{"${name_conf}Url"} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 1 | $self->logger->debug("${name_conf}Url is missing"); | |||||||||||
| 28 | 1 | return 0; | |||||||||||
| 29 | } | ||||||||||||
| 30 | $self->namedCalls->{$name_internal}->{url} = | ||||||||||||
| 31 | 10 | $self->conf->{"${name_conf}Url"}; | |||||||||||
| 32 | |||||||||||||
| 33 | 10 | ▶ | while ( my ( $k, $rule_or_attr ) = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 21 | each %{ $self->conf->{"${name_conf}Args"} } ) | |||||||||||
| 35 | { | ||||||||||||
| 36 | 11 | my $rule; | |||||||||||
| 37 | |||||||||||||
| 38 | # Tolerate using the name of an attribute instead of an actual | ||||||||||||
| 39 | # perl expression for backward compatibility | ||||||||||||
| 40 | 11 | ▶ | if ( $rule_or_attr =~ /^\w+$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 8 | $rule = $self->p->buildRule( "\$$rule_or_attr", | |||||||||||
| 42 | "REST $name_internal_attrs custom argument $k" ); | ||||||||||||
| 43 | } | ||||||||||||
| 44 | else { | ||||||||||||
| 45 | 3 | $rule = $self->p->buildRule( $rule_or_attr, | |||||||||||
| 46 | "REST $name_internal_attrs custom argument $k" ); | ||||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 11 | ▶ | next unless $rule; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 11 | $self->logger->debug("$name_conf: push verify attribute $k"); | |||||||||||
| 51 | 11 | $self->namedCalls->{$name_internal}->{attrs}->{$k} = $rule; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 10 | return 1; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | sub restNamedCall { | ||||||||||
|
Subroutine
restNamedCall: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 58 | 0 | my ( $self, $req, $name, $initial_content, $session_info ) = @_; | |||||||||||
| 59 | 0 | my $url_accessor = "${name}Url"; | |||||||||||
| 60 | 0 | my $attr_accessor = "${name}Attrs"; | |||||||||||
| 61 | 0 | my $url = $self->namedCalls->{$name}->{url}; | |||||||||||
| 62 | |||||||||||||
| 63 | 0 | ▶ | die "Could not find URL for $name REST call" unless $url; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | ▶ | my $content = { %{ $initial_content || {} } }; | ||||||||||
|
Condition0%0/2
$initial_content || {}
MC/DC0%0/1
$initial_content || {}
$initial_content
Truth table0%0/2
A
A= $initial_content
| |||||||||||||
| 65 | 0 | ▶ | while ( my ( $json_key, $rule ) = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | each %{ $self->namedCalls->{$name}->{attrs} } ) | |||||||||||
| 67 | { | ||||||||||||
| 68 | 0 | my $value = $rule->( $req, $session_info ); | |||||||||||
| 69 | 0 | ▶ | if ( defined $value ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | $content->{$json_key} = $value; | |||||||||||
| 71 | } | ||||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 0 | return $self->restCall( $url, $content ); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | sub restCall { | ||||||||||
|
Subroutine
restCall: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 78 | 0 | my ( $self, $url, $content ) = @_; | |||||||||||
| 79 | 0 | $self->logger->debug("REST: trying to call $url with:"); | |||||||||||
| 80 | 0 | eval { | |||||||||||
| 81 | 0 | foreach ( keys %$content ) { | |||||||||||
| 82 | $self->logger->debug( | ||||||||||||
| 83 | 0 | ▶ | " $_: " . ( /password/ ? '****' : $content->{$_} ) ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | }; | ||||||||||||
| 86 | 0 | my $hreq = HTTP::Request->new( POST => $url ); | |||||||||||
| 87 | 0 | $hreq->header( 'Content-Type' => 'application/json' ); | |||||||||||
| 88 | 0 | $hreq->content( to_json($content) ); | |||||||||||
| 89 | 0 | my $resp = $self->ua->request($hreq); | |||||||||||
| 90 | 0 | ▶ | die $resp->status_line unless $resp->is_success; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $res = eval { from_json( $resp->content ) }; | |||||||||||
| 93 | 0 | ▶ | die "Bad REST response: $@" if ($@); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | ▶ | if ( ref($res) ne "HASH" ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | die "Bad REST response: expecting a JSON HASH, got " . ref($res); | |||||||||||
| 96 | } | ||||||||||||
| 97 | 0 | return $res; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 8 | 1; | |||||||||||