line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Armadito::Agent::HTTP::Client::ArmaditoGLPI; |
2
|
|
|
|
|
|
|
|
3
|
22
|
|
|
22
|
|
11955210
|
use strict; |
|
22
|
|
|
|
|
26
|
|
|
22
|
|
|
|
|
547
|
|
4
|
22
|
|
|
22
|
|
75
|
use warnings; |
|
22
|
|
|
|
|
26
|
|
|
22
|
|
|
|
|
537
|
|
5
|
22
|
|
|
22
|
|
78
|
use base 'Armadito::Agent::HTTP::Client'; |
|
22
|
|
|
|
|
68
|
|
|
22
|
|
|
|
|
8967
|
|
6
|
|
|
|
|
|
|
|
7
|
22
|
|
|
22
|
|
108
|
use English qw(-no_match_vars); |
|
22
|
|
|
|
|
29
|
|
|
22
|
|
|
|
|
205
|
|
8
|
22
|
|
|
22
|
|
7443
|
use HTTP::Request; |
|
22
|
|
|
|
|
31
|
|
|
22
|
|
|
|
|
277
|
|
9
|
22
|
|
|
22
|
|
11356
|
use HTTP::Request::Common qw{ POST }; |
|
22
|
|
|
|
|
35723
|
|
|
22
|
|
|
|
|
1126
|
|
10
|
22
|
|
|
22
|
|
119
|
use UNIVERSAL::require; |
|
22
|
|
|
|
|
28
|
|
|
22
|
|
|
|
|
119
|
|
11
|
22
|
|
|
22
|
|
409
|
use URI; |
|
22
|
|
|
|
|
50
|
|
|
22
|
|
|
|
|
162
|
|
12
|
22
|
|
|
22
|
|
11669
|
use Encode; |
|
22
|
|
|
|
|
166108
|
|
|
22
|
|
|
|
|
1728
|
|
13
|
22
|
|
|
22
|
|
12312
|
use Data::Dumper; |
|
22
|
|
|
|
|
100922
|
|
|
22
|
|
|
|
|
1202
|
|
14
|
22
|
|
|
22
|
|
130
|
use URI::Escape; |
|
22
|
|
|
|
|
32
|
|
|
22
|
|
|
|
|
9363
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
18
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(%params); |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _prepareVal { |
24
|
0
|
|
|
0
|
|
|
my ( $self, $val ) = @_; |
25
|
|
|
|
|
|
|
|
26
|
0
|
0
|
|
|
|
|
return '' unless length($val); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# forbid too long argument. |
29
|
0
|
|
|
|
|
|
while ( length( URI::Escape::uri_escape_utf8($val) ) > 1500 ) { |
30
|
0
|
|
|
|
|
|
$val =~ s/^.{5}/â¦/; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return URI::Escape::uri_escape_utf8($val); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _prepareURL { |
37
|
0
|
|
|
0
|
|
|
my ( $self, %params ) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
my $url = ref $params{url} eq 'URI' ? $params{url} : URI->new( $params{url} ); |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
if ( $params{method} eq 'GET' ) { |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $urlparams = 'agent_id=' . uri_escape( $params{args}->{agent_id} ); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
foreach my $k ( keys %{ $params{args} } ) { |
|
0
|
|
|
|
|
|
|
46
|
0
|
0
|
0
|
|
|
|
if ( ref( $params{args}->{$k} ) eq 'ARRAY' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
foreach ( @{ $params{args}->{$k} } ) { |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
0
|
|
|
|
$urlparams .= '&' . $k . '[]=' . $self->_prepareVal( $_ || '' ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
elsif ( ref( $params{args}->{$k} ) eq 'HASH' ) { |
52
|
0
|
|
|
|
|
|
foreach ( keys %{ $params{args}->{$k} } ) { |
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$urlparams .= '&' . $k . '[' . $_ . ']=' . $self->_prepareVal( $params{args}->{$k}{$_} ); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ( $k ne 'action' && length( $params{args}->{$k} ) ) { |
57
|
0
|
|
|
|
|
|
$urlparams .= '&' . $k . '=' . $self->_prepareVal( $params{args}->{$k} ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$url .= '?' . $urlparams; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $url; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub sendRequest { |
68
|
0
|
|
|
0
|
1
|
|
my ( $self, %params ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
my $url = $self->_prepareURL(%params); |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
$self->{logger}->debug2($url) if $self->{logger}; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $headers = HTTP::Headers->new( |
75
|
|
|
|
|
|
|
'Content-Type' => 'application/json', |
76
|
|
|
|
|
|
|
'Referer' => $url |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $request = HTTP::Request->new( |
80
|
0
|
|
|
|
|
|
$params{method} => $url, |
81
|
|
|
|
|
|
|
$headers |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
0
|
0
|
0
|
|
|
|
if ( $params{message} && $params{method} eq 'POST' ) { |
85
|
0
|
|
|
|
|
|
$request->content( encode( 'UTF-8', $params{message} ) ); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
return $self->request($request); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
__END__ |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 NAME |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Armadito::Agent::HTTP::Client::ArmaditoGLPI - HTTP Client for armadito plugin for GLPI. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 DESCRIPTION |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This is the class used by Armadito agent to communicate with armadito plugin in GLPI. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 METHODS |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 $task->sendRequest(%params) |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Send a request according to params given. If this is a GET request, params are formatted into URL with _prepareURL method. If this is a POST request, a message must be given in params. This should be a valid JSON message. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
The following parameters are allowed, as keys of the %params hash : |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=over |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item I<url> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
the url to send the message to (mandatory) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item I<method> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
the method used: GET or POST. (mandatory) |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=item I<message> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
the message to send (mandatory if method is POST) |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=back |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
The return value is a response object. See L<HTTP::Request> and L<HTTP::Response> for a description of the interface provided by these classes. |