File Coverage

blib/lib/Net/BaruwaAPI.pm
Criterion Covered Total %
statement 174 204 85.2
branch 0 12 0.0
condition 0 9 0.0
subroutine 58 59 98.3
pod 48 49 97.9
total 280 333 84.0


line stmt bran cond sub pod time code
1             # -*- coding: utf-8 -*-
2             # vim: ai ts=4 sts=4 et sw=4
3             # Net::BaruwaAPI Perl bindings for the Baruwa REST API
4             # Copyright (C) 2015-2016 Andrew Colin Kissa
5             #
6             # This Source Code Form is subject to the terms of the Mozilla Public
7             # License, v. 2.0. If a copy of the MPL was not distributed with this file,
8             # You can obtain one at http://mozilla.org/MPL/2.0/.
9             package Net::BaruwaAPI;
10              
11             # use utf8;
12 13     13   230075 use feature 'state';
  13         18  
  13         1035  
13 13     13   5422 use JSON::MaybeXS;
  13         81315  
  13         692  
14 13     13   5648 use HTTP::Request;
  13         208388  
  13         382  
15 13     13   72 use Carp qw/croak/;
  13         15  
  13         586  
16 13     13   7848 use LWP::UserAgent;
  13         206722  
  13         385  
17 13     13   5948 use Type::Params qw/compile/;
  13         772063  
  13         112  
18 13     13   2289 use Types::Standard qw(Str InstanceOf Object Int Bool Dict Num ArrayRef);
  13         21  
  13         50  
19 13     13   16820 use Moo;
  13         123487  
  13         80  
20              
21             our $VERSION = '0.04';
22             our $AUTHORITY = 'cpan:DATOPDOG';
23              
24             my $api_path = '/api/v1';
25              
26             has 'api_url' => (is => 'ro', isa => Str, predicate => 'has_api_url', required => 1);
27              
28             has 'api_token' => (is => 'ro', isa => Str, predicate => 'has_api_token', required => 1);
29              
30             has 'ua' => (
31             isa => InstanceOf['LWP::UserAgent'],
32             is => 'ro',
33             lazy => 1,
34             default => sub {
35             LWP::UserAgent->new(
36             agent => "BaruwaAPI-Perl",
37             cookie_jar => {},
38             keep_alive => 4,
39             timeout => 60,
40             );
41             },
42             );
43              
44             has 'json' => (
45             is => 'ro',
46             isa => Object,
47             lazy => 1,
48             default => sub {
49             return JSON::MaybeXS->new( utf8 => 1 );
50             }
51             );
52              
53             sub _call {
54 0     0   0 my ($self) = @_;
55 0         0 my $request_method = shift @_;
56 0         0 my $url = shift @_;
57 0         0 my $data = shift @_;
58              
59 0         0 my $ua = $self->ua;
60 0         0 $ua->default_header('Authorization', "Bearer " . $self->api_token);
61 0         0 $url = $self->api_url . $url;
62              
63 0         0 my $req = HTTP::Request->new( $request_method, $url );
64 0         0 $req->accept_decodable;
65              
66 0 0       0 if ($data) {
67 0         0 $req->content($data);
68             }
69 0         0 $req->header( 'Content-Length' => length $req->content );
70              
71 0         0 my $res = $ua->request($req);
72              
73 0 0 0     0 if ($res->header('Content-Type') and $res->header('Content-Type') =~ 'application/json') {
74 0         0 my $json = $res->decoded_content;
75 0         0 $data = eval { $self->json->decode($json) };
  0         0  
76 0 0       0 unless ($data) {
77 0 0       0 die unless $res->is_error;
78 0         0 $data = { code => $res->code, message => $res->message };
79             }
80             } else {
81 0         0 $data = { code => $res->code, message => $res->message };
82             }
83              
84 0 0 0     0 if (not $res->is_success and ref $data eq 'HASH' and exists $data->{message}) {
      0        
85 0         0 my $message = $data->{message};
86              
87 0 0       0 if (exists $data->{errors}) {
88 0         0 $message .= ': '.join(' - ', map { $_->{message} } grep { exists $_->{message} } @{ $data->{errors} });
  0         0  
  0         0  
  0         0  
89             }
90 0         0 croak $message;
91             }
92 0         0 return $data;
93             }
94              
95              
96             sub get_users {
97 1     1 1 1831 state $check = compile(Object);
98 1         504 my ($self) = $check->(@_);
99 1         13 return $self->_call('GET', "$api_path/users");
100             }
101              
102             sub get_user {
103 1     1 1 907 state $check = compile(Object, Int);
104 1         677 my ($self, $userid) = $check->(@_);
105 1         20 return $self->_call('GET', "$api_path/users/$userid");
106             }
107              
108             sub create_user {
109 1     1 1 3416 state $check = compile(Object,
110             Dict[
111             username => Str,
112             firstname => Str,
113             lastname => Str,
114             password1 => Str,
115             password2 => Str,
116             email => Str,
117             timezone => Str,
118             account_type => Int,
119             domains => Int,
120             active => Bool,
121             send_report => Bool,
122             spam_checks => Bool,
123             low_score => Num,
124             high_score => Num,
125             ]);
126 1         32756 my ($self, $data) = $check->(@_);
127 1         184 return $self->_call('POST', "$api_path/users", $data);
128             }
129              
130             sub update_user {
131 1     1 1 894 state $check = compile(Object,
132             Dict[
133             username => Str,
134             firstname => Str,
135             lastname => Str,
136             email => Str,
137             timezone => Str,
138             domains => Int,
139             active => Bool,
140             send_report => Bool,
141             spam_checks => Bool,
142             low_score => Num,
143             high_score => Num,
144             ]);
145 1         22888 my ($self, $data) = $check->(@_);
146             # my ($self, $data) = @_;
147 1         183 return $self->_call('PUT', "$api_path/users", $data);
148             }
149              
150             sub delete_user {
151 1     1 1 1107 state $check = compile(Object,
152             Dict[
153             username => Str,
154             firstname => Str,
155             lastname => Str,
156             email => Str,
157             timezone => Str,
158             domains => Int,
159             active => Bool,
160             send_report => Bool,
161             spam_checks => Bool,
162             low_score => Num,
163             high_score => Num,
164             ]);
165 1         22839 my ($self, $data) = $check->(@_);
166             # my ($self, $data) = @_;
167 1         187 return $self->_call('DELETE', "$api_path/users", $data);
168             }
169              
170             sub set_user_passwd {
171 1     1 1 1199 state $check = compile(Object, Int,
172             Dict[
173             password1 => Str,
174             password2 => Str,
175             ]);
176 1         5874 my ($self, $userid, $data) = $check->(@_);
177 1         102 return $self->_call('POST', "$api_path/users/chpw/$userid");
178             }
179              
180             sub get_aliases {
181 1     1 1 2689 state $check = compile(Object, Int);
182 1         733 my ($self, $addressid) = $check->(@_);
183 1         16 return $self->_call('GET', "$api_path/aliasaddresses/$addressid");
184             }
185              
186             sub create_alias {
187 1     1 1 765 state $check = compile(Object, Int,
188             Dict[
189             address => Str,
190             enabled => Bool,
191             ]);
192 1         6611 my ($self, $userid, $data) = $check->(@_);
193 1         85 return $self->_call('POST', "$api_path/aliasaddresses/$userid", $data);
194             }
195              
196             sub update_alias {
197 1     1 1 763 state $check = compile(Object, Int,
198             Dict[
199             address => Str,
200             enabled => Bool,
201             ]);
202 1         4081 my ($self, $addressid, $data) = $check->(@_);
203 1         80 return $self->_call('PUT', "$api_path/aliasaddresses/$addressid", $data);
204             }
205              
206             sub delete_alias {
207 1     1 1 758 state $check = compile(Object, Int,
208             Dict[
209             address => Str,
210             enabled => Bool,
211             ]);
212 1         4071 my ($self, $addressid, $data) = $check->(@_);
213 1         80 return $self->_call('DELETE', "$api_path/aliasaddresses/$addressid", $data);
214             }
215              
216             sub get_domains {
217 1     1 1 3060 state $check = compile(Object);
218 1         702 my ($self) = $check->(@_);
219 1         14 return $self->_call('GET', "$api_path/domains");
220             }
221              
222             sub get_domain {
223 1     1 1 754 state $check = compile(Object, Int);
224 1         743 my ($self, $domainid) = $check->(@_);
225 1         37 return $self->_call('GET', "$api_path/domains/$domainid");
226             }
227              
228             sub get_domain_by_name {
229 1     1 0 661 state $check = compile(Object, Str);
230 1         702 my ($self, $domain_name) = $check->(@_);
231 1         15 return $self->_call('GET', "$api_path/domains/byname/$domain_name");
232             }
233              
234             sub create_domain {
235 1     1 1 684 state $check = compile(Object,
236             Dict[
237             name => Str,
238             site_url => Str,
239             status => Bool,
240             smtp_callout => Bool,
241             ldap_callout => Bool,
242             virus_checks => Bool,
243             virus_checks_at_smtp => Bool,
244             spam_checks => Bool,
245             spam_actions => Num,
246             highspam_actions => Num,
247             virus_actions => Num,
248             low_score => Num,
249             high_score => Num,
250             message_size => Str,
251             delivery_mode => Num,
252             language => Str,
253             timezone => Str,
254             report_every => Num,
255             organizations => Num
256             ]);
257 1         45135 my ($self, $data) = $check->(@_);
258 1         259 return $self->_call('POST', "$api_path/domains", $data);
259             }
260              
261             sub update_domain {
262 1     1 1 805 state $check = compile(Object, Int,
263             Dict[
264             name => Str,
265             site_url => Str,
266             status => Bool,
267             smtp_callout => Bool,
268             ldap_callout => Bool,
269             virus_checks => Bool,
270             virus_checks_at_smtp => Bool,
271             spam_checks => Bool,
272             spam_actions => Num,
273             highspam_actions => Num,
274             virus_actions => Num,
275             low_score => Num,
276             high_score => Num,
277             message_size => Str,
278             delivery_mode => Num,
279             language => Str,
280             timezone => Str,
281             report_every => Num,
282             organizations => Num
283             ]);
284 1         41642 my ($self, $domainid, $data) = $check->(@_);
285 1         255 return $self->_call('PUT', "$api_path/domains/$domainid", $data);
286             }
287              
288             sub delete_domain {
289 1     1 1 722 state $check = compile(Object, Int);
290 1         777 my ($self, $domainid) = $check->(@_);
291 1         19 return $self->_call('DELETE', "$api_path/domains/$domainid");
292             }
293              
294             sub get_domainaliases {
295 1     1 1 2769 state $check = compile(Object, Int);
296 1         848 my ($self, $domainid) = $check->(@_);
297 1         20 return $self->_call('GET', "$api_path/domainaliases/$domainid");
298             }
299              
300             sub get_domainalias {
301 1     1 1 707 state $check = compile(Object, Int, Int);
302 1         895 my ($self, $domainid, $aliasid) = $check->(@_);
303 1         27 return $self->_call('GET', "$api_path/domainaliases/$domainid/$aliasid");
304             }
305              
306             sub create_domainalias {
307 1     1 1 624 state $check = compile(Object, Int,
308             Dict[
309             name => Str,
310             status => Bool,
311             domain => Int
312             ]);
313 1         11227 my ($self, $domainid, $data) = $check->(@_);
314 1         130 return $self->_call('POST', "$api_path/domainaliases/$domainid", $data);
315             }
316              
317             sub update_domainalias {
318 1     1 1 941 state $check = compile(Object, Int, Int,
319             Dict[
320             name => Str,
321             status => Bool,
322             domain => Int
323             ]);
324 1         8112 my ($self, $domainid, $aliasid, $data) = $check->(@_);
325 1         110 return $self->_call('PUT', "$api_path/domainaliases/$domainid/$aliasid", $data);
326             }
327              
328             sub delete_domainalias {
329 1     1 1 933 state $check = compile(Object, Int, Int,
330             Dict[
331             name => Str,
332             status => Bool,
333             domain => Int
334             ]);
335 1         8462 my ($self, $domainid, $aliasid, $data) = $check->(@_);
336 1         147 return $self->_call('DELETE', "$api_path/domainaliases/$domainid/$aliasid", $data);
337             }
338              
339             sub get_deliveryservers {
340 1     1 1 2557 state $check = compile(Object, Int);
341 1         761 my ($self, $domainid) = $check->(@_);
342 1         18 return $self->_call('GET', "$api_path/deliveryservers/$domainid");
343             }
344              
345             sub get_deliveryserver {
346 1     1 1 604 state $check = compile(Object, Int, Int);
347 1         801 my ($self, $domainid, $serverid) = $check->(@_);
348 1         20 return $self->_call('GET', "$api_path/deliveryservers/$domainid/$serverid");
349             }
350              
351             sub create_deliveryserver {
352 1     1 1 589 state $check = compile(Object, Int,
353             Dict[
354             address => Str,
355             protocol => Int,
356             port => Int,
357             enabled => Bool
358             ]);
359 1         11928 my ($self, $domainid, $data) = $check->(@_);
360 1         105 return $self->_call('POST', "$api_path/deliveryservers/$domainid", $data);
361             }
362              
363             sub update_deliveryserver {
364 1     1 1 624 state $check = compile(Object, Int, Int,
365             Dict[
366             address => Str,
367             protocol => Int,
368             port => Int,
369             enabled => Bool
370             ]);
371 1         9427 my ($self, $domainid, $serverid, $data) = $check->(@_);
372 1         98 return $self->_call('PUT', "$api_path/deliveryservers/$domainid/$serverid", $data);
373             }
374              
375             sub delete_deliveryserver {
376 1     1 1 610 state $check = compile(Object, Int, Int,
377             Dict[
378             address => Str,
379             protocol => Int,
380             port => Int,
381             enabled => Bool
382             ]);
383 1         9398 my ($self, $domainid, $serverid, $data) = $check->(@_);
384 1         99 return $self->_call('DELETE', "$api_path/deliveryservers/$domainid/$serverid", $data);
385             }
386              
387             sub get_authservers {
388 1     1 1 2452 state $check = compile(Object, Int);
389 1         665 my ($self, $domainid) = $check->(@_);
390 1         15 return $self->_call('GET', "$api_path/authservers/$domainid");
391             }
392              
393             sub get_authserver {
394 1     1 1 754 state $check = compile(Object, Int, Int);
395 1         762 my ($self, $domainid, $serverid) = $check->(@_);
396 1         20 return $self->_call('GET', "$api_path/authservers/$domainid/$serverid");
397             }
398              
399             sub create_authserver {
400 1     1 1 749 state $check = compile(Object, Int,
401             Dict[
402             address => Str,
403             protocol => Int,
404             port => Int,
405             enabled => Bool,
406             split_address => Bool,
407             user_map_template => Str
408             ]);
409 1         13723 my ($self, $domainid, $data) = $check->(@_);
410 1         108 return $self->_call('POST', "$api_path/authservers/$domainid", $data);
411             }
412              
413             sub update_authserver {
414 1     1 1 810 state $check = compile(Object, Int, Int,
415             Dict[
416             address => Str,
417             protocol => Int,
418             port => Int,
419             enabled => Bool,
420             split_address => Bool,
421             user_map_template => Str
422             ]);
423 1         11434 my ($self, $domainid, $serverid, $data) = $check->(@_);
424 1         103 return $self->_call('PUT', "$api_path/authservers/$domainid/$serverid", $data);
425             }
426              
427             sub delete_authserver {
428 1     1 1 810 state $check = compile(Object, Int, Int,
429             Dict[
430             address => Str,
431             protocol => Int,
432             port => Int,
433             enabled => Bool,
434             split_address => Bool,
435             user_map_template => Str
436             ]);
437 1         11546 my ($self, $domainid, $serverid, $data) = $check->(@_);
438 1         107 return $self->_call('DELETE', "$api_path/authservers/$domainid/$serverid", $data);
439             }
440              
441             sub get_ldapsettings {
442 1     1 1 3320 state $check = compile(Object, Int, Int, Int);
443 1         1764 my ($self, $domainid, $serverid, $settingsid) = $check->(@_);
444 1         28 return $self->_call('GET', "$api_path/ldapsettings/$domainid/$serverid/$settingsid");
445             }
446              
447             sub create_ldapsettings {
448 1     1 1 983 state $check = compile(Object, Int, Int,
449             Dict[
450             basedn => Str,
451             nameattribute => Str,
452             emailattribute => Str,
453             binddn => Str,
454             bindpw => Str,
455             usetls => Bool,
456             search_scope => Str,
457             emailsearch_scope => Str
458             ]);
459 1         18955 my ($self, $domainid, $serverid, $data) = $check->(@_);
460 1         144 return $self->_call('POST', "$api_path/ldapsettings/$domainid/$serverid", $data);
461             }
462              
463             sub update_ldapsettings {
464 1     1 1 981 state $check = compile(Object, Int, Int, Int,
465             Dict[
466             basedn => Str,
467             nameattribute => Str,
468             emailattribute => Str,
469             binddn => Str,
470             bindpw => Str,
471             usetls => Bool,
472             search_scope => Str,
473             emailsearch_scope => Str
474             ]);
475 1         16269 my ($self, $domainid, $serverid, $settingsid, $data) = $check->(@_);
476 1         141 return $self->_call('PUT', "$api_path/ldapsettings/$domainid/$serverid/$settingsid", $data);
477             }
478              
479             sub delete_ldapsettings {
480 1     1 1 962 state $check = compile(Object, Int, Int, Int,
481             Dict[
482             basedn => Str,
483             nameattribute => Str,
484             emailattribute => Str,
485             binddn => Str,
486             bindpw => Str,
487             usetls => Bool,
488             search_scope => Str,
489             emailsearch_scope => Str
490             ]);
491 1         15930 my ($self, $domainid, $serverid, $settingsid, $data) = $check->(@_);
492 1         150 return $self->_call('DELETE', "$api_path/ldapsettings/$domainid/$serverid/$settingsid", $data);
493             }
494              
495             sub get_radiussettings {
496 1     1 1 3007 state $check = compile(Object, Int, Int, Int);
497 1         1292 my ($self, $domainid, $serverid, $settingsid) = $check->(@_);
498 1         28 return $self->_call('GET', "$api_path/radiussettings/$domainid/$serverid/$settingsid");
499             }
500              
501             sub create_radiussettings {
502 1     1 1 715 state $check = compile(Object, Int, Int,
503             Dict[
504             secret => Str,
505             timeout => Int
506             ]);
507 1         10534 my ($self, $domainid, $serverid, $data) = $check->(@_);
508 1         125 return $self->_call('POST', "$api_path/radiussettings/$domainid/$serverid", $data);
509             }
510              
511             sub update_radiussettings {
512 1     1 1 734 state $check = compile(Object, Int, Int, Int,
513             Dict[
514             secret => Str,
515             timeout => Int
516             ]);
517 1         7351 my ($self, $domainid, $serverid, $settingsid, $data) = $check->(@_);
518 1         111 return $self->_call('PUT', "$api_path/radiussettings/$domainid/$serverid/$settingsid", $data);
519             }
520              
521             sub delete_radiussettings {
522 1     1 1 710 state $check = compile(Object, Int, Int, Int,
523             Dict[
524             secret => Str,
525             timeout => Int
526             ]);
527 1         7377 my ($self, $domainid, $serverid, $settingsid, $data) = $check->(@_);
528 1         111 return $self->_call('DELETE', "$api_path/radiussettings/$domainid/$serverid/$settingsid", $data);
529             }
530              
531             sub get_organizations {
532 1     1 1 720 state $check = compile(Object);
533 1         486 my ($self) = $check->(@_);
534 1         13 return $self->_call('GET', "$api_path/organizations");
535             }
536              
537             sub get_organization {
538 1     1 1 690 state $check = compile(Object, Int);
539 1         691 my ($self, $orgid) = $check->(@_);
540 1         19 return $self->_call('GET', "$api_path/organizations/$orgid");
541             }
542              
543             sub create_organization {
544 1     1 1 2947 state $check = compile(Object,
545             Dict[
546             name => Str,
547             domains => ArrayRef,
548             admins => ArrayRef
549             ]);
550 1         10338 my ($self, $data) = $check->(@_);
551 1         140 return $self->_call('POST', "$api_path/organizations", $data);
552             }
553              
554             sub update_organization {
555 1     1 1 689 state $check = compile(Object, Int,
556             Dict[
557             name => Str,
558             domains => ArrayRef,
559             admins => ArrayRef
560             ]);
561 1         7421 my ($self, $orgid, $data) = $check->(@_);
562 1         106 return $self->_call('PUT', "$api_path/organizations/$orgid", $data);
563             }
564              
565             sub delete_organization {
566 1     1 1 693 state $check = compile(Object, Int);
567 1         707 my ($self, $orgid) = $check->(@_);
568 1         19 return $self->_call('DELETE', "$api_path/organizations/$orgid");
569             }
570              
571             sub get_relay {
572 1     1 1 764 state $check = compile(Object, Int);
573 1         684 my ($self, $relayid) = $check->(@_);
574 1         21 return $self->_call('GET', "$api_path/relays/$relayid");
575             }
576              
577             sub create_relay {
578 1     1 1 2874 state $check = compile(Object, Int,
579             Dict[
580             address => Str,
581             enabled => Bool,
582             username => Str,
583             password1 => Str,
584             password2 => Str,
585             description => Str,
586             low_score => Num,
587             high_score => Num,
588             spam_actions => Int,
589             highspam_actions => Int,
590             ]);
591 1         27328 my ($self, $orgid, $data) = $check->(@_);
592 1         169 return $self->_call('POST', "$api_path/relays/$orgid", $data);
593             }
594              
595             sub update_relay {
596 1     1 1 670 state $check = compile(Object, Int,
597             Dict[
598             address => Str,
599             enabled => Bool,
600             username => Str,
601             password1 => Str,
602             password2 => Str,
603             description => Str,
604             low_score => Num,
605             high_score => Num,
606             spam_actions => Int,
607             highspam_actions => Int,
608             ]);
609 1         23754 my ($self, $relayid, $data) = $check->(@_);
610 1         159 return $self->_call('PUT', "$api_path/relays/$relayid", $data);
611             }
612              
613             sub delete_relay {
614 1     1 1 710 state $check = compile(Object, Int,
615             Dict[
616             address => Str,
617             enabled => Bool,
618             username => Str,
619             password1 => Str,
620             password2 => Str,
621             description => Str,
622             low_score => Num,
623             high_score => Num,
624             spam_actions => Int,
625             highspam_actions => Int,
626             ]);
627 1         23588 my ($self, $relayid, $data) = $check->(@_);
628 1         158 return $self->_call('DELETE', "$api_path/relays/$relayid", $data);
629             }
630              
631             sub get_status {
632 1     1 1 2746 state $check = compile(Object);
633 1         641 my ($self) = $check->(@_);
634 1         14 return $self->_call('GET', "$api_path/status");
635             }
636              
637 13     13   62663 no Moo;
  13         18  
  13         74  
638              
639             1;
640              
641             __END__