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
« 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 # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package BZ::Client::XMLRPC;
10 $BZ::Client::XMLRPC::VERSION = '4.91';
11  
12 1 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use BZ::Client::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 0 ▶use BZ::Client::XMLRPC::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
19 use DateTime::TimeZone;
20  
21 my $counter;
22 my $tz = DateTime::TimeZone->new( name => 'UTC' );
23  
24 sub new {
25 my $class = shift;
26 my $self = { @_ };
27 bless($self, ref($class) || $class);
28 return $self
29 }
30  
31 sub url {
32 my $self = shift;
33 if (@_) {
34 $self->{'url'} = shift;
35 }
36 else {
37 return $self->{'url'};
38 }
39 }
40  
41 sub web_agent {
42 my $self = shift;
43 if (@_) {
44 $self->{'web_agent'} = shift;
45 }
46 else {
47 my $wa = $self->{'web_agent'};
48 my $connect = $self->{'connect'} || {};
49 $self->error(q/'connect' parameter in new() not a hashref/)
50 unless ref $connect eq 'HASH';
51 if (!defined($wa)) {
52 $wa = HTTP::Tiny->new(
53 %{$connect},
54 agent => sprintf('BZ::Client::XMLRPC %s (perl %s; %s)',
55 $BZ::Client::XMLRPC::VERSION, $^V, $^O)
56 );
57 $self->web_agent($wa);
58 }
59 return $wa;
60 }
61 }
62  
63 sub error {
64 my($self, $message, $http_code, $xmlrpc_code) = @_;
65 BZ::Client::Exception->throw('message' => $message,
66 'http_code' => $http_code,
67 'xmlrpc_code' => $xmlrpc_code)
68 }
69  
70 {
71  
72 my %actions = (
73  
74 'HASH' => sub {
75 my($self, $writer, $value) = @_;
76 $writer->startTag('value');
77 $writer->startTag('struct');
78 for my $key (sort keys %{$value}) {
79 $writer->startTag('member');
80 $writer->startTag('name');
81 $writer->characters($key);
82 $writer->endTag('name');
83 $self->value($writer, $value->{$key});
84 $writer->endTag('member');
85 }
86 $writer->endTag('struct');
87 $writer->endTag('value');
88 },
89  
90 'ARRAY' => sub {
91 my($self, $writer, $value) = @_;
92 $writer->startTag('value');
93 $writer->startTag('array');
94 $writer->startTag('data');
95 for my $val (@$value) {
96 $self->value($writer, $val);
97 }
98 $writer->endTag('data');
99 $writer->endTag('array');
100 $writer->endTag('value');
101 },
102  
103 'BZ::Client::XMLRPC::int' => sub {
104 my($self, $writer, $value) = @_;
105 $writer->startTag('value');
106 $writer->startTag('i4');
107 $writer->characters( $value->stringify() );
108 $writer->endTag('i4');
109 $writer->endTag('value');
110 },
111  
112 'BZ::Client::XMLRPC::base64' => sub {
113 my($self, $writer, $value) = @_;
114 $writer->startTag('value');
115 $writer->startTag('base64');
116 $writer->characters( $value->base64() );
117 $writer->endTag('base64');
118 $writer->endTag('value');
119 },
120  
121 'BZ::Client::XMLRPC::boolean' => sub {
122 my($self, $writer, $value) = @_;
123 $writer->startTag('value');
124 $writer->startTag('boolean');
125 $writer->characters( $value->stringify() );
126 $writer->endTag('boolean');
127 $writer->endTag('value');
128 },
129  
130 'BZ::Client::XMLRPC::double' => sub {
131 my($self, $writer, $value) = @_;
132 $writer->startTag('value');
133 $writer->startTag('double');
134 $writer->characters( $value->stringify() );
135 $writer->endTag('double');
136 $writer->endTag('value');
137 },
138  
139 'DateTime' => sub {
140 my($self, $writer, $value) = @_;
141 my $clone = $value->clone();
142 $clone->set_time_zone($tz);
143 $writer->startTag('value');
144 $writer->startTag('dateTime.iso8601');
145 $writer->characters($clone->iso8601(). 'Z');
146 $writer->endTag('dateTime.iso8601');
147 $writer->endTag('value');
148 },
149  
150 );
151  
152 sub value {
153 my($self, $writer, $value) = @_;
154  
155 if ($actions{ ref($value) }) {
156 $actions{ ref($value) }->($self, $writer, $value);
157 }
158 else {
159 $writer->startTag('value');
160 $writer->characters($value);
161 $writer->endTag('value');
162 }
163 }
164  
165 }
166  
167 sub create_request {
168 my($self, $methodName, $params) = @_;
169 my $contents;
170 my $writer = XML::Writer->new(
171 OUTPUT => \$contents,
172 ENCODING => 'UTF-8' );
173 $writer->startTag('methodCall');
174 $writer->startTag('methodName');
175 $writer->characters($methodName);
176 $writer->endTag('methodName');
177 $writer->startTag('params');
178 for my $param (@$params) {
179 $writer->startTag('param');
180 $self->value($writer, $param);
181 $writer->endTag('param');
182 }
183 $writer->endTag('params');
184 $writer->endTag('methodCall');
185 $writer->end();
186 return encode_utf8($contents)
187 }
188  
189 sub get_response {
190 my($self, $contents) = @_;
191 return _get_response(
192 $self,
193 {
194 'url' => $self->url() . '/xmlrpc.cgi',
195 'contentType' => 'text/xml',
196 'contents' => encode_utf8($contents)
197 }
198 )
199 }
200  
201 sub _get_response {
202 my($self, $params) = @_;
203 my $url = $params->{'url'};
204 my $contentType = $params->{'contentType'};
205 my $contents = $params->{'contents'};
206 if (ref($contents) eq 'ARRAY') {
207 my $uri = URI->new('http:');
208 $uri->query_form($contents);
209 $contents = $uri->query();
210 }
211  
212 my %options = (
213 headers => {
214 'Content-Type' => $contentType,
215 },
216 content => $contents, # carefull of the s
217 );
218  
219 my $wa = $self->web_agent();
220  
221 my($logDir,$logId) = $self->logDirectory();
222  
223 if ($logDir) {
224 $logId = ++$counter;
225 my $fileName = File::Spec->catfile($logDir, "$$.$logId.request.log");
226 if (open(my $fh, '>', $fileName)) {
227 while (my($header,$value) = each %{$options{headers}} ) {
228 print $fh "$header: $value\n";
229 }
230 print $fh 'user-agent: ', $wa->agent(), "\n";
231 if ($wa->{cookie_jar}) {
232 print $fh join("\n", $wa->{cookie_jar}->dump_cookies());
233 }
234 print $fh "\n";
235 print $fh $contents;
236 close($fh);
237 }
238 }
239  
240 my $res = $wa->request(POST => $url, \%options);
241 my $response = $res->{success} ? $res->{content} : undef;
242 if ($logDir) {
243 my $fileName = File::Spec->catfile($logDir, "$$.$logId.response.log");
244 if (open(my $fh, '>', $fileName)) {
245 for my $header (sort keys %{$res->{headers}}) {
246 my $value = $res->{headers}->{$header};
247 if (ref $value) {
248 print $fh "$header: $_\n" for @$value;
249 }
250 else {
251 print $fh "$header: $value\n";
252 }
253 }
254 print $fh "\n";
255 print $fh $res->{content} if $res->{content};
256 close($fh);
257 }
258 }
259 if (!$res->{success}) {
260 my $code = $res->{status};
261 if ($code == 401) {
262 $self->error('Authorization error, perhaps invalid user name and/or password', $code);
263 }
264 elsif ($code == 404) {
265 $self->error('Bugzilla server not found, perhaps invalid URL.', $code);
266 }
267 else {
268 my $msg = $res->{reason};
269 $msg .= ' : ' . $res->{content} if $res->{content};
270 $self->error("Unknown error: $msg", $code);
271 }
272 }
273  
274 return $response
275 }
276  
277 sub parse_response {
278 my($self, $contents) = @_;
279 my $parser = BZ::Client::XMLRPC::Parser->new();
280 return $parser->parse($contents)
281 }
282  
283 sub request {
284 my $self = shift;
285 my %args = @_;
286 my $methodName = $args{'methodName'};
287 $self->error('Missing argument: methodName')
288 unless defined($methodName);
289 my $params = $args{'params'};
290 $self->error('Missing argument: params')
291 unless defined($params);
292 $self->error('Invalid argument: params (Expected array)')
293 unless ref($params) eq 'ARRAY';
294 my $contents = $self->create_request($methodName, $params);
295 $self->log('debug', "BZ::Client::XMLRPC::request: Sending method $methodName to " . $self->url());
296 my $response = $self->get_response($contents);
297 $self->log('debug', "BZ::Client::XMLRPC::request: Got result for method $methodName");
298 return $self->parse_response($response)
299 }
300  
301 sub log {
302 my($self, $level, $msg) = @_;
303 my $logger = $self->logger();
304 if ($logger) {
305 &$logger($level, $msg);
306 }
307 }
308  
309 sub logger {
310 my($self) = shift;
311 if (@_) {
312 $self->{'logger'} = shift;
313 }
314 else {
315 return $self->{'logger'};
316 }
317 }
318  
319 sub logDirectory {
320 my($self) = shift;
321 if (@_) {
322 $self->{'logDirectory'} = shift;
323 }
324 else {
325 return $self->{'logDirectory'};
326 }
327 }
328  
329 ### Objects to represent data types
330  
331 package BZ::Client::XMLRPC::int;
332 $BZ::Client::XMLRPC::int::VERSION = '4.91';
333 sub new {
334 my($class, $value) = @_;
335 return bless(\$value, (ref($class) || $class))
336 }
337  
338 sub stringify {
339 my $self = shift;
340 return $$self
341 }
342  
343 package BZ::Client::XMLRPC::base64;
344 $BZ::Client::XMLRPC::base64::VERSION = '4.91';
345 use MIME::Base64 qw( decode_base64 encode_base64 );
346  
347 sub new {
348 my($class, $value) = @_;
349 return bless(
350 {
351 raw => $value,
352 base64 => encode_base64($value, '')
353 },
354 (ref($class) || $class))
355 }
356  
357 sub new64 {
358 my($class, $value) = @_;
359 return bless(
360 {
361 raw => decode_base64($value),
362 base64 => $value
363 },
364 (ref($class) || $class))
365 }
366  
367 sub base64 {
368 my $self = shift;
369 return $self->{base64}
370 }
371  
372 sub raw {
373 my $self = shift;
374 return $self->{raw}
375 }
376  
377 package BZ::Client::XMLRPC::boolean;
378 $BZ::Client::XMLRPC::boolean::VERSION = '4.91';
379 sub new {
380 my($class, $value) = @_;
381 return bless(\$value, (ref($class) || $class))
382 }
383  
384 sub stringify {
385 my $self = shift;
386 return $$self ? '1' : '0';
387 }
388  
389 {
390  
391 my $true = BZ::Client::XMLRPC::boolean->new(1);
392 my $false = BZ::Client::XMLRPC::boolean->new(0);
393  
394 sub TRUE { $true }
395 sub FALSE { $false }
396  
397 }
398  
399 package BZ::Client::XMLRPC::double;
400 $BZ::Client::XMLRPC::double::VERSION = '4.91';
401 sub new {
402 my($class, $value) = @_;
403 return bless(\$value, (ref($class) || $class))
404 }
405  
406 sub stringify {
407 my $self = shift;
408 return $$self
409 }
410  
411 1;
412  
413 __END__
« Client/XMLRPC/Parser.pm ↑ Summary