File Coverage

blib/lib/ArangoDB2/HTTP/LWP.pm
Criterion Covered Total %
statement 26 80 32.5
branch 0 34 0.0
condition 0 12 0.0
subroutine 8 16 50.0
pod 9 9 100.0
total 43 151 28.4


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