Gett/Request.pm

filter: Statementstmt 35.7% 25 / 70 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.1% 7 / 17 Podpod 100% 3 / 3 totaltotal 33.6% 35 / 104 CC 8 SCAR 21.4
CC
8
Cov
30%
_send
30.0
__ANON__
6.0
put
6.0
CRAP 8.5
« Gett/Share.pm ↑ Summary
1 package Net::API::Gett::Request;
2  
3 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 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<https://open.ge.tt/1>.
33  
34 =back
35  
36 =cut
37  
38 has 'base_url' => (
39 is => 'ro',
40 0 ▶ default => sub { 'https://open.ge.tt/1' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 5 );
42  
43 =over
44  
45 =item ua
46  
47 User agent object. Read only. Populated at object construction. Uses a default L<LWP::UserAgent>.
48  
49 =back
50  
51 =cut
52  
53 has 'ua' => (
54 is => 'ro',
55 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 my $ua = LWP::UserAgent->new();
57 0 $ua->agent("Net-API-Gett/$VERSION/(Perl)");
58 0 return $ua;
59 },
60 0 ▶ isa => sub { die "$_[0] is not LWP::UserAgent" unless ref($_[0])=~/UserAgent/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /UserAgent/ 0 0 0
61 5 );
62  
63  
64 0 ▶sub _encode {
Subroutine _encode: not called CC=1 SCAR=6.9
65 0 my $self = shift;
66 0 my $hr = shift;
67  
68 0 return encode_json($hr);
69 }
70  
71 0 ▶sub _decode {
Subroutine _decode: not called CC=1 SCAR=6.9
72 0 my $self = shift;
73 0 my $json = shift;
74  
75 0 return decode_json($json);
76 }
77  
78 0 ▶sub _send {
Subroutine _send: not called CC=5 SCAR=34.0
79 0 my $self = shift;
80 0 my $method = uc shift;
81 0 my $endpoint = shift;
82 0 my $data = shift;
83  
84 0 my $url = $self->base_url . $endpoint;
85  
86 0 my $req;
87 0 ▶ if ( $method eq "POST" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'POST') { } 0 0 0
elsif ($method eq 'GET') { } 0 0 0
88 0 ▶ if ( ref($data) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 0 0 0
89 0 $data = $self->_encode($data);
90 }
91  
92 0 $req = POST $url, Content => $data;
93 }
94 elsif ( $method eq "GET" ) {
95 0 $req = GET $url;
96 }
97 else {
98 0 croak "$method is not supported.";
99 }
100  
101 0 my $response = $self->ua->request($req);
102  
103 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
104 0 return $self->_decode($response->content());
105 }
106 else {
107 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 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
142 0 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 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
178 0 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 0 ▶sub put {
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
218 0 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 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
234 0 my $ret = read($fh, my $chunk, $chunk_size);
235 0 ▶ return $ret ? $chunk : ();
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 0 0
236 },
237 0 );
238  
239 0 my $response = $self->ua->request($req);
240  
241 0 close $fh;
242
243 0 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
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<Net::API::Gett>
254  
255 =cut
256  
257 5 1;
« Gett/Share.pm ↑ Summary