line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OVH::OvhApi::Answer; |
2
|
|
|
|
|
|
|
|
3
|
36
|
|
|
36
|
|
243
|
use strict; |
|
36
|
|
|
|
|
79
|
|
|
36
|
|
|
|
|
985
|
|
4
|
36
|
|
|
36
|
|
176
|
use warnings; |
|
36
|
|
|
|
|
84
|
|
|
36
|
|
|
|
|
2419
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 0.48; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use overload ( |
10
|
36
|
|
|
|
|
308
|
bool => \&isSuccess, |
11
|
|
|
|
|
|
|
'!' => \&isFailure, |
12
|
|
|
|
|
|
|
fallback => 0, |
13
|
36
|
|
|
36
|
|
43398
|
); |
|
36
|
|
|
|
|
36390
|
|
14
|
|
|
|
|
|
|
|
15
|
36
|
|
|
36
|
|
3293
|
use Scalar::Util 'blessed'; |
|
36
|
|
|
|
|
82
|
|
|
36
|
|
|
|
|
1950
|
|
16
|
36
|
|
|
36
|
|
232
|
use Carp qw{ carp croak }; |
|
36
|
|
|
|
|
75
|
|
|
36
|
|
|
|
|
2038
|
|
17
|
36
|
|
|
36
|
|
23264
|
use JSON (); |
|
36
|
|
|
|
|
369364
|
|
|
36
|
|
|
|
|
12707
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
22
|
|
|
|
|
|
|
# Class variables |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $Json = JSON->new->allow_nonref; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# End - Class variables |
27
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
32
|
|
|
|
|
|
|
# Class methods |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub new |
35
|
|
|
|
|
|
|
{ |
36
|
0
|
|
|
0
|
0
|
|
my ($class, %params) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
unless ($params{'response'}) |
39
|
|
|
|
|
|
|
{ |
40
|
0
|
|
|
|
|
|
croak 'Missing parameter: response'; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
0
|
|
|
|
unless (blessed $params{'response'} and $params{'response'}->isa('HTTP::Response')) |
44
|
|
|
|
|
|
|
{ |
45
|
0
|
|
|
|
|
|
croak 'Invalid parameter: reponse'; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
bless { response => $params{'response'} }, $class; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# End - Class methods |
52
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
57
|
|
|
|
|
|
|
# Instance methods |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub isSuccess |
60
|
|
|
|
|
|
|
{ |
61
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
return $self->{'response'}->is_success; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub isFailure |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return not $self->isSuccess; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub content |
75
|
|
|
|
|
|
|
{ |
76
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
if ($self->isFailure) |
79
|
|
|
|
|
|
|
{ |
80
|
0
|
|
|
|
|
|
carp 'Fetching content from a failed OvhApi::Response Object'; |
81
|
0
|
|
|
|
|
|
return; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
return $self->_generateContent; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub error |
88
|
|
|
|
|
|
|
{ |
89
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
90
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
return $self ? '' : $self->_generateContent->{'message'}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
95
|
|
|
|
|
|
|
# private part |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _generateContent |
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $content; |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
0
|
|
|
|
if ($self->{'response'}->header('Client-Warning') and $self->{'response'}->header('Client-Warning') eq 'Internal response') |
104
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
|
|
|
return { message => 'Internal LWP::UserAgent error : ' . $self->{'response'}->content }; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
eval { $content = $Json->decode($self->{'response'}->content); 1; } or do { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
carp 'Failed to parse JSON content from the answer: ', $self->{'response'}->content; |
110
|
0
|
|
|
|
|
|
return; |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
return $content; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# End - Instance methods |
117
|
|
|
|
|
|
|
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
return 42; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 NAME |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
OvhApi::Answer - Response to a request run with C<OvhApi>. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 SYNOPSIS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $Answer = $Api->get(path => '/me'); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
if ($Answer) |
133
|
|
|
|
|
|
|
{ |
134
|
|
|
|
|
|
|
# Success: can fetch content and process |
135
|
|
|
|
|
|
|
my $content = $Answer->content; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
else |
138
|
|
|
|
|
|
|
{ |
139
|
|
|
|
|
|
|
# Request failed: stop here and retrieve the error |
140
|
|
|
|
|
|
|
my $error = $Answer->error; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This module represents a response to a query run with C<OvhApi>. It is build upon a C<HTTP::Request> object. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 CLASS METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 Constructor |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
There is only one constructor: C<new>. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Its parameters are: |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Parameter Mandatory Default Usage |
156
|
|
|
|
|
|
|
------------ ------------ ---------- -------- |
157
|
|
|
|
|
|
|
response Yes - An HTTP::Response object return by LWP::UserAgent |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 INSTANCE METHODS |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 content |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Returns the content of the answer. This method will C<carp> if the answer is an error. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
It takes no parameter. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 error |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Returns the error message of the answer, or an empty string if the answer is a success. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
It takes no parameter. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 isSuccess |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Forwards a call to C<HTTP::Response::is_error> in the inner C<HTTP::Response> of the answer. Returns true is the request was a success, false otherwise. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
It takes no parameter. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This method is used for the C<bool> L<overload|overload>. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 isFailure |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Helper method which returns the boolean negation of L<isSuccess|/isSuccess>. |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
It takes no parameter. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 SEE ALSO |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
The guts of module are using: C<JSON>. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 COPYRIGHT |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Copyright (c) 2013, OVH SAS. |
194
|
|
|
|
|
|
|
All rights reserved. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
This library is distributed under the terms of C<license.txt>. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|