ArangoDB2/HTTP/Curl.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ArangoDB2/Transaction.pm ↑ Summary
1 package ArangoDB2::HTTP::Curl;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 ArangoDB2::HTTP
8 1 );
9  
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use WWW::Curl::Easy;
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16  
17  
18 my $JSON = JSON::XS->new->utf8;
19  
20  
21  
22 sub new
23 {
24 my($class, $arango) = @_;
25  
26 # we do not want to hold this reference if the
27 # parent goes out of scope
28 weaken $arango;
29  
30 # create new instance
31 my $self = {
32 arango => $arango,
33 };
34 bless($self, $class);
35  
36 # setup curl
37 my $curl = $self->{curl} = WWW::Curl::Easy->new;
38 # set authentication is username is specified
39 if ($self->arango->username) {
40 $curl->setopt(CURLOPT_USERNAME, $self->arango->username);
41 $curl->setopt(CURLOPT_PASSWORD, $self->arango->password);
42 $curl->setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
43 }
44  
45 return $self;
46 }
47  
48 # curl
49 #
50 # return new Curl client instance
51 sub curl
52 {
53 my($self, $method, $path, $args, $data, $raw) = @_;
54  
55 my $curl = $self->{curl};
56  
57 # get copy of ArangoDB API URI
58 my $uri = $self->arango->uri->clone;
59 # set path for request
60 $uri->path($path);
61 # set query params on URI if passed
62 $uri->query_form($args) if $args;
63  
64 # response body
65 my $response;
66 # setup curl request
67 $curl->setopt(CURLOPT_URL, $uri->as_string);
68 $curl->setopt(CURLOPT_CUSTOMREQUEST, $method);
69 $curl->setopt(CURLOPT_WRITEDATA, \$response);
70 # add post data if set
71 if ($data && length $data) {
72 $curl->setopt(CURLOPT_POSTFIELDS, $data);
73 $curl->setopt(CURLOPT_POSTFIELDSIZE, length $data);
74 }
75 else {
76 $curl->setopt(CURLOPT_POSTFIELDS, '');
77 $curl->setopt(CURLOPT_POSTFIELDSIZE, 0);
78 }
79 # do not get body on HEAD request
80 if ($method eq 'HEAD') {
81 $curl->setopt(CURLOPT_NOBODY, 1);
82 }
83 else {
84 $curl->setopt(CURLOPT_NOBODY, 0);
85 }
86 # combined headers and body for parsing by HTTP::Response
87 if ($raw) {
88 $curl->setopt(CURLOPT_HEADER, 1)
89 }
90 else {
91 $curl->setopt(CURLOPT_HEADER, 0)
92 }
93  
94 # do request
95 my $ret = $curl->perform;
96  
97 # if raw is specified we build an HTTP::Response to return
98 return $raw
99 ? HTTP::Response->parse($response)
100 : $self->curl_response($curl, $ret, \$response);
101 }
102  
103 # delete
104 #
105 # make a DELETE request using the ArangoDB API uri along with
106 # the path and any args passed
107 sub delete
108 {
109 my($self, $path, $args, $raw) = @_;
110  
111 return $self->curl("DELETE", $path, $args, undef, $raw);
112 }
113  
114 # get
115 #
116 # make a GET request using the ArangoDB API uri along with
117 # the path and any args passed
118 sub get
119 {
120 my($self, $path, $args, $raw) = @_;
121  
122 return $self->curl("GET", $path, $args, undef, $raw);
123 }
124  
125 # head
126 #
127 # make a HEAD request using the ArangoDB API uri along with
128 # the path and any args passed
129 sub head
130 {
131 my($self, $path, $args, $raw) = @_;
132 # get HTTP::Response so we can check code
133 my $res = $self->curl("HEAD", $path, $args, undef, 1);
134 # if raw was set then return response
135 return $res if $raw;
136 # return code if success
137 return $res->is_success ? $res->code : undef;
138 }
139  
140 # patch
141 #
142 # make a PATCH request using the ArangoDB API uri along with
143 # the path and any args passed
144 sub patch
145 {
146 my($self, $path, $args, $data, $raw) = @_;
147  
148 return $self->curl("PATCH", $path, $args, $data, $raw);
149 }
150  
151 # put
152 #
153 # make a PUT request using the ArangoDB API uri along with
154 # the path and any args passed
155 sub put
156 {
157 my($self, $path, $args, $data, $raw) = @_;
158  
159 return $self->curl("PUT", $path, $args, $data, $raw);
160 }
161  
162 # post
163 #
164 # make a POST request using the ArangoDB API uri along with
165 # the path and any args passed
166 sub post
167 {
168 my($self, $path, $args, $data, $raw) = @_;
169  
170 return $self->curl("POST", $path, $args, $data, $raw);
171 }
172  
173 # curl_response
174 #
175 # process a curl response
176 sub curl_response
177 {
178 my($self, $curl, $ret, $response) = @_;
179 # a curl error occured
180 if ($ret != 0) {
181 $self->error($curl->strerror($ret)." ".$curl->errbuf);
182 return;
183 }
184 # get http response code
185 my $code = $curl->getinfo(CURLINFO_HTTP_CODE);
186 # require code in 200 range for success
187 if (!($code >= 200 && $code < 300)) {
188 $self->error($code);
189 return;
190 }
191 # decode response, assuming JSON
192 my $res = eval { $JSON->decode($$response) };
193 # if content is not valid JSON then return entire content
194 return $$response unless $res;
195 # res may be array in rare cases
196 if (!(ref $res eq 'HASH')) {
197 return $res;
198 }
199 # if there is a result object and no error and this is not a
200 # cursor result then only return the result object
201 elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
202 && !$res->{error} && !defined $res->{hasMore} )
203 {
204 return $res->{result} || $res->{graph} || $res->{graphs};
205 }
206 # otherwise return entire response
207 else {
208 return $res;
209 }
210 }
211  
212 1;
213  
214 __END__
« ArangoDB2/Transaction.pm ↑ Summary