blib/lib/BZ/Client.pm untested

filter: Statementstmt 0.0% 0 / 138 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 40 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 279 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.9';
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__
285  
286 =pod
287  
288 =encoding UTF-8
289  
290 =head1 NAME
291  
292 BZ::Client - A client for the Bugzilla web services API.
293  
294 =head1 VERSION
295  
296 version 4.9
297  
298 =head1 SYNOPSIS
299  
300 my $client = BZ::Client->new( url => $url,
301 user => $user,
302 password => $password,
303 autologin => 0 );
304 $client->login();
305  
306 =head1 BUGZILLA VERSIONS
307  
308 Important: this L<BZ::Client> module supports the L<XMLRPC API|https://bugzilla.readthedocs.io/en/latest/integrating/api/Bugzilla/WebService/Server/XMLRPC.html> available in Bugzilla 5.0 and earlier, it is deprecated in newer versions and will be removed in a future version of Bugzilla, but may still work.
309  
310 For Bugzilla 5.1 and later, there is a new L<WebService API|https://bugzilla.readthedocs.io/en/latest/api/index.html#apis>, please see L<WebService::Bugzilla>.
311  
312 I welcome all patches (via pull request) for functionality relating to L<'maintained'|https://www.bugzilla.org/releases/> Bugzilla versions with XMLRPC support.
313  
314 Please upgrade your server and duplicate the problem, or see the above commitment to accept patches to fix for older versions.
315  
316 =head1 CLASS METHODS
317  
318 This section lists the class methods of BZ::Client.
319  
320 =head2 new
321  
322 my $client = BZ::Client->new( url => $url,
323 user => $user,
324 password => $password );
325  
326 my $client = BZ::Client->new( url => $url,
327 api_key => $api_key );
328  
329 The new method constructs a new instance of BZ::Client. Whenever you
330 want to connect to the Bugzilla server, you must first create a
331 Bugzilla client. The methods input is a hash of parameters.
332  
333 For debugging, you can pass in a subref named C<logger> which will be
334 fed debugging information as the client works. Also the C<logDirectory>
335 option is a directory where the raw http content will be dumped.
336  
337 =head3 Parameters
338  
339 =over
340  
341 =item url
342  
343 The Bugzilla servers URL, for example C<https://bugzilla.mozilla.org/>.
344  
345 =item api_key
346  
347 API keys were introduced in 5.0.
348  
349 An API Key can be obtained in the web interface of your Bugzilla server,
350 with the following steps:
351  
352 =over 4
353  
354 =item 1. Click 'Preferences' link
355  
356 =item 2. Click 'API Keys' tab
357  
358 =item 3. Click the checkbox next to 'Generate a new API key...'
359  
360 =item 4. If you like, add some description in the textbox
361  
362 =item 5. Click the 'Submit Changes' button
363  
364 =item 6. Key appears in the table under the 'Existing API keys' subheading
365  
366 =back
367  
368 =item user
369  
370 The user name to use when logging in to the Bugzilla server. Typically,
371 this will be your email address.
372  
373 =item password
374  
375 The password to use when logging in to the Bugzilla server.
376  
377 =item autologin
378  
379 If set to C<1> (true), will try to log in (if not already logged in) when
380 the first API call is made. This is default.
381  
382 If set to C<0>, will try APi calls without logging in. You can
383 still call $client->login() to log in manually.
384  
385 Note: once you're logged in, you'll stay that way until you call L</logout>
386  
387 =item restrictlogin
388  
389 If set to C<1> (true), will ask Bugzilla to restrict logins to your IP only.
390 Generally this is a good idea, but may caused problems if you are using
391 a loadbalanced forward proxy.
392  
393 Default: C<0>
394  
395 =item connect
396  
397 A hashref with options for L<HTTP::Tiny>, this is passed through so the
398 bellow are for reference only:
399  
400 =over 4
401  
402 =item http_proxy, https_proxy, proxy
403  
404 Nominates a proxy for HTTP, HTTPS or both, respectively.
405  
406 You might also use C<$ENV{all_proxy}>, C<$ENV{http_proxy}>, C<$ENV{https_proxy}>
407 or C<$ENV{all_proxy}>.
408  
409 =item timeout
410  
411 Request timeout in seconds (default is C<60>)
412  
413 =item verify_SSL
414  
415 A boolean that indicates whether to validate the SSL certificate of an
416 "https" connection (default is false)
417  
418 =back
419  
420 =back
421  
422 =head3 Connect Via Socks Proxy
423  
424 Try something like:
425  
426 use HTTP::Tiny; # load this manually
427 use IO::Socket::Socks::Wrapper (
428 'HTTP::Tiny::Handle::connect()' => {
429 ProxyAddr => 'localhost',
430 ProxyPort => 1080,
431 SocksVersion => 4,
432 Timeout => 15
433 }
434 );
435 use BZ::Client ...etc
436  
437 =head1 INSTANCE METHODS
438  
439 This section lists the methods, which an instance of BZ::Client can
440 perform.
441  
442 =head2 url
443  
444 $url = $client->url();
445 $client->url( $url );
446  
447 Returns or sets the Bugzilla servers URL.
448  
449 =head2 user
450  
451 $user = $client->user();
452 $client->user( $user );
453  
454 Returns or sets the user name to use when logging in to the Bugzilla
455 server. Typically, this will be your email address.
456  
457 =head2 password
458  
459 $password = $client->password();
460 $client->password( $password );
461  
462 Returns or sets the password to use when logging in to the Bugzilla server.
463  
464 =head2 autologin
465  
466 If L<login> is automatically called, or not.
467  
468 =head2 login
469  
470 Used to login to the Bugzilla server. By default, there is no need to call
471 this method explicitly: It is done automatically, whenever required.
472  
473 If L<autologin> is set to C<0>, call this to log in.
474  
475 =head2 is_logged_in
476  
477 Returns C<1> if logged in, otherwise C<0>.
478  
479 =head2 logout
480  
481 Deletes local cookies and calls Bugzilla's logout function
482  
483 =head2 logger
484  
485 Sets or gets the logging function. Argument is a coderef. Returns C<undef> if none.
486  
487 $logger = $client->logger();
488  
489 $client->logger(
490 sub {
491 my ($level, $msg) = @_;
492 print STDERR "$level $message\n";
493 return 1
494 });
495  
496 Also can be set via L</new>, e.g.
497  
498 $client = BZ::Client->new( logger => sub { },
499 url => $url,
500 user => $user,
501 password => $password );
502  
503 =head2 log
504  
505 $client->log( $level, $message );
506  
507 Sends log messages to whatever is loaded via L</logger>.
508  
509 =head2 api_call
510  
511 $response = $client->api_call( $methodName, $params, $options );
512  
513 Used by subclasses of L<BZ::Client::API> to invoke methods of the Bugzilla
514 API. The method name and the hash ref of parameters are sent to the Bugzilla server.
515 The options hash ref adjusts the behaviour of this function when calls are made.
516  
517 The params and options hash refs are optional, although certain param values may be required
518 depending on the method name being called. This client library relies upon the Bugzilla
519 server to enforce any method parameter requirements, so insufficient requests will still be
520 sent to the server for it to then reject them.
521  
522 Returns a hash ref of named result objects.
523  
524 =head3 Options
525  
526 =over 4
527  
528 =item empty_response_ok
529  
530 With this set, an empty response is not considered an error. In this case, api_call returns 1 to indicate
531 success.
532  
533 =item cookies
534  
535 This is used internally to set cookies with the log in functions. Don't touch this.
536  
537 =back
538  
539 =head1 EXCEPTION HANDLING
540  
541 See L<BZ::Client::Exception>
542  
543 =head1 ERROR CODES
544  
545 =head2 300 (Invalid Username or Password)
546  
547 The username does not exist, or the password is wrong.
548  
549 =head2 301 (Login Disabled)
550  
551 The ability to login with this account has been disabled. A reason may be specified with the error.
552  
553 =head2 305 (New Password Required)
554  
555 The current password is correct, but the user is asked to change his password.
556  
557 =head2 50 (Param Required)
558  
559 A login or password parameter was not provided.
560  
561 =head1 TESTING
562  
563 Bugzilla maintains demos of all supported versions and trunk at L<https://landfill.bugzilla.org>
564  
565 You might consider using that for testing against live versions.
566  
567 =head1 SEE ALSO
568  
569 L<BZ::Client::Exception>
570  
571 =head1 AUTHORS
572  
573 =over 4
574  
575 =item *
576  
577 Dean Hamstead <dean@bytefoundry.com.au>
578  
579 =item *
580  
581 Jochen Wiedmann <jochen.wiedmann@gmail.com>
582  
583 =back
584  
585 =head1 COPYRIGHT AND LICENSE
586  
587 This software is copyright (c) 2026 by Dean Hamstad.
588  
589 This is free software; you can redistribute it and/or modify it under
590 the same terms as the Perl 5 programming language system itself.
591  
592 =cut