File Coverage

blib/lib/ELab/Client.pm
Criterion Covered Total %
statement 24 135 17.7
branch 0 6 0.0
condition n/a
subroutine 8 42 19.0
pod 33 34 97.0
total 65 217 29.9


line stmt bran cond sub pod time code
1             package ELab::Client;
2             # ABSTRACT: Access the eLabFTW API with Perl
3             $ELab::Client::VERSION = '0.020';
4 1     1   2553 use strict;
  1         3  
  1         35  
5 1     1   5 use warnings;
  1         3  
  1         32  
6              
7 1     1   644 use Moose;
  1         520677  
  1         8  
8 1     1   8716 use MooseX::NonMoose;
  1         1317  
  1         5  
9 1     1   80788 use MooseX::Params::Validate;
  1         97373  
  1         10  
10 1     1   1548 use JSON;
  1         9300  
  1         12  
11 1     1   815 use HTTP::Request::Common qw '';
  1         24863  
  1         2098  
12              
13             extends 'REST::Client';
14              
15             has host => (
16             is => 'ro',
17             isa => 'Str',
18             required => 1,
19             );
20              
21             has token => (
22             is => 'ro',
23             isa => 'Str',
24             required => 1,
25             );
26              
27             has endpoint => (
28             is => 'ro',
29             isa => 'Str',
30             default => 'api/v1/'
31             );
32              
33              
34             sub get_backup_zip {
35 0     0 1   my $self = shift;
36 0           my $datespan = shift;
37 0           return $self->elab_get("backupzip/$datespan");
38             }
39              
40              
41              
42             sub get_items_types {
43 0     0 1   my $self = shift;
44 0           return decode_json $self->elab_get("items_types/");
45             }
46              
47              
48             sub get_item_types {
49 0     0 1   return get_items_types(@_);
50             }
51              
52              
53              
54             sub get_status {
55 0     0 1   my $self = shift;
56 0           return decode_json $self->elab_get("status/");
57             }
58              
59              
60             sub get_experiment_states {
61 0     0 1   return get_status(@_);
62             }
63              
64              
65              
66             sub add_link_to_experiment {
67 0     0 1   my $self = shift;
68 0           my $id = shift;
69 0           my (%args) = validated_hash(
70             \@_,
71             link => { isa => 'Str' },
72             );
73 0           return decode_json $self->elab_post("experiments/$id", $self->buildQuery(%args));
74             }
75              
76              
77              
78             sub add_link_to_item {
79 0     0 1   my $self = shift;
80 0           my $id = shift;
81 0           my (%args) = validated_hash(
82             \@_,
83             link => { isa => 'Str' },
84             );
85 0           return decode_json $self->elab_post("items/$id", $self->buildQuery(%args));
86             }
87              
88              
89              
90             sub add_tag_to_experiment {
91 0     0 1   my $self = shift;
92 0           my $id = shift;
93 0           my (%args) = validated_hash(
94             \@_,
95             tag => { isa => 'Str' },
96             );
97 0           return decode_json $self->elab_post("experiments/$id", $self->buildQuery(%args));
98             }
99              
100              
101              
102             sub add_tag_to_item {
103 0     0 1   my $self = shift;
104 0           my $id = shift;
105 0           my (%args) = validated_hash(
106             \@_,
107             tag => { isa => 'Str' },
108             );
109 0           return decode_json $self->elab_post("items/$id", $self->buildQuery(%args));
110             }
111              
112              
113              
114             sub create_item {
115 0     0 1   my $self = shift;
116 0           my $type = shift;
117 0           return decode_json $self->elab_post("items/$type");
118             }
119              
120              
121              
122             sub create_experiment {
123 0     0 1   my $self = shift;
124 0           return decode_json $self->elab_post("experiments");
125             }
126              
127              
128              
129             sub create_template {
130 0     0 1   my $self = shift;
131 0           return decode_json $self->elab_post("templates");
132             }
133              
134              
135              
136             sub get_upload {
137 0     0 1   my $self = shift;
138 0           my $id = shift;
139 0           return $self->elab_get("uploads/$id");
140             }
141              
142              
143              
144             sub get_bookable {
145 0     0 1   my $self = shift;
146 0           return decode_json $self->elab_get("bookable/");
147             }
148              
149              
150              
151             sub get_all_experiments {
152 0     0 1   my $self = shift;
153 0           my (%args) = validated_hash(
154             \@_,
155             limit => { isa => 'Int', default => 25 },
156             offset => { isa => 'Int', default => 0 },
157             );
158 0           return decode_json $self->elab_get("experiments/?".$self->buildQuery(%args));
159             }
160              
161              
162              
163             sub get_experiment {
164 0     0 1   my $self = shift;
165 0           my $id = shift;
166 0           return decode_json $self->elab_get("experiments/$id");
167             }
168              
169              
170              
171             sub get_all_items {
172 0     0 1   my $self = shift;
173 0           my (%args) = validated_hash(
174             \@_,
175             limit => { isa => 'Int', default => 25 },
176             offset => { isa => 'Int', default => 0 },
177             );
178 0           return decode_json $self->elab_get("items/".$self->buildQuery(%args));
179             }
180              
181              
182              
183             sub get_item {
184 0     0 1   my $self = shift;
185 0           my $id = shift;
186 0           return decode_json $self->elab_get("items/$id");
187             }
188              
189              
190              
191             sub get_tags {
192 0     0 1   my $self = shift;
193 0           return decode_json $self->elab_get("tags/");
194             }
195              
196              
197              
198             sub get_all_templates {
199 0     0 1   my $self = shift;
200 0           return decode_json $self->elab_get("templates/");
201             }
202              
203              
204              
205             sub get_template {
206 0     0 1   my $self = shift;
207 0           my $id = shift;
208 0           return decode_json $self->elab_get("templates/$id");
209             }
210              
211              
212              
213             sub post_experiment {
214 0     0 1   my $self = shift;
215 0           my $id = shift;
216 0           my (%args) = validated_hash(
217             \@_,
218             category => { isa => 'Str', optional => 1 },
219             title => { isa => 'Str', optional => 1 },
220             date => { isa => 'Str', optional => 1 },
221             body => { isa => 'Str', optional => 1 },
222             bodyappend => { isa => 'Str', optional => 1 },
223             );
224 0           return decode_json $self->elab_post("experiments/$id", $self->buildQuery(%args));
225             }
226              
227              
228              
229             sub post_item {
230 0     0 1   my $self = shift;
231 0           my $id = shift;
232 0           my (%args) = validated_hash(
233             \@_,
234             category => { isa => 'Str', optional => 1 },
235             title => { isa => 'Str', optional => 1 },
236             date => { isa => 'Str', optional => 1 },
237             body => { isa => 'Str', optional => 1 },
238             bodyappend => { isa => 'Str', optional => 1 },
239             );
240 0           return decode_json $self->elab_post("items/$id", $self->buildQuery(%args));
241             }
242              
243              
244              
245             sub post_template {
246 0     0 1   my $self = shift;
247 0           my $id = shift;
248 0           my (%args) = validated_hash(
249             \@_,
250             title => { isa => 'Str', optional => 1 },
251             date => { isa => 'Str', optional => 1 },
252             body => { isa => 'Str', optional => 1 },
253             );
254 0           return decode_json $self->elab_post("templates/$id", $self->buildQuery(%args));
255             }
256              
257              
258              
259             sub upload_to_experiment {
260 0     0 1   my $self = shift;
261 0           my $id = shift;
262 0           my (%args) = validated_hash(
263             \@_,
264             file => { isa => 'Str' },
265             );
266             my $request = HTTP::Request::Common::POST(
267             $self->host().$self->endpoint()."experiments/$id",
268             {
269 0           file => [ $args{file} ]
270             },
271             Content_Type => 'form-data',
272             Authorization => $self->token(),
273             );
274 0           return decode_json $self->getUseragent()->request($request)->decoded_content();
275             }
276              
277              
278              
279             sub upload_to_item {
280 0     0 1   my $self = shift;
281 0           my $id = shift;
282 0           my (%args) = validated_hash(
283             \@_,
284             file => { isa => 'Str' },
285             );
286             my $request = HTTP::Request::Common::POST(
287             $self->host().$self->endpoint()."items/$id",
288             {
289 0           file => [ $args{file} ]
290             },
291             Content_Type => 'form-data',
292             Authorization => $self->token(),
293             );
294 0           return decode_json $self->getUseragent()->request($request)->decoded_content();
295             }
296              
297              
298              
299             sub create_event {
300 0     0 1   my $self = shift;
301 0           my $id = shift;
302 0           my (%args) = validated_hash(
303             \@_,
304             start => { isa => 'Str' },
305             end => { isa => 'Str' },
306             title => { isa => 'Str' },
307             );
308 0           return decode_json $self->elab_post("events/$id", $self->buildQuery(%args));
309             }
310              
311              
312              
313             sub destroy_event {
314 0     0 1   my $self = shift;
315 0           my $id = shift;
316 0           return decode_json $self->elab_delete("events/$id");
317             }
318              
319              
320              
321             sub get_all_events {
322 0     0 1   my $self = shift;
323 0           return decode_json $self->elab_get("events/");
324             }
325              
326              
327              
328             sub get_event {
329 0     0 1   my $self = shift;
330 0           my $id = shift;
331 0           return decode_json $self->elab_get("events/$id");
332             }
333              
334              
335              
336              
337             sub BUILD {
338 0     0 0   my $self = shift;
339 0           my $args = shift;
340              
341 0           $self->addHeader('Authorization', $self->token());
342             }
343              
344              
345             sub elab_get {
346 0     0 1   my $self = shift;
347 0           my $url = shift;
348 0           my $result = $self->GET($self->endpoint().$url);
349 0 0         return undef unless $result->responseCode() eq '200';
350 0           return $result->responseContent();
351             }
352              
353              
354             sub elab_delete {
355 0     0 1   my $self = shift;
356 0           my $url = shift;
357 0           my $result = $self->DELETE($self->endpoint().$url);
358 0 0         return undef unless $result->responseCode() eq '200';
359 0           return $result->responseContent();
360             }
361              
362              
363             sub elab_post {
364 0     0 1   my $self = shift;
365 0           my $url = shift;
366 0           my $data = shift;
367 0           $data =~ s/^\?//; # buildQuery starts with "?" (makes no sense here)
368 0           my $headers = { 'Content-Type' => 'application/x-www-form-urlencoded' };
369 0           my $result = $self->POST($self->endpoint().$url, $data, $headers);
370 0 0         return undef unless $result->responseCode() eq '200';
371 0           return $result->responseContent();
372             }
373              
374              
375 1     1   33 no Moose;
  1         3  
  1         22  
376             __PACKAGE__->meta->make_immutable;
377              
378             1;
379              
380             __END__
381              
382             =pod
383              
384             =encoding UTF-8
385              
386             =head1 NAME
387              
388             ELab::Client - Access the eLabFTW API with Perl
389              
390             =head1 VERSION
391              
392             version 0.020
393              
394             =head1 SYNOPSYS
395              
396             use ELab::Client;
397              
398             my $elab = ELab::Client->new(
399             host => 'https://elab.somewhere.de/',
400             token => 'ae...d4',
401             );
402              
403             my $e = $elab->post_experiment(4,
404             title => "Replacement experiment title",
405             body => "Replacement body text"
406             );
407              
408             =head1 METHODS
409              
410             =head2 API interface
411              
412             This interface is intended to be compatible to the elabapy Python client.
413              
414             =head3 get_backup_zip($datespan)
415              
416             use File::Slurp;
417             write_file('backup.zip', get_backup_zip('20200101-20210101'));
418              
419             Generates a zip file with all experiments changed in a given time period.
420             The period is specified as FROM-TO in the format YYYYMMDD-YYYYMMDD.
421              
422             Requires sysadmin permissions.
423              
424             =head3 get_items_types()
425              
426             my $t = $elab->get_items_types();
427              
428             Returns a list of database item types with their type id's.
429             The return value is an array reference, with the array items being hash
430             references for each item type.
431              
432             =head3 get_item_types()
433              
434             Alias for get_items_types()
435              
436             =head3 get_status()
437              
438             my $s = $elab->get_status();
439              
440             Returns a list of possible experiment states.
441             The return value is an array reference, with the array items being hash
442             references for each status type.
443              
444             =head3 get_experiment_states()
445              
446             Alias for get_status()
447              
448             =head3 add_link_to_experiment($id, ...)
449              
450             my $result = add_link_to_experiment(2, link => 5)
451              
452             Adds to an experiment a link to a database item with given id.
453             Returns a hash reference with status information.
454              
455             =head3 add_link_to_item($id, ...)
456              
457             my $result = add_link_to_item(2, link => 5);
458              
459             Adds to a database item a link to another database item with given id.
460             Returns a hash reference with status information.
461              
462             =head3 add_tag_to_experiment($id, ...)
463              
464             my $result = add_tag_to_experiment(2, tag => "awesome");
465              
466             Adds to an experiment the given tag (a string).
467             Returns a hash reference with status information.
468              
469             =head3 add_tag_to_item($id, ...)
470              
471             my $result = add_tag_to_item(42, tag => "broken");
472              
473             Adds to a database item the given tag (a string).
474             Returns a hash reference with status information.
475              
476             =head3 create_item($type)
477              
478             my $e = $elab->create_item($type);
479              
480             Creates a new database item of type $type. The return value is a hash
481             reference with status information and the id of the new item.
482              
483             =head3 create_experiment()
484              
485             my $e = $elab->create_experiment();
486              
487             Creates a new experiment. The return value is a hash reference with status
488             information and the id of the new experiment.
489              
490             =head3 create_template()
491              
492             my $t = $elab->create_template();
493              
494             Creates a new template. The return value is a hash reference with status
495             information and the id of the new template.
496              
497             =head3 get_upload($id)
498              
499             my $data = $elab->get_upload($id);
500              
501             Get an uploaded file from its id.
502             The result is the raw binary data of the uploaded file.
503              
504             =head3 get_bookable()
505              
506             my $b = $elab->get_bookable();
507              
508             Returns a list of bookable items (i.e., equipment etc). The result is an
509             array reference.
510              
511             =head3 get_all_experiments(...)
512              
513             my $a = $elab->get_all_experiments(limit => 15, offset => 0);
514              
515             Lists the stored experiments, at most limit and starting at offset.
516             The return value is an array reference, where each element is a hash reference
517             describing an experiment (not fully, but abbreviated).
518              
519             =head3 get_experiment($id)
520              
521             my $e = $elab->get_experiment($id);
522              
523             Returns an experiment. The return value is a hash reference with the full
524             experiment information.
525              
526             =head3 get_all_items(...)
527              
528             my $a = $elab->get_all_items(limit => 25, offset => 0);
529              
530             Lists database items, with maximum number limit and starting at offset.
531             The return value is an array reference, where each element is a hash reference
532             corresponding to a database item.
533              
534             =head3 get_item($id)
535              
536             my $i = $elab->get_item($id);
537              
538             Returns a database item. The return value is a hash reference with the full
539             item information.
540              
541             =head3 get_tags()
542              
543             my $t = $elab->get_tags();
544              
545             Returns the tags that are in use.
546              
547             =head3 get_all_templates()
548              
549             my $t = $elab->get_all_templates();
550              
551             Lists the available templates.
552             The return value is an array reference, where each element is a hash reference
553             describing a template.
554              
555             =head3 get_template
556              
557             my $t = $elab->get_template($id);
558              
559             Returns a template. The return value is a hash reference with the template
560             information.
561              
562             =head3 post_experiment($id, ...)
563              
564             my $e = $elab->post_experiment(13,
565             title => "Updated experiment title",
566             body => "Updated experiment body text"
567             );
568              
569             Updates an experiment, overwriting previous values or (in the case of
570             'bodyappend') appending to the existing text. The following parameters can
571             be given:
572              
573             category The (id of the) experiment status
574             title The experiment title
575             date The date
576             body The experiment body text
577             bodyappend Text that is appended to the experiment body text
578              
579             =head3 post_item
580              
581             my $i = $elab->post_item(4,
582             title => "Database item",
583             body => "This is a piece of expensive equipment."
584             );
585              
586             Updates a database item, overwriting previous values or (in the case of
587             'bodyappend') appending to the existing text. The following parameters can
588             be given:
589              
590             category The (id of the) database item type
591             title The item title
592             date The date
593             body The item body text
594             bodyappend Text that is appended to the item body text
595              
596             =head3 post_template($id, ...)
597              
598             my $t = $elab->post_template(4,
599             title => "New template title",
600             body => "Lots of text"
601             );
602              
603             Updates a template, overwriting previous values. The following parameters can
604             be given:
605              
606             title The item title
607             date The date
608             body The item body text
609              
610             =head3 upload_to_experiment($id, ...)
611              
612             my $e = $elab->upload_to_experiment(13, file => "mauterndorf.jpg");
613              
614             Uploads a file given by its name and appends it to the specified experiment.
615             The return value is a hash reference with status information.
616              
617             =head3 upload_to_item
618              
619             my $e = $elab->upload_to_item(13, file => "mauterndorf.jpg");
620              
621             Uploads a file given by its name and appends it to the specified database item.
622             The return value is a hash reference with status information.
623              
624             =head3 create_event(...)
625              
626             my $e = $elab->create_event(
627             start => "2019-11-30T12:00:00",
628             end => "2019-11-30T14:00:00",
629             title => "Booked from API",
630             );
631              
632             Creates an event in the scheduler for a bookable item. The return value is
633             a hash reference with status information and the id of the generated event.
634              
635             =head3 destroy_event($id)
636              
637             my $e = $elab->destroy_event(1);
638              
639             Deletes the event with the given id.
640              
641             =head3 get_all_events()
642              
643             my $e = $elab->get_all_events();
644              
645             Returns a list reference with information on all events.
646              
647             =head3 get_event($id)
648              
649             my $e = $elab->get_event(1);
650              
651             Returns a hash reference with information on the event specified by id.
652              
653             =head2 Low-level methods
654              
655             =head3 elab_get($url)
656              
657             my $hashref = decode_json $self->elab_get("events/$id");
658              
659             Sends a GET requrest to the server, and returns the response as JSON.
660              
661             =head3 elab_delete($url)
662              
663             my $hashref = decode_json $self->elab_delete("events/$id");
664              
665             Sends a DELETE requrest to the server, and returns the response as JSON.
666              
667             =head3 elab_post($url, $data)
668              
669             my $hashref = decode_json $self->elab_post("events/$id", $self->buildQuery(%args));
670              
671             Sends a POST requrest to the server, with the posted data supplied as an
672             urlencoded string (starting with '?' for convenient use of buildQuery).
673             Returns the obtained data as JSON.
674              
675             =head1 AUTHOR
676              
677             Andreas K. Huettel <dilfridge@gentoo.org>
678              
679             =head1 COPYRIGHT AND LICENSE
680              
681             This software is copyright (c) 2021 by Andreas K. Huettel.
682              
683             This is free software; you can redistribute it and/or modify it under
684             the same terms as the Perl 5 programming language system itself.
685              
686             =cut