Client.pm untested

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 303 CC 33 SCAR 32.7
CC
33
Cov
0%
_api_call
90.0
login
30.0
logout
30.0
CRAP 26.3
1 #!/bin/false
2 # ABSTRACT: A client for the Bugzilla web services API.
3 # PODNAME: BZ::Client
4  
5 use strict;
6 use warnings 'all';
7  
8 package BZ::Client;
9 $BZ::Client::VERSION = '4.91';
10  
11 use BZ::Client::XMLRPC;
12 use BZ::Client::Exception;
13 use HTTP::CookieJar;
14  
15 sub new {
16 my $class = shift;
17 my $self = {@_};
18 bless( $self, ref($class) || $class );
19 return $self
20 }
21  
22 sub url {
23 my $self = shift;
24 if (@_) {
25 $self->{'url'} = shift;
26 }
27 else {
28 return $self->{'url'};
29 }
30 }
31  
32 sub api_key {
33 my $self = shift;
34 if (@_) {
35 $self->{'api_key'} = shift;
36 }
37 else {
38 return $self->{'api_key'};
39 }
40 }
41  
42 sub user {
43 my $self = shift;
44 if (@_) {
45 $self->{'user'} = shift;
46 }
47 else {
48 return $self->{'user'};
49 }
50 }
51  
52 sub password {
53 my $self = shift;
54 if (@_) {
55 $self->{'password'} = shift;
56 }
57 else {
58 return $self->{'password'};
59 }
60 }
61  
62 sub autologin {
63 my $self = shift;
64 if (@_) {
65 $self->{'autologin'} = shift;
66 }
67 else {
68 $self->{'autologin'} = 1
69 unless defined($self->{'autologin'});
70 return $self->{'autologin'};
71 }
72 }
73  
74 sub error {
75 my ( $self, $message, $http_code, $xmlrpc_code ) = @_;
76 BZ::Client::Exception->throw(
77 message => $message,
78 http_code => $http_code,
79 xmlrpc_code => $xmlrpc_code
80 );
81 }
82  
83 sub log {
84 my ( $self, $level, $msg ) = @_;
85 my $logger = $self->logger();
86 if ($logger) {
87 &$logger( $level, $msg );
88 }
89 }
90  
91 sub logger {
92 my $self = shift;
93 if (@_) {
94 my $logger = shift;
95 $self->error('Cannot set logger to non-coderef.')
96 unless ref $logger eq 'CODE';
97 $self->{'logger'} = $logger;
98 }
99 else {
100 return $self->{'logger'};
101 }
102 }
103  
104 sub logDirectory {
105 my $self = shift;
106 if (@_) {
107 $self->{'logDirectory'} = shift;
108 }
109 else {
110 return $self->{'logDirectory'};
111 }
112 }
113  
114 sub xmlrpc {
115 my $self = shift;
116 if (@_) {
117 $self->{'xmlrpc'} = shift;
118 }
119 else {
120 my $xmlrpc = $self->{'xmlrpc'};
121 if ( !$xmlrpc ) {
122 my $url = $self->url()
123 || $self->error('The Bugzilla servers URL is not set.');
124 $xmlrpc = BZ::Client::XMLRPC->new(
125 url => $url,
126 connect => $self->{'connect'} );
127 $xmlrpc->logDirectory( $self->logDirectory() );
128 $xmlrpc->logger( $self->logger() );
129 $self->xmlrpc($xmlrpc);
130 }
131 return $xmlrpc;
132 }
133 }
134  
135 sub login {
136 my $self = shift;
137  
138 if ($self->api_key()) {
139 $self->log( 'debug', 'BZ::Client::login, no need for User.login call when using api_key' );
140 return 1
141 }
142  
143 my $rl = BZ::Client::XMLRPC::boolean->new($self->{'restrictlogin'} ? 1 : 0);
144 my %params = (
145 'remember' => BZ::Client::XMLRPC::boolean->FALSE, # dropped in 4.4 as cookies no longer used
146 'restrictlogin' => $rl, # added in 3.6
147 'restrict_login' => $rl, # added in 4.4 for tokens
148 );
149  
150 # FIXME username and password can be provided to any function and it will be ok
151 my $user = $self->user()
152 or $self->error('The Bugzilla servers user name is not set.');
153 my $password = $self->password()
154 or $self->error('The Bugzilla servers password is not set.');
155  
156 $params{login} = $user;
157 $params{password} = $password;
158 $self->log( 'debug', 'BZ::Client::login, going to log in with username and password' );
159  
160 my $cookies = HTTP::CookieJar->new();
161 my $response = $self->_api_call( 'User.login', \%params, { cookies => $cookies } );
162 if ( not defined( $response->{'id'} )
163 or $response->{'id'} !~ m/^\d+$/s )
164 {
165 $self->error('Server did not return a valid user ID.');
166 }
167 $self->log( 'debug', 'BZ::Client::login, got ID ' . $response->{'id'} );
168 if ( my $token = $response->{'token'} ) { # for 4.4.3 onward
169 $self->{'token'} = $token;
170 $self->log( 'debug', 'BZ::Client::login, got token ' . $token );
171 }
172 else {
173 $self->{'cookies'} = $cookies;
174 }
175 return 1
176 }
177  
178 sub logout {
179 my $self = shift;
180 return 1 unless $self->is_logged_in;
181 my $response = 1; # if have not cookie or token, return 1
182 my $cookies = $self->{'cookies'};
183 my $token = $self->{'token'};
184 if ($cookies or $token) {
185 my %params;
186 $params{'token'} = $self->{'token'}
187 if $self->{'token'};
188 # Note: A good response from User.logout is empty so, empty_response_ok => 1
189 $response = $self->_api_call( 'User.logout', \%params, { empty_response_ok => 1 } );
190 $cookies->clear() if $cookies;
191 delete $self->{'token'};
192 delete $self->{'cookies'};
193 }
194 return $response
195 }
196  
197 sub is_logged_in {
198 my $self = shift;
199 return 1 if $self->{'cookies'};
200 return 1 if $self->{'token'};
201 return 1 if $self->{'api_key'};
202 return
203 }
204  
205 sub api_call {
206  
207 my ( $self, $methodName, $params, $options ) = @_;
208  
209 $params ||= {};
210 $options ||= {};
211  
212 if ( $self->autologin && not $self->is_logged_in() ) {
213 $self->login();
214 }
215  
216 return $self->_api_call( $methodName, $params, $options )
217 }
218  
219 sub _api_call {
220  
221 my ( $self, $methodName, $params, $options ) = @_;
222  
223 $self->log( 'debug',
224 "BZ::Client::_api_call, sending request for method $methodName to "
225 . $self->url() );
226  
227 my $xmlrpc = $self->xmlrpc();
228  
229 if ($options->{cookies}) {
230 $xmlrpc->web_agent->{cookie_jar} = $options->{cookies};
231 }
232  
233 $params->{Bugzilla_token} = $self->{'token'}
234 if ($self->{'token'}
235 and not $params->{token}
236 and not $params->{Bugzilla_token});
237  
238 $params->{Bugzilla_api_key} = $self->api_key()
239 if ($self->api_key()
240 and not $params->{Bugzilla_token}
241 and not $params->{Bugzilla_api_key});
242  
243 my $response =
244 $xmlrpc->request( 'methodName' => $methodName, params => [$params] );
245  
246 if ($options->{empty_response_ok}) {
247  
248 if ( ref $response and ref($response) ne 'HASH' ) {
249 $self->error("Invalid response from server: $response");
250 }
251  
252 if ($response) {
253 $self->log( 'debug',
254 "BZ::Client::_api_call, got response for method $methodName" );
255 }
256 else {
257 $self->log( 'debug',
258 "BZ::Client::_api_call, got empty response for method $methodName but thats OK" );
259 $response = 1
260 }
261  
262 }
263 else {
264  
265 if ( not $response ) {
266 $self->error('Empty response from server.');
267 }
268  
269 if ( ref($response) ne 'HASH' ) {
270 $self->error("Invalid response from server: $response");
271 }
272  
273 $self->log( 'debug',
274 "BZ::Client::_api_call, got response for method $methodName" );
275  
276 }
277  
278  
279 return $response
280 }
281  
282 1;
283  
284 __END__