ArangoDB
view release on metacpan or search on metacpan
lib/ArangoDB/Connection.pm view on Meta::CPAN
}
my %furl_args = (
timeout => $opts->timeout,
headers => $headers,
proxy => $opts->proxy,
);
if( $opts->inet_aton ){
$furl_args{inet_aton} = $opts->inet_aton;
}
my $furl = Furl::HTTP->new(%furl_args);
my $self = bless {
options => $opts,
_req_args => {
scheme => 'http',
host => $opts->host,
port => $opts->port,
},
_http_agent => $furl,
}, $class;
return $self;
}
sub http_get {
my ( $self, $path, $additional_headers ) = @_;
my $headers = $self->_build_headers(undef, $additional_headers );
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'GET',
path_query => $path,
headers => $headers,
);
return $self->_parse_response( $code, $msg, $body );
}
sub http_post {
my ( $self, $path, $data, $raw, $additional_headers ) = @_;
if( !$raw ){
$data = $JSON->encode( defined $data ? $data : {} );
}
my $headers = $self->_build_headers($data, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'POST',
path_query => $path,
headers => $headers,
content => $data,
);
return $self->_parse_response( $code, $msg, $body );
}
sub http_put {
my ( $self, $path, $data, $raw, $additional_headers ) = @_;
if( !$raw ){
$data = $JSON->encode( defined $data ? $data : {} );
}
my $headers = $self->_build_headers($data, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'PUT',
path_query => $path,
headers => $headers,
content => $data,
);
return $self->_parse_response( $code, $msg, $body );
}
sub http_delete {
my ( $self, $path, $additional_headers ) = @_;
my $headers = $self->_build_headers(undef, $additional_headers);
my ( undef, $code, $msg, undef, $body ) = $self->{_http_agent}->request(
%{ $self->{_req_args} },
method => 'DELETE',
path_query => $path,
headers => $headers,
);
return $self->_parse_response( $code, $msg, $body );
}
sub _build_headers {
my ( $self, $body, $additional_headers ) = @_;
my $content_length = length( $body || q{} );
my @headers = ();
if ( $content_length > 0 ) {
push @headers, 'Content-Type' => 'application/json';
}
if( $additional_headers ){
push @headers, @{ $additional_headers };
}
return \@headers;
}
sub _parse_response {
my ( $self, $code, $status, $body ) = @_;
if ( $code < 200 || $code >= 400 ) {
if ( $body ne q{} ) {
my $details = $JSON->decode($body);
my $exception = ArangoDB::ServerException->new( code => $code, status => $status, detail => $details );
die $exception;
}
die ArangoDB::ServerException->new( code => $code, status => $status, detail => {} );
}
my $data = $JSON->decode($body);
return $data;
}
1;
__END__
( run in 0.529 second using v1.01-cache-2.11-cpan-d8267643d1d )