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   160893 use 5.006;
  3         20  
6 3     3   18 use strict;
  3         5  
  3         70  
7 3     3   15 use warnings;
  3         4  
  3         109  
8              
9 3     3   18 use Carp;
  3         5  
  3         202  
10 3     3   1492 use JSON::RPC::Legacy::Client;
  3         195493  
  3         2529  
11              
12             our $VERSION = 'v0.1.2';
13              
14             my @CONFIG_VARS = qw(apikey password url username);
15              
16             sub new {
17 5     5 1 2003840 my ($class,$config) = @_;
18 5         173 my $self = {
19             config => {},
20             version => '2.0',
21             };
22              
23 5         42 bless($self, $class);
24 5 50       93 if (defined $config) {
25 5         54 for my $cv (@CONFIG_VARS) {
26 20 100       113 if (exists $config->{$cv}) {
27 6         77 $self->{config}->{$cv} = $config->{$cv};
28             }
29             }
30 5         56 $self->_test_minimum_config();
31             }
32 2         42 return $self;
33             } # new()
34              
35             sub DESTROY {
36 5     5   4113 my $self = shift;
37              
38 5 50       17 if ($self->is_logged_in()) {
39 0         0 $self->logout();
40             }
41 5         34 return undef;
42             } # DESTROY()
43              
44             sub request {
45 1     1 1 612 my ($self,$request) = @_;
46 1 50       43 if (defined $request) {
47 1         20 my $client;
48 1 50       39 if (exists $self->{client}) {
49 0         0 $client = $self->{client};
50             }
51             else {
52 1         236 $client = new JSON::RPC::Legacy::Client;
53 1         8598 $self->{client} = $client;
54 1 50       8 if ($self->{session_id}) {
55 0         0 $client->{ua}->default_header('X-RPC-Auth-Session' => $self->{session_id});
56             }
57             else {
58 1 50       35 if (defined $self->{config}->{password}) {
59 0         0 $client->{ua}->default_header( 'X-RPC-Auth-Password' => $self->{config}->{password} );
60             }
61 1 50       10 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       21 unless (defined $request->{version});
68             $request->{id} = 1
69 1 50       8 unless (defined $request->{id});
70             $request->{params}->{language} = 'en'
71 1 50       7 unless (defined $request->{params}->{language});
72 1         4 $request->{params}->{apikey} = $self->{config}->{apikey};
73              
74 1         4 my $res = do {
75 1         4 local $@;
76 1         2 my $ret;
77 1         3 eval { $ret = $client->call($self->{config}->{url},$request); 1};
  1         7  
  0         0  
78 1 50       18550 if ( $@ ) {
79 1         4 my $status_line = $self->{client}->{status_line};
80 1 50       4 if ( $status_line !~ /^2[0-9]{2} / ) {
81 1         10 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 17 return exists $_[0]->{session_id};
126             } # is_logged_in()
127              
128             sub _test_minimum_config {
129 5     5   30 my $self = shift;
130             croak "configuration is missing the API key"
131 5 100       64 unless ( $self->{config}->{apikey} );
132             croak "configuration is missing the URL for the API"
133 3 100       43 unless ( $self->{config}->{url} );
134             } # _test_minimum_config()
135              
136             1; # End of WebService::IdoitAPI
137              
138             __DATA__