blib/lib/Kayako/RestAPI.pm

filter: Statementstmt 72.1% 101 / 140 Branchbran 40.0% 4 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.6% 28 / 37 Podpod 58.3% 14 / 24 totaltotal 68.5% 148 / 216 CC 10 SCAR 8.8
CC
10
Cov
69%
_query
12.0
_generate_t_samples
6.0
get_ticket_hash
3.3
CRAP 2.4
↑ Summary
1 package Kayako::RestAPI;
2 1 $Kayako::RestAPI::VERSION = '0.07';
3  
4 # ABSTRACT: Perl library for working with L<Kayako REST API|https://kayako.atlassian.net/wiki/display/DEV/Kayako+REST+API>. Tested with
5  
6  
7 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Digest::SHA qw(hmac_sha256_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use XML::LibXML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $xs = XML::LibXML::Simple->new();
16  
17 1 say "Load local lib";
18  
19 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
20 1 my $class = shift;
21 1 my $o = {};
22 1 $o->{auth_hash} = shift;
23 1 $o->{xml2json_options} = shift;
24 $o->{xml2json_options} =
25 { content_key => 'text', pretty => 1, attribute_prefix => 'attr_' }
26 1 ▶ if not defined $o->{xml2json_options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"xml2json_options"} 0 1 1
27 1 $o->{ua} = Mojo::UserAgent->new;
28 1 bless $o, $class;
29 1 return $o;
30 }
31  
32 1 ▶sub _prepare_request {
Subroutine _prepare_request: called CC=1 SCAR=0.0
33 1 my $self = shift;
34 1 my @alphabet = ( ( 'a' .. 'z' ), ( 'A' .. 'Z' ), 0 .. 9 );
35 1 my $len = 10;
36 1 my $salt = join '', map { $alphabet[ rand(@alphabet) ] } 1 .. $len;
37 my $digest =
38 1 hmac_sha256_base64( $salt, $self->{auth_hash}{secret_key} ) . '=';
39 my $hash = {
40 "apikey" => $self->{auth_hash}{api_key},
41 1 "salt" => $salt,
42 "signature" => $digest
43 };
44 1 return $hash;
45 }
46  
47  
48 19 ▶sub xml2obj {
Subroutine xml2obj: called CC=1 SCAR=0.0
Pod documented
49 19 my ( $self, $xml ) = @_;
50  
51 4 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
52 4 my $die_str = '';
53 4 ▶ if ( ref $_[0] eq 'XML::LibXML::Error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'XML::LibXML::Error') { } 2 2 4
54 2 $die_str = "XML::LibXML::Error\n";
55 2 $die_str .= "Message : " . $_[0]->message();
56 }
57 else {
58 2 $die_str = $_[0] . "\n";
59 }
60 4 $die_str .= "Last res: \n" . $self->last_res;
61 4 die $die_str;
62 19 };
63  
64 19 $xs->XMLin($xml);
65 }
66  
67 # abstract api GET/POST/PUT/DELETE _query. return plain xml
68 0 ▶sub _query {
Subroutine _query: not called CC=3 SCAR=24.8
69 0 my ( $self, $method, $route, $params ) = @_;
70 0 $params->{e} = $route;
71 0 my %hash = ( %{ $self->_prepare_request }, %$params );
72  
73 my $res =
74 0 $self->{ua}->$method( $self->{auth_hash}{api_url} => form => \%hash )
75 ->res;
76  
77 0 ▶ if ( $res->is_success ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
elsif ($res->code == 301) { } 0 0 0
78 0 my $xml = $res->body;
79 0 $self->{last_res} = $res;
80  
81 # $xml =~ s/([\x00-\x09]+)|([\x0B-\x1F]+)//g;
82 0 return $xml;
83 }
84 elsif ( $res->code == 301 ) {
85 0 die "Moved Permanently: " . $res->headers->location;
86 }
87 else {
88 0 die "HTTP Error: " . $res->code;
89 }
90 }
91  
92  
93 4 ▶sub last_res {
Subroutine last_res: called CC=1 SCAR=0.0
Pod documented
94 4 shift->{last_res};
95 }
96  
97 11 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
98 11 my ( $self, $route, $params ) = @_;
99 11 $self->_query( 'get', $route, $params );
100 }
101  
102 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
103 0 my ( $self, $route, $params ) = @_;
104 0 $self->_query( 'put', $route, $params );
105 }
106  
107 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my ( $self, $route, $params ) = @_;
109 0 $self->_query( 'post', $route, $params );
110 }
111  
112 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ( $self, $route, $params ) = @_;
114 0 $self->_query( 'delete', $route, $params );
115 }
116  
117  
118 0 ▶sub get_hash {
Subroutine get_hash: not called CC=1 SCAR=6.9
Pod documented
119 0 my ( $self, $route, $params ) = @_;
120 0 my $xml = $self->get( $route, $params );
121 0 return $self->xml2obj($xml);
122 }
123  
124  
125 1 ▶sub get_ticket_xml {
Subroutine get_ticket_xml: called CC=1 SCAR=0.0
Pod documented
126 1 my ( $self, $ticket_id, $params ) = @_;
127 1 return $self->get( "/Tickets/Ticket/" . $ticket_id . "/", $params );
128 }
129  
130  
131 1 ▶sub get_ticket_hash {
Subroutine get_ticket_hash: called CC=3 SCAR=12.0
Pod documented
132 1 my ( $self, $ticket_id ) = @_;
133 1 my $xml = $self->get_ticket_xml($ticket_id);
134 1 my $hash = {};
135 1 ▶ if ( $xml eq 'Ticket not Found' || $xml eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml eq 'Ticket not Found' or $xml eq '') { } 0 1 1
Condition33%1/3
$xml eq "Ticket not Found" or $xml eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
$xml eq "Ticket not Found" or $xml eq ""
$xml eq "Ticket not Found" $xml eq ""
Truth table33%1/3
A or B
A= $xml eq "Ticket not Found"B= $xml eq ""
ABresult
1X1
011
000
136 0 die "Ticket not Found";
137  
138 # $hash->{'ticket_id'} = $ticket_id;
139 # $hash->{'is_found'} = 0;
140 }
141 else {
142 1 $hash = $self->xml2obj($xml)->{ticket};
143 }
144 1 return $hash;
145 }
146  
147  
148 0 ▶sub change_ticket_owner {
Subroutine change_ticket_owner: not called CC=1 SCAR=6.9
Pod documented
149 0 my ( $self, $ticket_id, $new_owner_id ) = @_;
150 0 my $content_key = $self->{xml2json_options}{content_key};
151 0 my $old_data = $self->get_ticket_hash($ticket_id);
152 0 $self->put(
153 "/Tickets/Ticket/" . $ticket_id . "/",
154 { ownerstaffid => $new_owner_id }
155 );
156 0 my $new_data = $self->get_ticket_hash($ticket_id);
157 my $res = {
158 old => {
159 ownerstaffid => $old_data->{ownerstaffid}{$content_key},
160 ownerstaffname => $old_data->{ownerstaffname}{$content_key}
161 },
162 new => {
163 ownerstaffid => $new_data->{ownerstaffid}{$content_key},
164 0 ownerstaffname => $new_data->{ownerstaffname}{$content_key}
165 }
166 };
167 0 return $res;
168 }
169  
170  
171 0 ▶sub make_unassigned {
Subroutine make_unassigned: not called CC=1 SCAR=6.9
Pod documented
172 0 my ( $self, $ticket_id ) = @_;
173 0 $self->change_ticket_owner( $ticket_id, 0 );
174 }
175  
176  
177 0 ▶sub create_ticket {
Subroutine create_ticket: not called CC=1 SCAR=6.9
Pod documented
178 0 my ( $self, $params ) = @_;
179 0 my $xml = $self->post( '/Tickets/Ticket/', $params );
180 0 $self->xml2obj($xml);
181 }
182  
183  
184 1 ▶sub filter_fields {
Subroutine filter_fields: called CC=2 SCAR=6.9
Pod documented
185 1 my ( $self, $a ) = @_; # array of hashes
186 1 my $key = $self->{xml2json_options}{content_key};
187 1 for my $j (@$a) {
188 4 $j = { map { $_ => $j->{$_}{$key} }
189 2 grep { exists $j->{$_} } qw/id title module/ };
190 }
191 1 return $a;
192 }
193  
194 # Convert nested hash with id keys into array of hashes. For compatibility with old API
195 # Usage is same as filter_fields
196 5 ▶sub _postprocess_libxml {
Subroutine _postprocess_libxml: called CC=2 SCAR=6.9
197 5 my ( $self, $hash ) = @_; # array of hashes
198 5 my @res;
199 5 for my $k ( sort keys %$hash ) {
200  
201 # my $j = { map { $_ => $hash->{$k} } grep { exists $hash->{$k} } qw/id title module fullname/ };
202 # warn "Element:".Dumper $j;
203 my $j = {
204 id => $k,
205 title => $hash->{$k}{title},
206 module => $hash->{$k}{module}
207 20 };
208 20 push @res, $j;
209  
210 # push @res, $j;
211 }
212 5 return \@res;
213 }
214  
215  
216 2 ▶sub get_departements {
Subroutine get_departements: called CC=1 SCAR=0.0
Pod undocumented
217 2 my $self = shift;
218 2 my $xml = $self->get('/Base/Department/');
219 2 $self->xml2obj($xml)->{department};
220 }
221  
222 1 ▶sub get_departements_old {
Subroutine get_departements_old: called CC=1 SCAR=0.0
Pod documented
223 1 my $self = shift;
224 1 $self->_postprocess_libxml( $self->get_departements );
225 }
226  
227  
228 2 ▶sub get_ticket_statuses {
Subroutine get_ticket_statuses: called CC=1 SCAR=0.0
Pod undocumented
229 2 my $self = shift;
230 2 my $xml = $self->get('/Tickets/TicketStatus/');
231 2 $self->xml2obj($xml)->{ticketstatus};
232 }
233  
234 1 ▶sub get_ticket_statuses_old {
Subroutine get_ticket_statuses_old: called CC=1 SCAR=0.0
Pod documented
235 1 my $self = shift;
236 1 $self->_postprocess_libxml( $self->get_ticket_statuses );
237 }
238  
239  
240 2 ▶sub get_ticket_priorities {
Subroutine get_ticket_priorities: called CC=1 SCAR=0.0
Pod undocumented
241 2 my $self = shift;
242 2 my $xml = $self->get('/Tickets/TicketPriority/');
243 2 $self->xml2obj($xml)->{ticketpriority};
244 }
245  
246 1 ▶sub get_ticket_priorities_old {
Subroutine get_ticket_priorities_old: called CC=1 SCAR=0.0
Pod documented
247 1 my $self = shift;
248 1 $self->_postprocess_libxml( $self->get_ticket_priorities );
249 }
250  
251  
252 2 ▶sub get_ticket_types {
Subroutine get_ticket_types: called CC=1 SCAR=0.0
Pod undocumented
253 2 my $self = shift;
254 2 my $xml = $self->get('/Tickets/TicketType/');
255 2 $self->xml2obj($xml)->{tickettype}; # array
256 }
257  
258 1 ▶sub get_ticket_types_old {
Subroutine get_ticket_types_old: called CC=1 SCAR=0.0
Pod documented
259 1 my $self = shift;
260 1 $self->_postprocess_libxml( $self->get_ticket_types );
261 }
262  
263  
264 2 ▶sub get_staff {
Subroutine get_staff: called CC=1 SCAR=0.0
Pod undocumented
265 2 my $self = shift;
266 2 my $xml = $self->get('/Base/Staff/');
267 2 $self->xml2obj($xml)->{staff}; # array
268 }
269  
270 1 ▶sub get_staff_old {
Subroutine get_staff_old: called CC=1 SCAR=0.0
Pod documented
271 1 my $self = shift;
272 1 $self->_postprocess_libxml( $self->get_staff );
273 }
274  
275 # HTTP mocks
276 # perl -Ilib -MData::Dumper -MKayako::RestAPI -E 'print Dumper Kayako::RestAPI::_samples();'
277 12 ▶sub _samples {
Subroutine _samples: called CC=1 SCAR=0.0
278 return [
279 {
280 12 method => 'get',
281 route => '/Tickets/Ticket/1000/',
282 sample_file => 'ticket.xml',
283 params => {}
284 },
285 {
286 method => 'get',
287 route => '/Base/Department/',
288 sample_file => 'departments.xml'
289 },
290 {
291 method => 'get',
292 route => '/Tickets/TicketStatus/',
293 sample_file => 'ticket_status.xml'
294 },
295 {
296 method => 'get',
297 route => '/Tickets/TicketPriority/',
298 sample_file => 'ticket_priority.xml'
299 },
300 {
301 method => 'get',
302 route => '/Tickets/TicketType/',
303 sample_file => 'ticket_type.xml'
304 },
305 {
306 method => 'get',
307 route => '/Base/Staff/',
308 sample_file => 'staff.xml'
309 }
310 ];
311 }
312  
313 # generate ethalon data to t/lib/Kayako/samples
314 # perl -Ilib -MData::Dumper -MKayako::RestAPI -E 'print Dumper Kayako::RestAPI::_generate_t_samples();'
315 0 ▶sub _generate_t_samples {
Subroutine _generate_t_samples: not called CC=2 SCAR=17.9
316 0 my @samples = @{ __PACKAGE__->_samples };
317 0 my $k = Kayako::RestAPI->new(
318 {
319 api_url => '',
320 api_key => '',
321 secret_key => ''
322 }
323 );
324  
325 0 for my $s (@samples) {
326 0 my $data = $k->_query( $s->{method}, $s->{route} );
327 write_file( 't/lib/Kayako/samples/' . $s->{sample_file},
328 0 { binmode => ':raw' }, $data );
329 }
330  
331 }
332  
333 1 1;
334  
335 __END__
↑ Summary