line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::API::Gett::Request; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
30
|
use Moo; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
40
|
|
4
|
5
|
|
|
5
|
|
6276
|
use Sub::Quote; |
|
5
|
|
|
|
|
21630
|
|
|
5
|
|
|
|
|
377
|
|
5
|
5
|
|
|
5
|
|
43
|
use Carp qw(croak); |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
232
|
|
6
|
5
|
|
|
5
|
|
6308
|
use JSON; |
|
5
|
|
|
|
|
100380
|
|
|
5
|
|
|
|
|
32
|
|
7
|
5
|
|
|
5
|
|
7089
|
use LWP::UserAgent; |
|
5
|
|
|
|
|
415056
|
|
|
5
|
|
|
|
|
222
|
|
8
|
5
|
|
|
5
|
|
33394
|
use HTTP::Request::Common; |
|
5
|
|
|
|
|
13145
|
|
|
5
|
|
|
|
|
493
|
|
9
|
5
|
|
|
5
|
|
42
|
use HTTP::Headers; |
|
5
|
|
|
|
|
12
|
|
|
5
|
|
|
|
|
3869
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '1.06'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Net::API::Gett::Request - Gett Request object |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 PURPOSE |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
This object encapsulates requests to and from the Gett API server. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
You normally shouldn't instanstiate this class on its own as the library |
22
|
|
|
|
|
|
|
will create and return this object when appropriate. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
These are read only attributes. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item base_url |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Scalar string. Read-only. Populated at object construction. Default value: L. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=back |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=cut |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has 'base_url' => ( |
39
|
|
|
|
|
|
|
is => 'ro', |
40
|
|
|
|
|
|
|
default => sub { 'https://open.ge.tt/1' }, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=over |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=item ua |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
User agent object. Read only. Populated at object construction. Uses a default L. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=back |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=cut |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has 'ua' => ( |
54
|
|
|
|
|
|
|
is => 'ro', |
55
|
|
|
|
|
|
|
default => sub { |
56
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
57
|
|
|
|
|
|
|
$ua->agent("Net-API-Gett/$VERSION/(Perl)"); |
58
|
|
|
|
|
|
|
return $ua; |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
isa => sub { die "$_[0] is not LWP::UserAgent" unless ref($_[0])=~/UserAgent/ }, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _encode { |
65
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
66
|
0
|
|
|
|
|
0
|
my $hr = shift; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
return encode_json($hr); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub _decode { |
72
|
3
|
|
|
3
|
|
63
|
my $self = shift; |
73
|
3
|
|
|
|
|
12
|
my $json = shift; |
74
|
|
|
|
|
|
|
|
75
|
3
|
|
|
|
|
1290
|
return decode_json($json); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _send { |
79
|
3
|
|
|
3
|
|
7
|
my $self = shift; |
80
|
3
|
|
|
|
|
11
|
my $method = uc shift; |
81
|
3
|
|
|
|
|
10
|
my $endpoint = shift; |
82
|
3
|
|
|
|
|
5
|
my $data = shift; |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
|
|
70
|
my $url = $self->base_url . $endpoint; |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
8
|
my $req; |
87
|
3
|
50
|
|
|
|
20
|
if ( $method eq "POST" ) { |
|
|
50
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
0
|
if ( ref($data) eq "HASH" ) { |
89
|
0
|
|
|
|
|
0
|
$data = $self->_encode($data); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
0
|
$req = POST $url, Content => $data; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif ( $method eq "GET" ) { |
95
|
3
|
|
|
|
|
21
|
$req = GET $url; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
else { |
98
|
0
|
|
|
|
|
0
|
croak "$method is not supported."; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
3
|
|
|
|
|
31127
|
my $response = $self->ua->request($req); |
102
|
|
|
|
|
|
|
|
103
|
3
|
50
|
|
|
|
1074399
|
if ( $response->is_success ) { |
104
|
3
|
|
|
|
|
62
|
return $self->_decode($response->content()); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
0
|
|
|
|
|
0
|
croak "$method $url said " . $response->status_line; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 METHODS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=over |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item get() |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This method uses the GET HTTP verb to fetch data from the Gett service. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Input: |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=over |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item * endpoint fragment |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=back |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Output: |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * Perl hash ref of the JSON response from the API |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=back |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Gives a fatal error under any error condition. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=back |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=cut |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub get { |
142
|
3
|
|
|
3
|
1
|
57
|
shift->_send('GET', @_); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=over |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item post() |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
This method uses the POST HTTP verb to send or fetch data to/from the Gett service. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Input: |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item * endpoint fragment |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item * data (as a string or Perl hashref) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=back |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
If the data is a Perl hashref, it will be automatically encoded as JSON. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Output: |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=over |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item * Perl hash ref of the JSON response from the API |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=back |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This method will die under any error condition. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=back |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub post { |
178
|
0
|
|
|
0
|
1
|
|
shift->_send('POST', @_); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item put() |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
This method uses the PUT HTTP verb to send data to the Gett service. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Input: |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=over |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item * Full endpoint |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item * Data filehandle |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item * A chunksize |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=item * the length of the data in bytes |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
No automatic encoding is done this data. It is passed "as is" to the remote API. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Output: |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=over |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item * A true value |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=back |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
This method will die under any error condition. |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=back |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub put { |
218
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
219
|
0
|
|
|
|
|
|
my $url = shift; |
220
|
0
|
|
|
|
|
|
my $fh = shift; |
221
|
0
|
|
|
|
|
|
my $chunk_size = shift; |
222
|
0
|
|
|
|
|
|
my $length = shift; |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
local $HTTP::Request::Common::DYNAMIC_FILE_UPLOAD = 1; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
my $header = HTTP::Headers->new; |
227
|
0
|
|
|
|
|
|
$header->content_length($length); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
my $req = HTTP::Request->new( |
230
|
|
|
|
|
|
|
'PUT', |
231
|
|
|
|
|
|
|
$url, |
232
|
|
|
|
|
|
|
$header, |
233
|
|
|
|
|
|
|
sub { |
234
|
0
|
|
|
0
|
|
|
my $ret = read($fh, my $chunk, $chunk_size); |
235
|
0
|
0
|
|
|
|
|
return $ret ? $chunk : (); |
236
|
|
|
|
|
|
|
}, |
237
|
0
|
|
|
|
|
|
); |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
my $response = $self->ua->request($req); |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
|
|
|
close $fh; |
242
|
|
|
|
|
|
|
|
243
|
0
|
0
|
|
|
|
|
if ( $response->is_success ) { |
244
|
0
|
|
|
|
|
|
return 1; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
else { |
247
|
0
|
|
|
|
|
|
croak "$url said " . $response->status_line; |
248
|
|
|
|
|
|
|
} |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 SEE ALSO |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
L |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
1; |