ArangoDB2

 view release on metacpan or  search on metacpan

lib/ArangoDB2/HTTP/Curl.pm  view on Meta::CPAN

package ArangoDB2::HTTP::Curl;

use strict;
use warnings;

use base qw(
    ArangoDB2::HTTP
);

use Data::Dumper;
use HTTP::Response;
use JSON::XS;
use Scalar::Util qw(weaken);
use WWW::Curl::Easy;



my $JSON = JSON::XS->new->utf8;



sub new
{
    my($class, $arango) = @_;

    # we do not want to hold this reference if the
    # parent goes out of scope
    weaken $arango;

    # create new instance
    my $self = {
        arango => $arango,
    };
    bless($self, $class);

    # setup curl
    my $curl = $self->{curl} = WWW::Curl::Easy->new;
    # set authentication is username is specified
    if ($self->arango->username) {
        $curl->setopt(CURLOPT_USERNAME, $self->arango->username);
        $curl->setopt(CURLOPT_PASSWORD, $self->arango->password);
        $curl->setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    }

    return $self;
}

# curl
#
# return new Curl client instance
sub curl
{
    my($self, $method, $path, $args, $data, $raw) = @_;

    my $curl = $self->{curl};

    # get copy of ArangoDB API URI
    my $uri = $self->arango->uri->clone;
    # set path for request
    $uri->path($path);
    # set query params on URI if passed
    $uri->query_form($args) if $args;

    # response body
    my $response;
    # setup curl request
    $curl->setopt(CURLOPT_URL, $uri->as_string);
    $curl->setopt(CURLOPT_CUSTOMREQUEST, $method);
    $curl->setopt(CURLOPT_WRITEDATA, \$response);
    # add post data if set
    if ($data && length $data) {
        $curl->setopt(CURLOPT_POSTFIELDS, $data);
        $curl->setopt(CURLOPT_POSTFIELDSIZE, length $data);
    }
    else {
        $curl->setopt(CURLOPT_POSTFIELDS, '');
        $curl->setopt(CURLOPT_POSTFIELDSIZE, 0);
    }



( run in 1.353 second using v1.01-cache-2.11-cpan-39bf76dae61 )