ArangoDB2

 view release on metacpan or  search on metacpan

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

    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;
    # make request
    my $response = ref $put
        # if put is hashref then treat as key/value pairs
        # to be form encoded
        ? $self->lwp->put($uri, $put)
        # if put is string then put raw data
        : $self->lwp->put($uri, Content => $put);
    # do not process response if raw requested
    return $response if $raw;
    # process response
    return $self->response($response);
}

# post
#
# make a POST request using the ArangoDB API uri along with
# the path and any args passed
sub post
{
    my($self, $path, $args, $post, $raw) = @_;
    # 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;
    # make request
    my $response = ref $post
        # if post is hashref then treat as key/value pairs
        # to be form encoded
        ? $self->lwp->post($uri, $post)
        # if post is string then post raw data
        : $self->lwp->post($uri, Content => $post);
    # do not process response if raw requested
    return $response if $raw;
    # process response
    return $self->response($response);
}

# response
#
# process LWP::UserAgent response
sub response
{
    my($self, $response) = @_;

    if ($response->is_success) {
        my $res = eval { $JSON->decode($response->content) };
        # if content is not valid JSON then return entire content
        return $response->content 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;
        }
    }
    else {
        # set error code
        $self->error($response->code);
        return;
    }
}

1;

__END__


=head1 NAME

ArangoDB2::HTTP::LWP - ArangoDB HTTP transport layer implemented with LWP

=head1 METHODS

=over 4

=item new

=item delete

=item get

=item head

=item lwp

=item patch

=item put

=item post

=item response

=back

=head1 AUTHOR

Ersun Warncke, C<< <ersun.warncke at outlook.com> >>

http://ersun.warnckes.com

=head1 COPYRIGHT

Copyright (C) 2014 Ersun Warncke



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