Commit cc98ef1f authored by Côme Chilliet's avatar Côme Chilliet

Merge branch '5745-add-rest-api-client-in-argonaut-common' into '1.4-dev'

Resolve "Add REST API client in argonaut-common"

See merge request argonaut/argonaut!119
parents d2bfd021 065c2c1f
...@@ -40,6 +40,7 @@ create_perl_lint_rapport: ...@@ -40,6 +40,7 @@ create_perl_lint_rapport:
- apt-get install -y -qq debmirror libwww-perl - apt-get install -y -qq debmirror libwww-perl
- apt-get install -y -qq libmail-sendmail-perl - apt-get install -y -qq libmail-sendmail-perl
- apt-get install -y -qq libfile-slurp-perl libdigest-perl-md5-perl - apt-get install -y -qq libfile-slurp-perl libdigest-perl-md5-perl
- apt-get install -y -qq librest-client-perl
script: script:
- find . -type f -name '*.pm' -print0 | xargs -0 -n1 perl -cw -I argonaut-ldap2zone -I argonaut-common - find . -type f -name '*.pm' -print0 | xargs -0 -n1 perl -cw -I argonaut-ldap2zone -I argonaut-common
...@@ -95,7 +96,7 @@ build-release: ...@@ -95,7 +96,7 @@ build-release:
stage: tarballs stage: tarballs
only: only:
- tags - tags
script: script:
- tar -cvzf argonaut-$(grep '%' Changelog.md | head -n1 | cut -d ' ' -f3 | tr -d '"').tar.gz * - tar -cvzf argonaut-$(grep '%' Changelog.md | head -n1 | cut -d ' ' -f3 | tr -d '"').tar.gz *
artifacts: artifacts:
paths: paths:
......
...@@ -631,6 +631,10 @@ sub argonaut_read_config { ...@@ -631,6 +631,10 @@ sub argonaut_read_config {
$res{'ldap_dn'} = trim($config->val( ldap => "dn", "")); $res{'ldap_dn'} = trim($config->val( ldap => "dn", ""));
$res{'ldap_password'} = trim($config->val( ldap => "password", "")); $res{'ldap_password'} = trim($config->val( ldap => "password", ""));
$res{'ldap_tls'} = trim($config->val( ldap => "tls", "off")); $res{'ldap_tls'} = trim($config->val( ldap => "tls", "off"));
$res{'rest_endpoint'} = trim($config->val( rest => "endpoint", ""));
$res{'rest_ldap'} = trim($config->val( rest => "ldap", ""));
$res{'rest_login'} = trim($config->val( rest => "login", ""));
$res{'rest_password'} = trim($config->val( rest => "password", ""));
if ($res{'ldap_tls'} !~ m/^off|on$/i) { if ($res{'ldap_tls'} !~ m/^off|on$/i) {
warn "Unknown value for option ldap/tls: ".$res{'ldap_tls'}." (valid values are on/off)\n"; warn "Unknown value for option ldap/tls: ".$res{'ldap_tls'}." (valid values are on/off)\n";
......
#######################################################################
#
# Argonaut::Libraries::FusionDirectoryWebService -- Contact FusionDirectory REST API
#
# Copyright (C) 2018-2019 FusionDirectory project
#
# Author: Côme Chilliet
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
#######################################################################
package Argonaut::Libraries::FusionDirectoryWebService;
use strict;
use warnings;
use 5.008;
use REST::Client;
use JSON;
use Argonaut::Libraries::Common qw(:config);
use Exporter 'import'; # gives you Exporter's import() method directly
our @EXPORT_OK = qw(&argonaut_get_rest_client); # symbols to export on request
=item argonaut_get_rest_client
Get REST client connection using information from configuration file
=cut
sub argonaut_get_rest_client {
my $config = argonaut_read_config;
my $client = REST::Client->new();
$client->setHost($config->{'rest_endpoint'});
my %postBody = (
'user' => $config->{'rest_login'},
'password' => $config->{'rest_password'}
);
if ($config->{'rest_ldap'} ne '') {
$postBody{'ldap'} = $config->{'rest_ldap'};
}
$client->POST(
'/login',
encode_json(\%postBody)
);
if ($client->responseCode() eq '200') {
my $token = JSON->new->utf8->allow_nonref->decode($client->responseContent());
$client->addHeader('Session-Token', $token);
} else {
die('Connection to REST API failed: '.$client->responseCode().' '.$client->responseContent());
}
return $client;
}
1;
__END__
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment