| 1 | package Kayako::RestAPI; | ||||||||||||||||||||||||||||
| 2 | 1 | $Kayako::RestAPI::VERSION = '0.03'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl library for working with L<Kayako REST API|https://kayako.atlassian.net/wiki/display/DEV/Kayako+REST+API> | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Digest::SHA qw(hmac_sha256_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use XML::XML2JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub new { | ||||||||||||||||||||||||||||
| 14 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 1 | my $o = {}; | |||||||||||||||||||||||||||
| 16 | 1 | $o->{auth_hash} = shift; | |||||||||||||||||||||||||||
| 17 | 1 | $o->{xml2json_options} = shift; | |||||||||||||||||||||||||||
| 18 | 1 | ▶ | $o->{xml2json_options} = { content_key => 'text', pretty => 1, attribute_prefix => 'attr_' } if not defined $o->{xml2json_options}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 1 | $o->{xml2json} = XML::XML2JSON->new(%{$o->{xml2json_options}}); | |||||||||||||||||||||||||||
| 20 | 1 | $o->{ua} = Mojo::UserAgent->new; | |||||||||||||||||||||||||||
| 21 | 1 | bless $o, $class; | |||||||||||||||||||||||||||
| 22 | 1 | return $o; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub _prepare_request { | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_prepare_request: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | my @alphabet = (('a'..'z'), ('A'..'Z'), 0..9); | |||||||||||||||||||||||||||
| 28 | 1 | my $len = 10; | |||||||||||||||||||||||||||
| 29 | 1 | my $salt = join '', map {$alphabet[rand(@alphabet)]} 1..$len; | |||||||||||||||||||||||||||
| 30 | 1 | my $digest = hmac_sha256_base64($salt, $self->{auth_hash}{secret_key}).'='; | |||||||||||||||||||||||||||
| 31 | my $hash = { | ||||||||||||||||||||||||||||
| 32 | "apikey" => $self->{auth_hash}{api_key}, | ||||||||||||||||||||||||||||
| 33 | 1 | "salt" => $salt, | |||||||||||||||||||||||||||
| 34 | "signature" => $digest | ||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | 1 | return $hash; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub xml2obj { | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my ($self, $xml) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xml2obj: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 0 | $self->{xml2json}->xml2obj($xml); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # abstract api GET/POST/PUT/DELETE _query. return plain xml | ||||||||||||||||||||||||||||
| 46 | sub _query { | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ($self, $method, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_query: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | $params->{e} = $route; | |||||||||||||||||||||||||||
| 49 | 0 | my %hash = (%{$self->_prepare_request}, %$params); | |||||||||||||||||||||||||||
| 50 | 0 | my $xml = $self->{ua}->$method($self->{auth_hash}{api_url} => form => \%hash)->res->body; | |||||||||||||||||||||||||||
| 51 | 0 | $xml =~ s/([\x00-\x09]+)|([\x0B-\x1F]+)//g; | |||||||||||||||||||||||||||
| 52 | 0 | return $xml; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub get { | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | my ($self, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | $self->_query('get', $route, $params); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub put { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($self, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->_query('put', $route, $params); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub post { | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my ($self, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | $self->_query('post', $route, $params); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub delete { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ($self, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 0 | $self->_query('delete', $route, $params); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub get_hash { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my ($self, $route, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my $xml = $self->get($route, $params); | |||||||||||||||||||||||||||
| 79 | 0 | return $self->{xml2json}->xml2obj($xml); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub get_ticket_xml { | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my ($self, $ticket_id, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_ticket_xml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | return $self->get("/Tickets/Ticket/".$ticket_id."/", $params); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub get_ticket_hash { | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self, $ticket_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_ticket_hash: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 0 | my $xml = $self->get_ticket_xml($ticket_id); | |||||||||||||||||||||||||||
| 94 | 0 | my $hash = {}; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($xml eq 'Ticket not Found' || $xml eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$xml eq "Ticket not Found" or $xml eq ""
MC/DC0%0/2
$xml eq "Ticket not Found" or $xml eq ""
$xml eq "Ticket not Found"
$xml eq ""
Truth table0%0/3
A or B
A= $xml eq "Ticket not Found"B= $xml eq ""
| |||||||||||||||||||||||||||||
| 96 | 0 | die "Ticket not Found"; | |||||||||||||||||||||||||||
| 97 | # $hash->{'ticket_id'} = $ticket_id; | ||||||||||||||||||||||||||||
| 98 | # $hash->{'is_found'} = 0; | ||||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 0 | $hash = $self->{xml2json}->xml2obj($xml)->{tickets}{ticket}; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | return $hash; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub change_ticket_owner { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my ($self, $ticket_id, $new_owner_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
change_ticket_owner: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | my $content_key = $self->{xml2json_options}{content_key}; | |||||||||||||||||||||||||||
| 109 | 0 | my $old_data = $self->get_ticket_hash($ticket_id); | |||||||||||||||||||||||||||
| 110 | 0 | $self->put("/Tickets/Ticket/".$ticket_id."/", { ownerstaffid => $new_owner_id }); | |||||||||||||||||||||||||||
| 111 | 0 | my $new_data = $self->get_ticket_hash($ticket_id); | |||||||||||||||||||||||||||
| 112 | my $res = { | ||||||||||||||||||||||||||||
| 113 | old => { | ||||||||||||||||||||||||||||
| 114 | ownerstaffid => $old_data->{ownerstaffid}{$content_key}, | ||||||||||||||||||||||||||||
| 115 | ownerstaffname => $old_data->{ownerstaffname}{$content_key} | ||||||||||||||||||||||||||||
| 116 | }, | ||||||||||||||||||||||||||||
| 117 | new => { | ||||||||||||||||||||||||||||
| 118 | ownerstaffid => $new_data->{ownerstaffid}{$content_key}, | ||||||||||||||||||||||||||||
| 119 | 0 | ownerstaffname => $new_data->{ownerstaffname}{$content_key} | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | }; | ||||||||||||||||||||||||||||
| 122 | 0 | return $res; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub make_unassigned { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my ($self, $ticket_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_unassigned: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | $self->change_ticket_owner($ticket_id, 0); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | sub create_ticket { | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
create_ticket: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $xml = $self->post('/Tickets/Ticket/', $params); | |||||||||||||||||||||||||||
| 136 | 0 | $self->xml2obj($xml); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub filter_fields { | ||||||||||||||||||||||||||||
| 142 | 1 | ▶ | my ($self, $a) = @_; # array of hashes | ||||||||||||||||||||||||||
|
Subroutine
filter_fields: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 1 | my $key = $self->{xml2json_options}{content_key}; | |||||||||||||||||||||||||||
| 144 | 1 | for my $j (@$a) { | |||||||||||||||||||||||||||
| 145 | 2 | $j = { map { $_ => $j->{$_}{$key} } grep { exists $j->{$_} } qw/id title module/ }; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 1 | return $a; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub get_departements { | ||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_departements: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 0 | my $xml = $self->get('/Base/Department/'); | |||||||||||||||||||||||||||
| 154 | 0 | my $a = $self->xml2obj($xml)->{departments}{department}; # array | |||||||||||||||||||||||||||
| 155 | 0 | $self->filter_fields($a); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub get_ticket_statuses { | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_ticket_statuses: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $xml = $self->get('/Tickets/TicketStatus/'); | |||||||||||||||||||||||||||
| 164 | 0 | my $a = $self->xml2obj($xml)->{ticketstatuses}{ticketstatus}; # array | |||||||||||||||||||||||||||
| 165 | 0 | $self->filter_fields($a); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub get_ticket_priorities { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_ticket_priorities: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $xml = $self->get('/Tickets/TicketPriority/'); | |||||||||||||||||||||||||||
| 172 | 0 | my $a = $self->xml2obj($xml)->{ticketpriorities}{ticketpriority}; # array | |||||||||||||||||||||||||||
| 173 | 0 | $self->filter_fields($a); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | sub get_ticket_types { | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_ticket_types: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | 0 | my $xml = $self->get('/Tickets/TicketType/'); | |||||||||||||||||||||||||||
| 180 | 0 | my $a = $self->xml2obj($xml)->{tickettypes}{tickettype}; # array | |||||||||||||||||||||||||||
| 181 | 0 | $self->filter_fields($a); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | sub get_staff { | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_staff: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 187 | 0 | my $xml = $self->get('/Base/Staff/'); | |||||||||||||||||||||||||||
| 188 | 0 | $self->xml2obj($xml)->{staffusers}{staff}; # array | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 1 | 1; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||