ArangoDB2
view release on metacpan or search on metacpan
lib/ArangoDB2/HTTP/Curl.pm view on Meta::CPAN
# do request
my $ret = $curl->perform;
# if raw is specified we build an HTTP::Response to return
return $raw
? HTTP::Response->parse($response)
: $self->curl_response($curl, $ret, \$response);
}
# delete
#
# make a DELETE request using the ArangoDB API uri along with
# the path and any args passed
sub delete
{
my($self, $path, $args, $raw) = @_;
return $self->curl("DELETE", $path, $args, undef, $raw);
}
# get
#
# make a GET request using the ArangoDB API uri along with
# the path and any args passed
sub get
{
my($self, $path, $args, $raw) = @_;
return $self->curl("GET", $path, $args, undef, $raw);
}
# head
#
# make a HEAD request using the ArangoDB API uri along with
# the path and any args passed
sub head
{
my($self, $path, $args, $raw) = @_;
# get HTTP::Response so we can check code
my $res = $self->curl("HEAD", $path, $args, undef, 1);
# if raw was set then return response
return $res if $raw;
# return code if success
return $res->is_success ? $res->code : undef;
}
# patch
#
# make a PATCH request using the ArangoDB API uri along with
# the path and any args passed
sub patch
{
my($self, $path, $args, $data, $raw) = @_;
return $self->curl("PATCH", $path, $args, $data, $raw);
}
# put
#
# make a PUT request using the ArangoDB API uri along with
# the path and any args passed
sub put
{
my($self, $path, $args, $data, $raw) = @_;
return $self->curl("PUT", $path, $args, $data, $raw);
}
# post
#
# make a POST request using the ArangoDB API uri along with
# the path and any args passed
sub post
{
my($self, $path, $args, $data, $raw) = @_;
return $self->curl("POST", $path, $args, $data, $raw);
}
# curl_response
#
# process a curl response
sub curl_response
{
my($self, $curl, $ret, $response) = @_;
# a curl error occured
if ($ret != 0) {
$self->error($curl->strerror($ret)." ".$curl->errbuf);
return;
}
# get http response code
my $code = $curl->getinfo(CURLINFO_HTTP_CODE);
# require code in 200 range for success
if (!($code >= 200 && $code < 300)) {
$self->error($code);
return;
}
# decode response, assuming JSON
my $res = eval { $JSON->decode($$response) };
# if content is not valid JSON then return entire content
return $$response unless $res;
# res may be array in rare cases
if (!(ref $res eq 'HASH')) {
return $res;
}
# if there is a result object and no error and this is not a
# cursor result then only return the result object
elsif ( ($res->{result} || $res->{graph} || $res->{graphs})
&& !$res->{error} && !defined $res->{hasMore} )
{
return $res->{result} || $res->{graph} || $res->{graphs};
}
# otherwise return entire response
else {
return $res;
}
}
1;
__END__
=head1 NAME
ArangoDB2::HTTP::LWP - ArangoDB HTTP transport layer implemented with LWP
( run in 1.402 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )