blib/lib/Kayako/RestAPI.pm

filter: Statementstmt 69.5% 80 / 115 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 68.9% 20 / 29 Podpod 72.2% 13 / 18 totaltotal 67.8% 116 / 171 CC 6 SCAR 6.2
CC
6
Cov
68%
_generate_t_samples
6.0
get_ticket_hash
3.3
new
2.0
CRAP 1.9
↑ Summary
1 package Kayako::RestAPI;
2 1 $Kayako::RestAPI::VERSION = '0.05';
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::XML2JSON;
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 sub new {
16 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
17 1 my $o = {};
18 1 $o->{auth_hash} = shift;
19 1 $o->{xml2json_options} = shift;
20 $o->{xml2json_options} =
21 { content_key => 'text', pretty => 1, attribute_prefix => 'attr_' }
22 1 ▶ if not defined $o->{xml2json_options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"xml2json_options"} 0 1 1
23 1 $o->{xml2json} = XML::XML2JSON->new( %{ $o->{xml2json_options} } );
24 1 $o->{ua} = Mojo::UserAgent->new;
25 1 bless $o, $class;
26 1 return $o;
27 }
28  
29 sub _prepare_request {
30 1 ▶ my $self = shift;
Subroutine _prepare_request: called CC=1 SCAR=0.0
31 1 my @alphabet = ( ( 'a' .. 'z' ), ( 'A' .. 'Z' ), 0 .. 9 );
32 1 my $len = 10;
33 1 my $salt = join '', map { $alphabet[ rand(@alphabet) ] } 1 .. $len;
34 my $digest =
35 1 hmac_sha256_base64( $salt, $self->{auth_hash}{secret_key} ) . '=';
36 my $hash = {
37 "apikey" => $self->{auth_hash}{api_key},
38 1 "salt" => $salt,
39 "signature" => $digest
40 };
41 1 return $hash;
42 }
43  
44  
45 sub xml2obj {
46 5 ▶ my ( $self, $xml ) = @_;
Subroutine xml2obj: called CC=1 SCAR=0.0
Pod documented
47 5 $self->{xml2json}->xml2obj($xml);
48 }
49  
50 # abstract api GET/POST/PUT/DELETE _query. return plain xml
51 sub _query {
52 0 ▶ my ( $self, $method, $route, $params ) = @_;
Subroutine _query: not called CC=1 SCAR=6.9
53 0 $params->{e} = $route;
54 0 my %hash = ( %{ $self->_prepare_request }, %$params );
55 my $xml =
56 0 $self->{ua}->$method( $self->{auth_hash}{api_url} => form => \%hash )
57 ->res->body;
58 0 $xml =~ s/([\x00-\x09]+)|([\x0B-\x1F]+)//g;
59 0 return $xml;
60 }
61  
62 sub get {
63 6 ▶ my ( $self, $route, $params ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
64 6 $self->_query( 'get', $route, $params );
65 }
66  
67 sub put {
68 0 ▶ my ( $self, $route, $params ) = @_;
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
69 0 $self->_query( 'put', $route, $params );
70 }
71  
72 sub post {
73 0 ▶ my ( $self, $route, $params ) = @_;
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
74 0 $self->_query( 'post', $route, $params );
75 }
76  
77 sub delete {
78 0 ▶ my ( $self, $route, $params ) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
79 0 $self->_query( 'delete', $route, $params );
80 }
81  
82  
83 sub get_hash {
84 0 ▶ my ( $self, $route, $params ) = @_;
Subroutine get_hash: not called CC=1 SCAR=6.9
Pod documented
85 0 my $xml = $self->get( $route, $params );
86 0 return $self->{xml2json}->xml2obj($xml);
87 }
88  
89  
90 sub get_ticket_xml {
91 1 ▶ my ( $self, $ticket_id, $params ) = @_;
Subroutine get_ticket_xml: called CC=1 SCAR=0.0
Pod documented
92 1 return $self->get( "/Tickets/Ticket/" . $ticket_id . "/", $params );
93 }
94  
95  
96 sub get_ticket_hash {
97 1 ▶ my ( $self, $ticket_id ) = @_;
Subroutine get_ticket_hash: called CC=3 SCAR=12.0
Pod documented
98 1 my $xml = $self->get_ticket_xml($ticket_id);
99 1 my $hash = {};
100 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
101 0 die "Ticket not Found";
102  
103 # $hash->{'ticket_id'} = $ticket_id;
104 # $hash->{'is_found'} = 0;
105 }
106 else {
107 1 $hash = $self->{xml2json}->xml2obj($xml)->{tickets}{ticket};
108 }
109 1 return $hash;
110 }
111  
112  
113 sub change_ticket_owner {
114 0 ▶ my ( $self, $ticket_id, $new_owner_id ) = @_;
Subroutine change_ticket_owner: not called CC=1 SCAR=6.9
Pod documented
115 0 my $content_key = $self->{xml2json_options}{content_key};
116 0 my $old_data = $self->get_ticket_hash($ticket_id);
117 0 $self->put(
118 "/Tickets/Ticket/" . $ticket_id . "/",
119 { ownerstaffid => $new_owner_id }
120 );
121 0 my $new_data = $self->get_ticket_hash($ticket_id);
122 my $res = {
123 old => {
124 ownerstaffid => $old_data->{ownerstaffid}{$content_key},
125 ownerstaffname => $old_data->{ownerstaffname}{$content_key}
126 },
127 new => {
128 ownerstaffid => $new_data->{ownerstaffid}{$content_key},
129 0 ownerstaffname => $new_data->{ownerstaffname}{$content_key}
130 }
131 };
132 0 return $res;
133 }
134  
135  
136 sub make_unassigned {
137 0 ▶ my ( $self, $ticket_id ) = @_;
Subroutine make_unassigned: not called CC=1 SCAR=6.9
Pod documented
138 0 $self->change_ticket_owner( $ticket_id, 0 );
139 }
140  
141  
142 sub create_ticket {
143 0 ▶ my ( $self, $params ) = @_;
Subroutine create_ticket: not called CC=1 SCAR=6.9
Pod documented
144 0 my $xml = $self->post( '/Tickets/Ticket/', $params );
145 0 $self->xml2obj($xml);
146 }
147  
148  
149 sub filter_fields {
150 5 ▶ my ( $self, $a ) = @_; # array of hashes
Subroutine filter_fields: called CC=2 SCAR=6.9
Pod documented
151 5 my $key = $self->{xml2json_options}{content_key};
152 5 for my $j (@$a) {
153 40 $j = { map { $_ => $j->{$_}{$key} }
154 18 grep { exists $j->{$_} } qw/id title module/ };
155 }
156 5 return $a;
157 }
158  
159  
160 sub get_departements {
161 1 ▶ my $self = shift;
Subroutine get_departements: called CC=1 SCAR=0.0
Pod documented
162 1 my $xml = $self->get('/Base/Department/');
163 1 my $a = $self->xml2obj($xml)->{departments}{department}; # array
164 1 $self->filter_fields($a);
165 }
166  
167  
168 sub get_ticket_statuses {
169 1 ▶ my $self = shift;
Subroutine get_ticket_statuses: called CC=1 SCAR=0.0
Pod documented
170 1 my $xml = $self->get('/Tickets/TicketStatus/');
171 1 my $a = $self->xml2obj($xml)->{ticketstatuses}{ticketstatus}; # array
172 1 $self->filter_fields($a);
173 }
174  
175  
176 sub get_ticket_priorities {
177 1 ▶ my $self = shift;
Subroutine get_ticket_priorities: called CC=1 SCAR=0.0
Pod documented
178 1 my $xml = $self->get('/Tickets/TicketPriority/');
179 1 my $a = $self->xml2obj($xml)->{ticketpriorities}{ticketpriority}; # array
180 1 $self->filter_fields($a);
181 }
182  
183  
184 sub get_ticket_types {
185 1 ▶ my $self = shift;
Subroutine get_ticket_types: called CC=1 SCAR=0.0
Pod documented
186 1 my $xml = $self->get('/Tickets/TicketType/');
187 1 my $a = $self->xml2obj($xml)->{tickettypes}{tickettype}; # array
188 1 $self->filter_fields($a);
189 }
190  
191  
192 sub get_staff {
193 1 ▶ my $self = shift;
Subroutine get_staff: called CC=1 SCAR=0.0
Pod documented
194 1 my $xml = $self->get('/Base/Staff/');
195 1 $self->xml2obj($xml)->{staffusers}{staff}; # array
196 }
197  
198 # HTTP mocks
199 # perl -Ilib -MData::Dumper -MKayako::RestAPI -E 'print Dumper Kayako::RestAPI::_samples();'
200 sub _samples {
201 return [
202 {
203 6 ▶ method => 'get',
Subroutine _samples: called CC=1 SCAR=0.0
204 route => '/Tickets/Ticket/1000/',
205 sample_file => 'ticket.xml',
206 params => {}
207 },
208 {
209 method => 'get',
210 route => '/Base/Department/',
211 sample_file => 'departments.xml'
212 },
213 {
214 method => 'get',
215 route => '/Tickets/TicketStatus/',
216 sample_file => 'ticket_status.xml'
217 },
218 {
219 method => 'get',
220 route => '/Tickets/TicketPriority/',
221 sample_file => 'ticket_priority.xml'
222 },
223 {
224 method => 'get',
225 route => '/Tickets/TicketType/',
226 sample_file => 'ticket_type.xml'
227 },
228 {
229 method => 'get',
230 route => '/Base/Staff/',
231 sample_file => 'staff.xml'
232 }
233 ];
234 }
235  
236 # generate ethalon data to t/lib/Kayako/samples
237 # perl -Ilib -MData::Dumper -MKayako::RestAPI -E 'print Dumper Kayako::RestAPI::_generate_t_samples();'
238 sub _generate_t_samples {
239 0 ▶ my @samples = @{ __PACKAGE__->_samples };
Subroutine _generate_t_samples: not called CC=2 SCAR=17.9
240 0 my $k = Kayako::RestAPI->new(
241 {
242 api_url => '',
243 api_key => '',
244 secret_key => ''
245 }
246 );
247  
248 0 for my $s (@samples) {
249 0 my $data = $k->_query( $s->{method}, $s->{route} );
250 write_file( 't/lib/Kayako/samples/' . $s->{sample_file},
251 0 { binmode => ':raw' }, $data );
252 }
253  
254 }
255  
256 1 1;
257  
258 __END__
↑ Summary