Hetula-Client
view release on metacpan or search on metacpan
lib/Hetula/Client.pm view on Meta::CPAN
package Hetula::Client;
use Modern::Perl '2015';
our $VERSION = '0.008';
# ABSTRACT: Interface with Hetula
#
# Copyright 2018 National Library of Finland
=encoding utf8
=head1 NAME
Hetula::Client - Perl client implementation to communicate with Hetula.
=head1 DESCRIPTION
Perl client implementation to communicate with Hetula, the Patron data store
=head1 SYNOPSIS
my $hc = Hetula::Client->new({baseURL => 'https://hetula.example.com'});
my $loginResponse = $hc->login({username => 'master', password => 'blaster', organization => 'Administratoria'});
die($loginResponse->{error}) if ($loginResponse->{error});
my $loginActiveResp = $hc->loginActive();
ok(! $loginActiveResp->{error}, "Login active");
my $ssnAddResp = $hc->ssnAdd({ssn => 'bad-ssn'});
ok($ssnAddResp->{error}, "SSN add failed - Bad SSN '$ssnAddResp->{error}'");
my $ssnGetResp = $hc->ssnGet({id => 1});
ok(! $ssnGetResp->{error}, "SSN got");
my $ssnsBatchAddResp = $hc->ssnsBatchAdd(['101010-101A', '101010-102B']);
is(@$ssnsBatchAddResp, 2, "SSNs batch add");
=cut
##Pragmas
use Modern::Perl;
use feature qw(signatures);
no warnings qw(experimental::signatures);
use Carp::Always;
use autodie;
use English; #Use verbose alternatives for perl's strange $0 and $\ etc.
##External modules
use Mojo::UserAgent;
use Storable;
use Regexp::Common;
use Data::Printer;
=head3 new
@param1 {HASHRef} baseURL => https://hetula.example.com
credentials => filepath, Where to load the credentials file.
see slurpCredentials() for more info.
=cut
sub new($class, $params) {
slurpCredentials($params->{credentials}, $params) if ($params->{credentials});
_detectKohaEnvironment($params);
die("Hetula::Client::BadParam - parameter 'baseURL' is missing") unless $params->{baseURL};
die("Hetula::Client::BadParam - parameter 'baseURL' '$params->{baseURL}' is not a valid URI") unless $params->{baseURL} =~ /$RE{URI}{HTTP}{-scheme=>qr!https?!}/;
my $s = bless(Storable::dclone($params), $class);
$s->{ua} = Mojo::UserAgent->new() unless $s->{ua};
return $s;
}
=head2 API Access methods
=head3 login
See Hetula API doc for endpoint POST /api/v1/auth
@param1 {HASHRef} username => String || undef if given via credentials during construction,
password => String || undef if given via credentials during construction,
organization => String || undef if given via credentials during construction,
=cut
sub login($s, $params={}) {
$params->{username} = $s->{username} unless $params->{username};
$params->{password} = $s->{password} unless $params->{password};
$params->{organization} = $s->{organization} unless $params->{organization};
my $tx = $s->ua->post( $s->baseURL().'/api/v1/auth', {Accept => '*/*'}, json => $params );
my $json = _handleResponse($tx);
return $json if $json->{error};
my $cookies = $tx->res->cookies;
my $sessionCookie = $cookies->[0];
$s->ua->cookie_jar->add($sessionCookie);
my $csrfHeader = $tx->res->headers->header('X-CSRF-Token');
$s->ua->on(start => sub {
( run in 1.869 second using v1.01-cache-2.11-cpan-437f7b0c052 )