line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Opsview::REST::APICaller; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Opsview::REST::APICaller::VERSION = '0.013'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
9
|
|
|
9
|
|
7051
|
use Moo::Role; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
64
|
|
7
|
|
|
|
|
|
|
|
8
|
9
|
|
|
9
|
|
3119
|
use Carp; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
750
|
|
9
|
|
|
|
|
|
|
|
10
|
9
|
|
|
9
|
|
9528
|
use Try::Tiny; |
|
9
|
|
|
|
|
15814
|
|
|
9
|
|
|
|
|
550
|
|
11
|
9
|
|
|
9
|
|
10139
|
use JSON (); |
|
9
|
|
|
|
|
151602
|
|
|
9
|
|
|
|
|
295
|
|
12
|
9
|
|
|
9
|
|
25106
|
use HTTP::Tiny 0.014; |
|
9
|
|
|
|
|
580194
|
|
|
9
|
|
|
|
|
15381
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has ua => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
default => sub { HTTP::Tiny->new( |
17
|
|
|
|
|
|
|
agent => 'Opsview::REST/' . (__PACKAGE__->VERSION || '0.001_DEV'), |
18
|
|
|
|
|
|
|
); }, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has headers => ( |
22
|
|
|
|
|
|
|
is => 'rw', |
23
|
|
|
|
|
|
|
default => sub { { |
24
|
|
|
|
|
|
|
'Accept' => 'application/json', |
25
|
|
|
|
|
|
|
'Content-type' => 'application/json', |
26
|
|
|
|
|
|
|
}; }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has json => ( |
30
|
|
|
|
|
|
|
is => 'ro', |
31
|
|
|
|
|
|
|
default => sub { JSON->new }, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub get { |
35
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
36
|
0
|
|
|
|
|
|
my $r = $self->ua->get($self->base_url . shift, { |
37
|
|
|
|
|
|
|
headers => $self->headers, |
38
|
|
|
|
|
|
|
}); |
39
|
0
|
0
|
|
|
|
|
croak $self->_errmsg($r) unless $r->{success}; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
return $self->json->decode($r->{content}); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub delete { |
45
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
46
|
0
|
|
|
|
|
|
my $r = $self->ua->delete($self->base_url . shift, { |
47
|
|
|
|
|
|
|
headers => $self->headers, |
48
|
|
|
|
|
|
|
}); |
49
|
0
|
0
|
|
|
|
|
croak $self->_errmsg($r) unless $r->{success}; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
return $self->json->decode($r->{content}); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub post { |
55
|
0
|
|
|
0
|
0
|
|
my ($self, $method, $data) = @_; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my $stuff = { headers => $self->headers }; |
58
|
0
|
0
|
|
|
|
|
if (defined $data) { |
59
|
0
|
|
|
|
|
|
$stuff->{content} = $self->json->encode($data); |
60
|
|
|
|
|
|
|
} else { |
61
|
0
|
|
|
|
|
|
$stuff->{headers}->{'Content-Length'} = '0'; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $r = $self->ua->post($self->base_url . $method, $stuff); |
65
|
0
|
0
|
|
|
|
|
croak $self->_errmsg($r) unless $r->{success}; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
return $self->json->decode($r->{content}); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub put { |
71
|
0
|
|
|
0
|
0
|
|
my ($self, $method, $data) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $stuff = { headers => $self->headers }; |
74
|
0
|
0
|
|
|
|
|
$stuff->{content} = $self->json->encode($data) if defined $data; |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
my $r = $self->ua->put($self->base_url . $method, $stuff); |
77
|
0
|
0
|
|
|
|
|
croak $self->_errmsg($r) unless $r->{success}; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return $self->json->decode($r->{content}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _errmsg { |
83
|
0
|
|
|
0
|
|
|
my ($self, $r) = @_; |
84
|
0
|
|
|
|
|
|
my $exc = { |
85
|
|
|
|
|
|
|
status => $r->{status}, |
86
|
|
|
|
|
|
|
reason => $r->{reason}, |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
if ($r->{content}) { |
90
|
|
|
|
|
|
|
try { |
91
|
0
|
|
|
0
|
|
|
my $resp; |
92
|
0
|
|
|
|
|
|
$resp = $self->json->decode($r->{content}); |
93
|
0
|
|
0
|
|
|
|
$exc->{message} = $resp->{message} || undef; |
94
|
0
|
|
0
|
|
|
|
$exc->{detail} = $resp->{detail} || undef; |
95
|
|
|
|
|
|
|
} catch { |
96
|
0
|
|
|
0
|
|
|
$exc->{message} = $r->{content}; |
97
|
0
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
return Opsview::REST::Exception->new($exc); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
__END__ |