blib/lib/Hetula/Client.pm untested

filter: Statementstmt 0.0% 0 / 180 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 44 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 350 CC 40 SCAR 35.9
CC
40
Cov
0%
slurpCredentials
90.0
ssnsBatchAddFromFile
72.0
_detectKohaEnvironment
72.0
CRAP 36.2
1 package Hetula::Client;
2  
3 our $VERSION = '0.007';
4  
5 # ABSTRACT: Interface with Hetula
6 #
7 # Copyright 2018 National Library of Finland
8  
9 =encoding utf8
10  
11 =head1 NAME
12  
13 Hetula::Client - Perl client implementation to communicate with Hetula.
14  
15 =head1 DESCRIPTION
16  
17 Perl client implementation to communicate with Hetula, the Patron data store
18  
19 =head1 SYNOPSIS
20  
21 my $hc = Hetula::Client->new({baseURL => 'https://hetula.example.com'});
22  
23 my $loginResponse = $hc->login({username => 'master', password => 'blaster', organization => 'Administratoria'});
24 die($loginResponse->{error}) if ($loginResponse->{error});
25  
26 my $loginActiveResp = $hc->loginActive();
27 ok(! $loginActiveResp->{error}, "Login active");
28  
29 my $ssnAddResp = $hc->ssnAdd({ssn => 'bad-ssn'});
30 ok($ssnAddResp->{error}, "SSN add failed - Bad SSN '$ssnAddResp->{error}'");
31  
32 my $ssnGetResp = $hc->ssnGet({id => 1});
33 ok(! $ssnGetResp->{error}, "SSN got");
34  
35 my $ssnsBatchAddResp = $hc->ssnsBatchAdd(['101010-101A', '101010-102B']);
36 is(@$ssnsBatchAddResp, 2, "SSNs batch add");
37  
38 =cut
39  
40 ##Pragmas
41 use Modern::Perl;
42 use feature qw(signatures);
43 no warnings qw(experimental::signatures);
44 use Carp::Always;
45 use autodie;
46 use English; #Use verbose alternatives for perl's strange $0 and $\ etc.
47  
48 ##External modules
49 use Mojo::UserAgent;
50 use Storable;
51 use Regexp::Common;
52 use Data::Printer;
53  
54 =head3 new
55  
56 @param1 {HASHRef} baseURL => https://hetula.example.com
57 credentials => filepath, Where to load the credentials file.
58 see slurpCredentials() for more info.
59  
60 =cut
61  
62 sub new($class, $params) {
63 slurpCredentials($params->{credentials}, $params) if ($params->{credentials});
64 _detectKohaEnvironment($params);
65 die("Hetula::Client::BadParam - parameter 'baseURL' is missing") unless $params->{baseURL};
66 die("Hetula::Client::BadParam - parameter 'baseURL' '$params->{baseURL}' is not a valid URI") unless $params->{baseURL} =~ /$RE{URI}{HTTP}{-scheme=>qr!https?!}/;
67  
68 my $s = bless(Storable::dclone($params), $class);
69  
70 $s->{ua} = Mojo::UserAgent->new() unless $s->{ua};
71 return $s;
72 }
73  
74 =head2 API Access methods
75  
76 =head3 login
77  
78 See Hetula API doc for endpoint POST /api/v1/auth
79  
80 @param1 {HASHRef} username => String || undef if given via credentials during construction,
81 password => String || undef if given via credentials during construction,
82 organization => String || undef if given via credentials during construction,
83  
84 =cut
85  
86 sub login($s, $params={}) {
87 $params->{username} = $s->{username} unless $params->{username};
88 $params->{password} = $s->{password} unless $params->{password};
89 $params->{organization} = $s->{organization} unless $params->{organization};
90  
91 my $tx = $s->ua->post( $s->baseURL().'/api/v1/auth', {Accept => '*/*'}, json => $params );
92 my $json = _handleResponse($tx);
93 return $json if $json->{error};
94  
95 my $cookies = $tx->res->cookies;
96 my $sessionCookie = $cookies->[0];
97 $s->ua->cookie_jar->add($sessionCookie);
98  
99 my $csrfHeader = $tx->res->headers->header('X-CSRF-Token');
100  
101 $s->ua->on(start => sub {
102 my ($ua, $tx) = @_;
103 $tx->req->headers->header('X-CSRF-Token' => $csrfHeader);
104 });
105  
106 return $json;
107 }
108  
109 =head3 loginActive
110  
111 =cut
112  
113 sub loginActive($s) {
114 my $tx = $s->ua->get( $s->baseURL().'/api/v1/auth' );
115 return _handleResponse($tx);
116 }
117  
118 =head3 ssnAdd
119  
120 See Hetula API doc for endpoint POST /api/v1/ssns
121  
122 =cut
123  
124 sub ssnAdd($s, $params) {
125 my $tx = $s->ua->post( $s->baseURL().'/api/v1/ssns', {Accept => '*/*'}, json => $params );
126 return _handleResponse($tx);
127 }
128  
129 =head3 ssnGet
130  
131 See Hetula API doc for endpoint GET /api/v1/users/<id>
132  
133 @param1 {HASHRef} id => ssn id to get
134  
135 =cut
136  
137 sub ssnGet($s, $params) {
138 die("Hetula::Client::BadParameter - parameter 'id' is not an integer") unless $params->{id} =~ /$RE{num}{int}/;
139 my $tx = $s->ua->get( $s->baseURL().'/api/v1/ssns/'.$params->{id} );
140 return _handleResponse($tx);
141 }
142  
143 =head3 ssnsBatchAdd
144  
145 See Hetula API doc for endpoint GET /api/v1/ssns/batch
146  
147 @param1 {ARRAYRef} of ssns
148  
149 =cut
150  
151 sub ssnsBatchAdd($s, $ssnArray) {
152 my $tx = $s->ua->post( $s->baseURL().'/api/v1/ssns/batch', {Accept => '*/*'}, json => $ssnArray );
153 return _handleResponse($tx);
154 }
155  
156 =head2 ssnsBatchAddChunked
157  
158 Invokes the ssnsBatchAdd()-method repeatedly in small chunks. Useful for
159 importing an inconveniently large amount of ssns that would otherwise timeout
160 the Hetula-server.
161  
162 @param1 {sub} Receives a feeder callback, which sends ssn-lists to the
163 ssnsBatchAdd()-method.
164 for ex.
165 sub {
166 #Keep sending ssns while there are ssns to send
167 return ['ssn1','ssn2','ssn3'] if @ssns;
168 #When ssns run out, return false to signal the end of transmission
169 return undef || [];
170 }
171  
172 @param2 {sub} Receives a digester callback, which receives the ssnsBatchAdd()-methods
173 response|return value.
174 for ex.
175 sub {
176 my ($ssnReportsFromHetula) = @_;
177 print $FH_OUT "$_->{ssn}->{id},$_->{ssn}->{ssn},$_->{error}\n" for @$ssnReportsFromHetula;
178 }
179  
180 =cut
181  
182 sub ssnsBatchAddChunked($s, $feederCallback, $digesterCallback) {
183 while (my $ssns = $feederCallback->()) {
184 last unless($ssns && @$ssns);
185 $digesterCallback->($s->ssnsBatchAdd($ssns))
186 }
187 }
188  
189 =head2 ssnsBatchAddFromFile
190  
191 Wrapper for ssnsBatchAddChunked(), where this manages the file IO as well.
192  
193 @param1 {filepath} Where to read ssns from.
194 This can be a simple .csv-file, in this case the last (or only)
195 column is expected to be one containing the ssn that is
196 intended to be migrated to Hetula.
197 If there are any extra columns, they are appended to the
198 ssn report/result .csv-file as ssn report context.
199 @param2 {filepath} Where to write the ssn migration results/reports
200  
201 =cut
202  
203 sub ssnsBatchAddFromFile($s, $filenameIn, $filenameOut, $batchSize=500) {
204 open(my $FH_IN, "<:encoding(UTF-8)", $filenameIn) or die("Hetula::Client::File - Opening the given file '$filenameIn' for reading ssns failed: $!\n");
205 open(my $FH_OUT, ">:encoding(UTF-8)", $filenameOut) or die("Hetula::Client::File - Opening the given file '$filenameOut' for writing ssns results failed: $!\n");
206  
207 print $FH_OUT "ssnId,ssn,error,context\n";
208  
209 my $i = 0; #Keep track of how many ssns are processed.
210 my $retry = 0;
211 my @ssns;
212 my @context;
213 my $feeder = sub { #Feeds ssns to the batch grinder
214 if ($retry == 0) { #Do not reset the input if retrying
215 @ssns = ();
216 @context = ();
217 while (<$FH_IN>) {
218 $i++;
219 chomp;
220 my @cols = split(',', $_);
221 push(@ssns, pop(@cols)); #The last value is expected to be the ssn
222 push(@context, \@cols); #always push the context, even if cols is empty. This makes sure the order of contexts is preserved!
223 last if @ssns >= $batchSize;
224 }
225 }
226 if (@ssns) {
227 print __PACKAGE__."::ssnsBatchAddFromFile() :> '$i' ssns fed.\n";
228 }
229 else {
230 print __PACKAGE__."::ssnsBatchAddFromFile() :> All '$i' ssns fed.\n";
231 }
232  
233 return \@ssns;
234 };
235 my $digester = sub { #digests ssn reports from Hetula
236 my ($ssnReports) = @_;
237  
238 if (ref($ssnReports) ne 'ARRAY') { #There is something wrong!
239 Data::Printer::p($ssnReports);
240 $retry++;
241  
242 sleep($ENV{MOCK_BAD_CONNECTION}||10); #Wait a bit, maybe the pipi goes away.
243 $ENV{MOCK_BAD_CONNECTION_RETRIES} = $retry if $ENV{MOCK_BAD_CONNECTION}; #Awkwardly mix test hooks here, sorry about that.
244  
245 return if $retry <= 3;
246 die("Hetula::Client::Connection - Retried '".($retry-1)."' times, but still cannot succeed. Sorry... Exception from Hetula: ".Data::Printer::np($ssnReports));
247 }
248 $retry = 0; #Presumably we have succeeded in something here.
249  
250 for (my $i=0 ; $i<@$ssnReports ; $i++) {
251 my $res = $ssnReports->[$i];
252 my $ssn = $ssns[$i];
253  
254 die("Hetula::Client::SSN - Local ssns and Hetula ssns are out of sync at batch file row='$i', local ssn='$ssn', Hetula ssn='$res->{ssn}->{ssn}'?") unless ($res->{ssn}->{ssn} eq $ssn);
255  
256 print $FH_OUT join(",", $res->{ssn}->{id}//'', $ssn, $res->{error}//'',
257 @{$context[$i]} #Add what is left of the given file columns as a context for the ssn report file. This makes it easier for possible next processing steps in the migration pipeline.
258 )."\n";
259 }
260 print __PACKAGE__."::ssnsBatchAddFromFile() :> '$i' reports digested.\n";
261 };
262 $s->ssnsBatchAddChunked($feeder, $digester);
263 }
264  
265 =head3 userAdd
266  
267 See Hetula API doc for endpoint POST /api/v1/users
268  
269 =cut
270  
271 sub userAdd($s, $params) {
272 my $tx = $s->ua->post( $s->baseURL().'/api/v1/users', {Accept => '*/*'}, json => $params );
273 return _handleResponse($tx);
274 }
275  
276 =head3 userBasicAdd
277  
278 Adds a user with only the most minimum permisions needed to push records into Hetula.
279 Organization the user belongs to is implied from the currently logged in organization.
280  
281 @param {HASHRef} username => 'MattiM',
282 password => 'Secret',
283 realname => 'Matti Meikäläinen',
284  
285 =cut
286  
287 sub userBasicAdd($s, $params) {
288 $params->{permissions} = [
289 'ssns-post',
290 'auth-get',
291 ];
292 return $s->userAdd($params);
293 }
294  
295 =head3 userReadAdd
296  
297 Adds a user with read access to Hetula.
298 Organization the user belongs to is implied from the currently logged in organization.
299  
300 @param {HASHRef} username => 'MattiM',
301 password => 'Secret',
302 realname => 'Matti Meikäläinen',
303  
304 =cut
305  
306 sub userReadAdd($s, $params) {
307 $params->{permissions} = [
308 'ssns-post',
309 'ssns-id-get',
310 'auth-get',
311 ];
312 return $s->userAdd($params);
313 }
314  
315 =head3 userMod
316  
317 See Hetula API doc for endpoint PUT /api/v1/users/<id>
318  
319 @param {HASHRef} username or id => mandatory,
320 other patron attributes => and values,
321 ...
322  
323 =cut
324  
325 sub userMod($s, $params) {
326 my $id = $params->{id} || $params->{username};
327 die("Hetula::Client::BadParameter - parameter 'id' or 'username' is missing") unless ($id);
328 my $tx = $s->ua->put( $s->baseURL()."/api/v1/users/$id", {Accept => '*/*'}, json => $params );
329 return _handleResponse($tx);
330 }
331  
332 =head3 userChangePassword
333  
334 @param {HASHRef} username or id => mandatory,
335 password => mandatory - the new password,
336  
337 =cut
338  
339 sub userChangePassword($s, $params) {
340 my $id = $params->{id} || $params->{username};
341 die("Hetula::Client::BadParameter - parameter 'id' or 'username' is missing") unless ($id);
342 die("Hetula::Client::BadParameter - parameter 'password' is missing") unless $params->{password};
343 my $tx = $s->ua->put( $s->baseURL()."/api/v1/users/$id/password", {Accept => '*/*'}, json => $params );
344 return _handleResponse($tx);
345 }
346  
347 =head3 userDisableAccount
348  
349 To recover from a disabled account, change the password
350  
351 @param {String} username or id
352  
353 =cut
354  
355 sub userDisableAccount($s, $params) {
356 my $id = $params->{id} || $params->{username};
357 die("Hetula::Client::BadParameter - parameter 'id' or 'username' is missing") unless ($id);
358 my $tx = $s->ua->delete( $s->baseURL()."/api/v1/users/$id/password", {Accept => '*/*'} );
359 return _handleResponse($tx);
360 }
361  
362 =head2 HELPERS
363  
364 =head3 slurpCredentials
365 @static
366  
367 Reads the contents of a credentials file.
368  
369 The credentials file must consist of up to 4 lines, with each line
370 specifying the following commandline argument replacements:
371 username
372 password
373 organization
374 url
375  
376 @param1 {String} Path to the credentials file
377 @param2 {HASHRef} Optional, HASHRef where to inject the found credentials
378  
379 =cut
380  
381 sub slurpCredentials($credentialsFile, $injectHere=undef) {
382 open(my $FH, '<:encoding(UTF-8)', $credentialsFile) or die("Couldn't read '$credentialsFile': $!");
383 my $username = <$FH>; if ($username) { chomp($username); $injectHere->{username} = $username if $username && $injectHere; }
384 my $password = <$FH>; if ($password) { chomp($password); $injectHere->{password} = $password if $password && $injectHere; }
385 my $organization = <$FH>; if ($organization) { chomp($organization); $injectHere->{organization} = $organization if $organization && $injectHere; }
386 my $baseURL = <$FH>; if ($baseURL) { chomp($baseURL); $injectHere->{baseURL} = $baseURL if $baseURL && $injectHere; }
387 return ($username, $password, $organization, $baseURL);
388 }
389  
390 =head2 ATTRIBUTES
391  
392 =head3 ua
393  
394 =cut
395  
396 sub ua { return $_[0]->{ua} }
397  
398 =head3 baseURL
399  
400 =cut
401  
402 sub baseURL { return $_[0]->{baseURL} }
403  
404 ################
405 #######################
406 ### Private methods ###
407 ####################
408  
409 sub _handleResponse($tx) {
410 if (my $res = $tx->success) {
411 if ($ENV{HETULA_DEBUG}) {
412 print "Request success:\n";
413 Data::Printer::p($res->json);
414 }
415 return $res->json || { OK => $tx->res->code };
416 }
417 else {
418 my $error = $tx->error;
419 $error->{error} = $tx->res->body || $error->{message} || $error->{code};
420 if ($ENV{HETULA_DEBUG}) {
421 print "Request error:\n";
422 Data::Printer::p($error);
423 }
424 return $error;
425 }
426 }
427  
428 ## @static
429 ## If you are using https://koha-community.org/
430 ## Hetula::Client tries to pick configurations from there automatically.
431 ##
432 sub _detectKohaEnvironment($params) {
433 eval "use C4::Context;"; #This way the Dist::Zilla ::Plugin::AutoPrereqs doesn't think this is a mandatory requirement
434 unless ($@) {
435 print "Koha detected. ";
436 if (my $hetulaConfig = C4::Context->config('hetula')) {
437 if (my $url = $hetulaConfig->{url}) {
438 $params->{baseURL} = $url unless $params->{baseURL};
439 print "Hetula baseURL found '$url'. ";
440 }
441 else {
442 die "KOHA_CONF: hetula->url is missing!" unless $url;
443 }
444  
445 if (my $org = $hetulaConfig->{organization}) {
446 $params->{organization} = $org unless $params->{organization};
447 print "Hetula organization found '$org'.\n";
448 }
449 else {
450 die "KOHA_CONF: hetula->organization is missing!";
451 }
452 }
453 else {
454 die("Koha detected, but 'hetula' is not configured in the \$KOHA_CONF='$ENV{KOHA_CONF}'");
455 }
456 };
457 }
458  
459 1;