blib/lib/BZ/Client/XMLRPC.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/BZ/Client/XMLRPC/Parser.pm ↑ Summary
1 #!/bin/false
2 # PODNAME: BZ::Client::XMLRPC
3 # ABSTRACT: Performs XML-RPC calls on behalf of the client.
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package BZ::Client::XMLRPC;
9 $BZ::Client::XMLRPC::VERSION = '4.4001';
10  
11 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use BZ::Client::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use BZ::Client::XMLRPC::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
18 use DateTime::Format::Strptime;
19 use DateTime::TimeZone;
20  
21  
22 my $counter;
23 my $fmt = DateTime::Format::Strptime->new(
24 pattern => '%C%Y-%m-%dT%T',
25 time_zone => 'UTC' );
26 my $tz = DateTime::TimeZone->new( name => 'UTC' );
27  
28  
29 sub new {
30 my $class = shift;
31 my $self = { @_ };
32 bless($self, ref($class) || $class);
33 return $self
34 }
35  
36 sub url {
37 my $self = shift;
38 if (@_) {
39 $self->{'url'} = shift;
40 }
41 else {
42 return $self->{'url'};
43 }
44 }
45  
46 sub web_agent {
47 my $self = shift;
48 if (@_) {
49 $self->{'web_agent'} = shift;
50 }
51 else {
52 my $wa = $self->{'web_agent'};
53 my $connect = $self->{'connect'} || {};
54 $self->error(q/'connect' parameter in new() not a hashref/)
55 unless ref $connect eq 'HASH';
56 if (!defined($wa)) {
57 $wa = HTTP::Tiny->new(
58 %$connect,
59 agent => sprintf('BZ::Client::XMLRPC %s (perl %s; %s)', $BZ::Client::XMLRPC::VERSION, $^V, $^O)
60 );
61 $self->web_agent($wa);
62 }
63 return $wa;
64 }
65 }
66  
67 sub error {
68 my($self, $message, $http_code, $xmlrpc_code) = @_;
69 BZ::Client::Exception->throw('message' => $message,
70 'http_code' => $http_code,
71 'xmlrpc_code' => $xmlrpc_code)
72 }
73  
74 {
75  
76 my %actions = (
77  
78 'HASH' => sub {
79 my($self, $writer, $value) = @_;
80 $writer->startTag('value');
81 $writer->startTag('struct');
82 for my $key (sort keys %$value) {
83 $writer->startTag('member');
84 $writer->startTag('name');
85 $writer->characters($key);
86 $writer->endTag('name');
87 $self->value($writer, $value->{$key});
88 $writer->endTag('member');
89 }
90 $writer->endTag('struct');
91 $writer->endTag('value');
92 },
93  
94 'ARRAY' => sub {
95 my($self, $writer, $value) = @_;
96 $writer->startTag('value');
97 $writer->startTag('array');
98 $writer->startTag('data');
99 for my $val (@$value) {
100 $self->value($writer, $val);
101 }
102 $writer->endTag('data');
103 $writer->endTag('array');
104 $writer->endTag('value');
105 },
106  
107 'BZ::Client::XMLRPC::int' => sub {
108 my($self, $writer, $value) = @_;
109 $writer->startTag('value');
110 $writer->startTag('i4');
111 $writer->characters($$value);
112 $writer->endTag('i4');
113 $writer->endTag('value');
114 },
115  
116 'BZ::Client::XMLRPC::boolean' => sub {
117 my($self, $writer, $value) = @_;
118 $writer->startTag('value');
119 $writer->startTag('boolean');
120 $writer->characters($$value ? '1' : '0');
121 $writer->endTag('boolean');
122 $writer->endTag('value');
123 },
124  
125 'BZ::Client::XMLRPC::double' => sub {
126 my($self, $writer, $value) = @_;
127 $writer->startTag('value');
128 $writer->startTag('double');
129 $writer->characters($$value);
130 $writer->endTag('double');
131 $writer->endTag('value');
132 },
133  
134 'DateTime' => sub {
135 my($self, $writer, $value) = @_;
136 my $clone = $value->clone();
137 $clone->set_time_zone($tz);
138 $clone->set_formatter($fmt);
139 $writer->startTag('value');
140 $writer->startTag('dateTime.iso8601');
141 $writer->characters($clone->iso8601(). 'Z');
142 $writer->endTag('dateTime.iso8601');
143 $writer->endTag('value');
144 },
145  
146 );
147  
148 sub value {
149 my($self, $writer, $value) = @_;
150  
151 if ($actions{ ref($value) }) {
152 $actions{ ref($value) }->($self, $writer, $value);
153 }
154 else {
155 $writer->startTag('value');
156 $writer->characters($value);
157 $writer->endTag('value');
158 }
159 }
160  
161 }
162  
163 sub create_request {
164 my($self, $methodName, $params) = @_;
165 my $contents;
166 my $writer = XML::Writer->new(
167 OUTPUT => \$contents,
168 ENCODING => 'UTF-8' );
169 $writer->startTag('methodCall');
170 $writer->startTag('methodName');
171 $writer->characters($methodName);
172 $writer->endTag('methodName');
173 $writer->startTag('params');
174 for my $param (@$params) {
175 $writer->startTag('param');
176 $self->value($writer, $param);
177 $writer->endTag('param');
178 }
179 $writer->endTag('params');
180 $writer->endTag('methodCall');
181 $writer->end();
182 return encode('utf8', $contents)
183 }
184  
185 sub get_response {
186 my($self, $contents) = @_;
187 return _get_response($self,
188 { 'url' => $self->url() . '/xmlrpc.cgi',
189 'contentType' => 'text/xml',
190 'contents' => encode_utf8($contents) })
191 }
192  
193 sub _get_response {
194 my($self, $params) = @_;
195 my $url = $params->{'url'};
196 my $contentType = $params->{'contentType'};
197 my $contents = $params->{'contents'};
198 if (ref($contents) eq 'ARRAY') {
199 my $uri = URI->new('http:');
200 $uri->query_form($contents);
201 $contents = $uri->query();
202 }
203  
204 my %options = (
205 headers => {
206 'Content-Type' => $contentType,
207 },
208 content => $contents, # carefull of the s
209 );
210  
211 my $wa = $self->web_agent();
212  
213 my($logDir,$logId) = $self->logDirectory();
214  
215 if ($logDir) {
216 $logId = ++$counter;
217 my $fileName = File::Spec->catfile($logDir, "$$.$logId.request.log");
218 if (open(my $fh, '>', $fileName)) {
219 while (my($header,$value) = each %{$options{headers}} ) {
220 print $fh "$header: $value\n";
221 }
222 print $fh 'user-agent: ', $wa->agent(), "\n";
223 if ($wa->{cookie_jar}) {
224 print $fh join("\n", $wa->{cookie_jar}->dump_cookies());
225 }
226 print $fh "\n";
227 print $fh $contents;
228 close($fh);
229 }
230 }
231  
232 my $res = $wa->request(POST => $url, \%options);
233 my $response = $res->{success} ? $res->{content} : undef;
234 if ($logDir) {
235 my $fileName = File::Spec->catfile($logDir, "$$.$logId.response.log");
236 if (open(my $fh, '>', $fileName)) {
237 for my $header (sort keys %{$res->{headers}}) {
238 my $value = $res->{headers}->{$header};
239 if (ref $value) {
240 print $fh "$header: $_\n" for @$value;
241 }
242 else {
243 print $fh "$header: $value\n";
244 }
245 }
246 print $fh "\n";
247 if ($res->{success}) {
248 print $fh $response;
249 }
250 close($fh);
251 }
252 }
253 if (!$res->{success}) {
254 my $code = $res->{status};
255 if ($code == 401) {
256 $self->error('Authorization error, perhaps invalid user name and/or password', $code);
257 }
258 elsif ($code == 404) {
259 $self->error('Bugzilla server not found, perhaps invalid URL.', $code);
260 }
261 else {
262 my $msg = $res->{reason};
263 $msg .= ' : ' . $res->{content} if $res->{content};
264 $self->error("Unknown error: $msg", $code);
265 }
266 }
267  
268 return $response
269 }
270  
271 sub parse_response {
272 my($self, $contents) = @_;
273 my $parser = BZ::Client::XMLRPC::Parser->new();
274 return $parser->parse($contents)
275 }
276  
277 sub request {
278 my $self = shift;
279 my %args = @_;
280 my $methodName = $args{'methodName'};
281 $self->error('Missing argument: methodName') unless defined($methodName);
282 my $params = $args{'params'};
283 $self->error('Missing argument: params') unless defined($params);
284 $self->error('Invalid argument: params (Expected array)') unless ref($params) eq 'ARRAY';
285 my $contents = $self->create_request($methodName, $params);
286 $self->log('debug', "BZ::Client::XMLRPC::request: Sending method $methodName to " . $self->url());
287 my $response = $self->get_response($contents);
288 $self->log('debug', "BZ::Client::XMLRPC::request: Got result for method $methodName");
289 return $self->parse_response($response)
290 }
291  
292 sub log {
293 my($self, $level, $msg) = @_;
294 my $logger = $self->logger();
295 if ($logger) {
296 &$logger($level, $msg);
297 }
298 }
299  
300 sub logger {
301 my($self) = shift;
302 if (@_) {
303 $self->{'logger'} = shift;
304 }
305 else {
306 return $self->{'logger'};
307 }
308 }
309  
310 sub logDirectory {
311 my($self) = shift;
312 if (@_) {
313 $self->{'logDirectory'} = shift;
314 }
315 else {
316 return $self->{'logDirectory'};
317 }
318 }
319  
320 package BZ::Client::XMLRPC::int;
321 $BZ::Client::XMLRPC::int::VERSION = '4.4001';
322 sub new {
323 my($class, $value) = @_;
324 return bless(\$value, (ref($class) || $class))
325 }
326  
327 package BZ::Client::XMLRPC::boolean;
328 $BZ::Client::XMLRPC::boolean::VERSION = '4.4001';
329 sub new {
330 my($class, $value) = @_;
331 return bless(\$value, (ref($class) || $class))
332 }
333  
334 {
335  
336 my $true = BZ::Client::XMLRPC::boolean->new(1);
337 my $false = BZ::Client::XMLRPC::boolean->new(0);
338  
339 sub TRUE { $true }
340 sub FALSE { $false }
341  
342 }
343  
344 package BZ::Client::XMLRPC::double;
345 $BZ::Client::XMLRPC::double::VERSION = '4.4001';
346 sub new {
347 my($class, $value) = @_;
348 return bless(\$value, (ref($class) || $class))
349 }
350  
351 1;
352  
353 __END__
354  
355 =pod
356  
357 =encoding UTF-8
358  
359 =head1 NAME
360  
361 BZ::Client::XMLRPC - Performs XML-RPC calls on behalf of the client.
362  
363 =head1 VERSION
364  
365 version 4.4001
366  
367 =head1 SYNOPSIS
368  
369 my $xmlrpc = BZ::Client::XMLRPC->new( url => $url );
370 my $result = $xmlrpc->request( methodName => $methodName, params => $params );
371  
372 An instance of BZ::Client::XMLRPC is able to perform XML-RPC calls against the
373 given URL. A request is performed by passing the method name and the method
374 parameters to the method L</request>. The request result is returned.
375  
376 =head1 CLASS METHODS
377  
378 This section lists the possible class methods.
379  
380 =head2 new
381  
382 my $xmlrpc = BZ::Client::XMLRPC->new( url => $url );
383  
384 Creates a new instance with the given URL.
385  
386 =head1 INSTANCE METHODS
387  
388 This section lists the possible instance methods.
389  
390 =head2 url
391  
392 my $url = $xmlrpc->url();
393 $xmlrpc->url( $url );
394  
395 Returns or sets the XML-RPC servers URL.
396  
397 =head2 request
398  
399 my $result = $xmlrpc->request( methodName => $methodName, params => $params );
400  
401 Calls the XML-RPC servers method C<$methodCall>, passing the parameters given by
402 C<$params>, an array of parameters. Parameters may be hash refs, array refs, or
403 atomic values. Array refs and hash refs may recursively contain array or hash
404 refs as values. An instance of L<BZ::Client::Exception> is thrown in case of
405 errors.
406  
407 =head1 SEE ALSO
408  
409 L<BZ::Client>, L<BZ::Client::Exception>
410  
411 =head1 AUTHORS
412  
413 =over 4
414  
415 =item *
416  
417 Dean Hamstead <dean@bytefoundry.com.au>
418  
419 =item *
420  
421 Jochen Wiedmann <jochen.wiedmann@gmail.com>
422  
423 =back
424  
425 =head1 COPYRIGHT AND LICENSE
426  
427 This software is copyright (c) 2017 by Dean Hamstad.
428  
429 This is free software; you can redistribute it and/or modify it under
430 the same terms as the Perl 5 programming language system itself.
431  
432 =cut
« blib/lib/BZ/Client/XMLRPC/Parser.pm ↑ Summary