comparison translate.pl @ 7:29309bc8d932

initial objects to work with bugzilla web service
author cin
date Fri, 04 Sep 2015 19:41:28 +0300
parents 2a5f38eb25a9
children cc7244ab1b9f
comparison
equal deleted inserted replaced
6:2a5f38eb25a9 7:29309bc8d932
120 $writer->endTag(); 120 $writer->endTag();
121 121
122 close HIN; 122 close HIN;
123 finish($proc); 123 finish($proc);
124 124
125 #xalan( 125 package BzRest;
126 # in => \*OUT,
127 # out => \*STDOUT,
128 # params => [
129 #
130 # ]
131 #);
132 126
133 sub xalan {
134 my @params = @_;
135 return system 'java',
136 -cp => join( ':', @ClassPath ),
137 "org.apache.xalan.xslt.Process", @params;
138 }
139 127
140 package BzRest;
141 use fields qw(url apikey);
142 use LWP::UserAgent;
143 use XMLRPC::Lite;
144 use YAML::XS qw(Dump);
145
146 use constant { SELF => __PACKAGE__ };
147
148 sub new {
149 my $class = shift;
150 $class = ref $class || $class;
151
152 my $inst = fields::new($class);
153 $inst->CTOR(@_);
154
155 return $inst;
156 }
157
158 sub CTOR {
159 my SELF $this = shift;
160 my %params = @_;
161
162 $this->{url} = $params{url} or die "An url is required";
163 $this->{apikey} = $params{apikey} if $params{apikey};
164 }
165
166 sub GetBug {
167 my SELF $this = shift;
168 my $id = shift;
169 my %params = @_;
170
171 $params{api_key} = $this->{apikey};
172
173 my $bugurl = URI->new_abs( 'rest/bug/' . $id, $this->{url} );
174 $bugurl->query_form( \%params );
175
176 my $agent = LWP::UserAgent->new();
177 my $res = $agent->get($bugurl);
178
179 return $this->_AssertResponse( $res, $bugurl );
180 }
181
182 sub GetBugs {
183 my SELF $this = shift;
184
185 return $this->CallXMLRPC( 'Bug.get', shift )->{bugs};
186 }
187
188 sub CallXMLRPC {
189 my SELF $this = shift;
190 my ( $method, $params ) = @_;
191
192 die "Method must be specified" unless $method;
193 $params ||= {};
194
195 $params->{api_key} = $this->{apikey};
196 my $url = URI->new_abs( 'xmlrpc.cgi', $this->{url} );
197
198 my $result = XMLRPC::Lite->proxy($url)->call( $method, $params );
199
200 die $result->fault if $result->fault;
201 return $result->result;
202 }
203
204 sub _AssertResponse {
205 my SELF $this = shift;
206 my ( $res, $url ) = @_;
207
208 die "Failed to get any response: " . $url unless $res;
209
210 die "Failed to fetch: " . $url . ": " . $res->code unless $res->is_success;
211
212 my $bug = JSON->new()->utf8()->decode( $res->content );
213
214 die "Bugzilla failed: " . $bug->{message} if $bug->{error};
215
216 return $bug->{bugs};
217 }
218 128
219 __END__ 129 __END__
220 130
221 =pod 131 =pod
222 132