view lib/Benzin/Bugzilla/XmlRpcClient.pm @ 9:cc7244ab1b9f

implemented time reports on bugs
author cin
date Sat, 05 Sep 2015 22:01:12 +0300
parents 29309bc8d932
children 14a966369278
line wrap: on
line source

package Benzin::Bugzilla::XmlRpcClient;
use strict;

use LWP::UserAgent;
use XMLRPC::Lite;
use YAML::XS qw(Dump);

use IMPL::declare {
	require => {
		Bug        => 'Benzin::Bugzilla::Bug',
		BugComment => 'Benzin::Bugzilla::BugComment'
	},
	base => { 'IMPL::Object::Fields' => undef }
};

use fields qw(url apikey);

sub new {
	my $class = shift;
	$class = ref $class || $class;

	my $inst = fields::new($class);
	$inst->CTOR(@_);

	return $inst;
}

sub CTOR {
	my SELF $this = shift;
	my %params = @_;

	$this->{url} = $params{url} or die "An url is required";
	$this->{apikey} = $params{apikey} if $params{apikey};
}

sub GetBugs {
	my SELF $this = shift;

	return [
		map Bug->new($_),
		@{ $this->_CallService( 'Bug.get', shift )->{bugs} || [] }
	];
}

sub PopulateBugsWithComments {
	my SELF $this = shift;
	my $bugs = shift || [];

	if ( my @ids = map $_->{id}, @$bugs ) {

		my $resp = $this->_CallService( 'Bug.comments', { ids => \@ids } );

		for my Bug $bug (@$bugs) {
			$bug->{comments} = [
				map BugComment->new($_),
				@{ $resp->{bugs}{ $bug->{id} }->{comments} || [] }
			];
		}
	}
	return;
}

sub PopulateBugsHistory {
	my SELF $this = shift;

	my %bugs = map { $_->{id}, $_ } @{ shift || [] };

	if ( keys %bugs ) {

		my $resp =
		  $this->_CallService( 'Bug.history', { ids => [ keys %bugs ] } )->{bugs};

		for my $data (@$resp) {
			my Bug $bug = $bugs{$data->{id}};
			
			$bug->{history} = $data->{history};
		}
	}
	return;
}

sub _CallService {
	my SELF $this = shift;
	my ( $method, $params ) = @_;

	die "Method must be specified" unless $method;
	$params ||= {};

	$params->{api_key} = $this->{apikey};
	my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );

	my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );

	die $result->fault if $result->fault;
	return $result->result;
}

1;