comparison lib/Benzin/Bugzilla/XmlRpcClient.pm @ 7:29309bc8d932

initial objects to work with bugzilla web service
author cin
date Fri, 04 Sep 2015 19:41:28 +0300
parents
children cc7244ab1b9f
comparison
equal deleted inserted replaced
6:2a5f38eb25a9 7:29309bc8d932
1 package Benzin::Bugzilla::XmlRpcClient;
2 use strict;
3
4 use LWP::UserAgent;
5 use XMLRPC::Lite;
6 use YAML::XS qw(Dump);
7
8 use IMPL::require {
9 Bug => 'Benzin::Bugzilla::Bug'
10 };
11
12 use fields qw(url apikey);
13
14 use constant { SELF => __PACKAGE__ };
15
16 sub new {
17 my $class = shift;
18 $class = ref $class || $class;
19
20 my $inst = fields::new($class);
21 $inst->CTOR(@_);
22
23 return $inst;
24 }
25
26 sub CTOR {
27 my SELF $this = shift;
28 my %params = @_;
29
30 $this->{url} = $params{url} or die "An url is required";
31 $this->{apikey} = $params{apikey} if $params{apikey};
32 }
33
34 sub GetBugs {
35 my SELF $this = shift;
36
37 return [map Bug->new($_), @{$this->_CallService( 'Bug.get', shift )->{bugs} || [] }];
38 }
39
40 sub FillBugComments {
41 my SELF $this = shift;
42 my $bugs = shift || [];
43
44 if ( my @ids = map $_->{id}, @$bugs ) {
45
46 my $comments = $this->_CallService( 'Bug.comments', { ids => \@ids } );
47
48 for my Bug $bug (@$bugs) {
49 map @{$comments->{$bug->{id}}->{comments} || [] };
50 }
51 }
52 }
53
54 sub _CallService {
55 my SELF $this = shift;
56 my ( $method, $params ) = @_;
57
58 die "Method must be specified" unless $method;
59 $params ||= {};
60
61 $params->{api_key} = $this->{apikey};
62 my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );
63
64 my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );
65
66 die $result->fault if $result->fault;
67 return $result->result;
68 }
69
70 1;