File Coverage

blib/lib/WebService/IdoitAPI.pm
Criterion Covered Total %
statement 52 83 62.6
branch 18 36 50.0
condition n/a
subroutine 10 12 83.3
pod 5 5 100.0
total 85 136 62.5


line stmt bran cond sub pod time code
1             # vim: set sw=4 ts=4 et si ai:
2             #
3             package WebService::IdoitAPI;
4              
5 3     3   161456 use 5.006;
  3         23  
6 3     3   32 use strict;
  3         8  
  3         76  
7 3     3   16 use warnings;
  3         6  
  3         97  
8              
9 3     3   16 use Carp;
  3         6  
  3         221  
10 3     3   1422 use JSON::RPC::Legacy::Client;
  3         196242  
  3         2605  
11              
12             our $VERSION = 'v0.1.3';
13              
14             my @CONFIG_VARS = qw(apikey password url username);
15              
16             sub new {
17 5     5 1 2003975 my ($class,$config) = @_;
18 5         147 my $self = {
19             config => {},
20             version => '2.0',
21             };
22              
23 5         53 bless($self, $class);
24 5 50       79 if (defined $config) {
25 5         65 for my $cv (@CONFIG_VARS) {
26 20 100       170 if (exists $config->{$cv}) {
27 6         95 $self->{config}->{$cv} = $config->{$cv};
28             }
29             }
30 5         28 $self->_test_minimum_config();
31             }
32 2         14 return $self;
33             } # new()
34              
35             sub DESTROY {
36 5     5   4167 my $self = shift;
37              
38 5 50       15 if ($self->is_logged_in()) {
39 0         0 $self->logout();
40             }
41 5         37 return undef;
42             } # DESTROY()
43              
44             sub request {
45 1     1 1 458 my ($self,$request) = @_;
46 1 50       10 if (defined $request) {
47 1         5 my $client;
48 1 50       42 if (exists $self->{client}) {
49 0         0 $client = $self->{client};
50             }
51             else {
52 1         260 $client = new JSON::RPC::Legacy::Client;
53 1         10308 $self->{client} = $client;
54 1 50       7 if ($self->{session_id}) {
55 0         0 $client->{ua}->default_header('X-RPC-Auth-Session' => $self->{session_id});
56             }
57             else {
58 1 50       21 if (defined $self->{config}->{password}) {
59 0         0 $client->{ua}->default_header( 'X-RPC-Auth-Password' => $self->{config}->{password} );
60             }
61 1 50       11 if (defined $self->{config}->{username}) {
62 0         0 $client->{ua}->default_header( 'X-RPC-Auth-Username' => $self->{config}->{username} );
63             }
64             }
65             }
66             $request->{version} = "2.0"
67 1 50       23 unless (defined $request->{version});
68             $request->{id} = 1
69 1 50       7 unless (defined $request->{id});
70             $request->{params}->{language} = 'en'
71 1 50       4 unless (defined $request->{params}->{language});
72 1         2 $request->{params}->{apikey} = $self->{config}->{apikey};
73              
74 1         2 my $res = do {
75 1         2 local $@;
76 1         4 my $ret;
77 1         12 eval { $ret = $client->call($self->{config}->{url},$request); 1};
  1         7  
  0         0  
78 1 50       17615 if ( $@ ) {
79 1         3 my $status_line = $self->{client}->{status_line};
80 1 50       4 if ( $status_line !~ /^2[0-9]{2} / ) {
81 1         9 die "Connection problem: $status_line";
82             }
83 0         0 die "JSON RPC client failed: $@";
84             }
85 0         0 $ret;
86             };
87 0         0 return $res;
88             }
89 0         0 return undef;
90             } # request()
91              
92             sub login {
93 0     0 1 0 my ($self,$user,$pass) = @_;
94              
95 0 0       0 $user = $self->{config}->{username} unless ($user);
96 0 0       0 $pass = $self->{config}->{password} unless ($pass);
97              
98 0         0 my $client = new JSON::RPC::Legacy::Client;
99 0         0 $client->{ua}->default_header( 'X-RPC-Auth-Password' => $pass );
100 0         0 $client->{ua}->default_header( 'X-RPC-Auth-Username' => $user );
101 0         0 $self->{client} = $client;
102              
103 0         0 my $res = $self->request( { method => 'idoit.login' } );
104 0 0       0 if ($res->{is_success}) {
105 0         0 my $h = $self->{client}->{ua}->default_headers();
106 0         0 $h->header('X-RPC-Auth-Session' => $res->{content}->{result}->{'session-id'});
107 0         0 $h->remove_header('X-RPC-Auth-Username');
108 0         0 $h->remove_header('X-RPC-Auth-Password');
109 0         0 $self->{session_id} = $res->{content}->{result}->{'session-id'};
110 0         0 return $res;
111             }
112 0         0 return undef;
113             } # login()
114              
115             sub logout {
116 0     0 1 0 my $self = shift;
117              
118 0         0 my $res = $self->request( { method => 'idoit.login' } );
119 0         0 delete $self->{session_id};
120 0         0 delete $self->{client}; # grab a fresh client next time
121 0         0 return $res;
122             } # logout()
123              
124             sub is_logged_in {
125 5     5 1 18 return exists $_[0]->{session_id};
126             } # is_logged_in()
127              
128             sub _test_minimum_config {
129 5     5   33 my $self = shift;
130             croak "configuration is missing the API key"
131 5 100       109 unless ( $self->{config}->{apikey} );
132             croak "configuration is missing the URL for the API"
133 3 100       40 unless ( $self->{config}->{url} );
134             } # _test_minimum_config()
135              
136             1; # End of WebService::IdoitAPI
137              
138             __DATA__