File Coverage

blib/lib/ArangoDB2/HTTP/LWP.pm
Criterion Covered Total %
statement 28 85 32.9
branch 1 38 2.6
condition 0 12 0.0
subroutine 8 16 50.0
pod 9 9 100.0
total 46 160 28.7


line stmt bran cond sub pod time code
1             package ArangoDB2::HTTP::LWP;
2              
3 21     21   84 use strict;
  21         24  
  21         621  
4 21     21   81 use warnings;
  21         24  
  21         478  
5              
6 21         1503 use base qw(
7             ArangoDB2::HTTP
8 21     21   79 );
  21         21  
9              
10 21     21   125 use Data::Dumper;
  21         37  
  21         930  
11 21     21   94 use JSON::XS;
  21         31  
  21         863  
12 21     21   13655 use LWP::UserAgent;
  21         659101  
  21         699  
13 21     21   174 use Scalar::Util qw(weaken);
  21         39  
  21         15428  
14              
15              
16              
17             my $JSON = JSON::XS->new->utf8;
18              
19              
20              
21             sub new
22             {
23 1     1 1 1 my($class, $arango) = @_;
24             # we do not want to hold this reference if the
25             # parent goes out of scope
26 1         4 weaken $arango;
27             # create new instance
28 1         2 my $self = {
29             arango => $arango,
30             };
31 1         3 bless($self, $class);
32              
33 1         10 $self->{lwp} = LWP::UserAgent->new(
34             keep_alive => 1
35             );
36              
37             # set authentication is username is specified
38 1 50       3550 if ($self->arango->username) {
39 0         0 $self->lwp->default_headers->authorization_basic(
40             $self->arango->username,
41             $self->arango->password,
42             );
43             }
44              
45 1         14 return $self;
46             }
47              
48             # delete
49             #
50             # make a DELETE request using the ArangoDB API uri along with
51             # the path and any args passed
52             sub delete
53             {
54 0     0 1   my($self, $path, $args, $raw) = @_;
55             # get copy of ArangoDB API URI
56 0           my $uri = $self->arango->uri->clone;
57             # set path for request
58 0           $uri->path($path);
59             # set query params on URI if passed
60 0 0         $uri->query_form($args) if $args;
61             # make request
62 0           my $response = $self->lwp->delete($uri);
63             # do not process response if raw requested
64 0 0         return $response if $raw;
65             # process response
66 0           return $self->response($response);
67             }
68              
69             # get
70             #
71             # make a GET request using the ArangoDB API uri along with
72             # the path and any args passed
73             sub get
74             {
75 0     0 1   my($self, $path, $args, $raw) = @_;
76             # get copy of ArangoDB API URI
77 0           my $uri = $self->arango->uri->clone;
78             # set path for request
79 0           $uri->path($path);
80             # set query params on URI if passed
81 0 0         $uri->query_form($args) if $args;
82             # make request
83 0           my $response = $self->lwp->get($uri);
84             # do not process response if raw requested
85 0 0         return $response if $raw;
86             # process response
87 0           return $self->response($response);
88             }
89              
90             # head
91             #
92             # make a HEAD request using the ArangoDB API uri along with
93             # the path and any args passed
94             sub head
95             {
96 0     0 1   my($self, $path, $args, $raw) = @_;
97             # get copy of ArangoDB API URI
98 0           my $uri = $self->arango->uri->clone;
99             # set path for request
100 0           $uri->path($path);
101             # set query params on URI if passed
102 0 0         $uri->query_form($args) if $args;
103             # make request
104 0           my $response = $self->lwp->head($uri);
105             # do not process response if raw requested
106 0 0         return $response if $raw;
107             # return code
108 0           return $response->code;
109             }
110              
111             # lwp
112             #
113             # LWP::UserAgent instance
114 0     0 1   sub lwp { $_[0]->{lwp} }
115              
116             # patch
117             #
118             # make a PATCH request using the ArangoDB API uri along with
119             # the path and any args passed
120             sub patch
121             {
122 0     0 1   my($self, $path, $args, $patch, $raw) = @_;
123             # get copy of ArangoDB API URI
124 0           my $uri = $self->arango->uri->clone;
125             # set path for request
126 0           $uri->path($path);
127             # set query params on URI if passed
128 0 0         $uri->query_form($args) if $args;
129             # build HTTP::Request
130 0           my $request = HTTP::Request->new('PATCH', $uri);
131 0           $request->content($patch);
132             # make request
133 0           my $response = $self->lwp->request($request);
134             # do not process response if raw requested
135 0 0         return $response if $raw;
136             # process response
137 0           return $self->response($response);
138             }
139              
140             # put
141             #
142             # make a PUT request using the ArangoDB API uri along with
143             # the path and any args passed
144             sub put
145             {
146 0     0 1   my($self, $path, $args, $put, $raw) = @_;
147             # get copy of ArangoDB API URI
148 0           my $uri = $self->arango->uri->clone;
149             # set path for request
150 0           $uri->path($path);
151             # set query params on URI if passed
152 0 0         $uri->query_form($args) if $args;
153             # make request
154 0 0         my $response = ref $put
155             # if put is hashref then treat as key/value pairs
156             # to be form encoded
157             ? $self->lwp->put($uri, $put)
158             # if put is string then put raw data
159             : $self->lwp->put($uri, Content => $put);
160             # do not process response if raw requested
161 0 0         return $response if $raw;
162             # process response
163 0           return $self->response($response);
164             }
165              
166             # post
167             #
168             # make a POST request using the ArangoDB API uri along with
169             # the path and any args passed
170             sub post
171             {
172 0     0 1   my($self, $path, $args, $post, $raw) = @_;
173             # get copy of ArangoDB API URI
174 0           my $uri = $self->arango->uri->clone;
175             # set path for request
176 0           $uri->path($path);
177             # set query params on URI if passed
178 0 0         $uri->query_form($args) if $args;
179             # make request
180 0 0         my $response = ref $post
181             # if post is hashref then treat as key/value pairs
182             # to be form encoded
183             ? $self->lwp->post($uri, $post)
184             # if post is string then post raw data
185             : $self->lwp->post($uri, Content => $post);
186             # do not process response if raw requested
187 0 0         return $response if $raw;
188             # process response
189 0           return $self->response($response);
190             }
191              
192             # response
193             #
194             # process LWP::UserAgent response
195             sub response
196             {
197 0     0 1   my($self, $response) = @_;
198              
199 0 0         if ($response->is_success) {
200 0           my $res = eval { $JSON->decode($response->content) };
  0            
201             # if content is not valid JSON then return entire content
202 0 0         return $response->content unless $res;
203             # res may be array in rare cases
204 0 0 0       if (!(ref $res eq 'HASH')) {
    0 0        
      0        
205 0           return $res;
206             }
207             # if there is a result object and no error and this is not a
208             # cursor result then only return the result object
209             elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
210             && !$res->{error} && !defined $res->{hasMore} )
211             {
212 0   0       return $res->{result} || $res->{graph} || $res->{graphs};
213             }
214             # otherwise return entire response
215             else {
216 0           return $res;
217             }
218             }
219             else {
220             # set error code
221 0           $self->error($response->code);
222 0           return;
223             }
224             }
225              
226             1;
227              
228             __END__