App-ElasticSearch-Utilities
view release on metacpan or search on metacpan
lib/App/ElasticSearch/Utilities/Connection.pm view on Meta::CPAN
grep { defined && length && !/^\s+$/ }
split /\r?\n/, $response->content
];
debug_var($decoded);
$response->content($decoded);
}
if( my $content = $response->content ) {
debug({color=>'yellow'}, "After translation:");
if( is_ref($content) ) {
debug_var( $content );
}
else{
debug( $content );
}
}
$_[0] = $response;
});
# Warn About Basic Auth without TLS
warn "HTTP Basic Authorization configured and not using TLS, this is not supported"
if length $self->password && $self->proto ne 'https';
return $ua;
}
sub request {
my ($self,$url,$options,$body) = @_;
# Build the Path
$options->{command} ||= $url;
my @path = grep { defined and length } @{ $options }{qw(index command)};
my $path = join('/', @path);
debug(sprintf "calling %s->request(%s)", ref $self, $path);
# Build a URI
my $uri = URI->new( sprintf "%s://%s:%d",
$self->proto,
$self->host,
$self->port,
);
$uri->path($path);
# Query String
if( exists $options->{uri_param} and is_hashref($options->{uri_param}) ) {
foreach my $k ( keys %{ $options->{uri_param} } ) {
$uri->query_param( $k => $options->{uri_param}{$k} );
}
}
# Body Translations
if(!defined $body && exists $options->{body}) {
$body ||= delete $options->{body};
}
# Determine request method
my $method = exists $options->{method} ? uc $options->{method} : 'GET';
# Special Case for Index Creation
if( $method eq 'PUT' && $options->{index} && $options->{command} eq '/' ) {
$uri->path($options->{index});
}
debug({color=>'magenta'}, sprintf "Issuing %s with URI of '%s'", $method, $uri->as_string);
if( defined $body ) {
if( is_ref($body) ) {
debug_var({indent=>1}, $body);
}
else {
debug({indent=>1}, split /\r?\n/, $body);
}
}
# Make the request
my $req = App::ElasticSearch::Utilities::HTTPRequest->new( $method => $uri->as_string );
# Authentication
$req->authorization_basic( $self->username, $self->password )
if length $self->password and $self->proto eq 'https';
$req->content($body) if defined $body;
return $self->ua->request( $req );
}
sub exists {
my ($self,%options) = @_;
return unless exists $options{index};
my %params = (
method => 'HEAD',
index => $options{index},
);
return $self->request('', \%params,)->is_success;
}
sub put {
my ($self,%options) = @_;
return unless exists $options{body};
my %params = ( method => 'PUT' );
$params{index} = $options{index} if exists $options{index};
my $resp = $self->request('', \%params, $options{body});
return ( $resp->code, $resp->content );
}
sub bulk {
my ($self,%options) = @_;
return unless exists $options{body};
my %params = ( method => 'POST' );
$params{index} = $options{index} if exists $options{index};
my $resp = $self->request( '_bulk', \%params, $options{body} );
return ( $resp->code, $resp->content );
}
__PACKAGE__->meta->make_immutable;
__END__
=pod
=head1 NAME
App::ElasticSearch::Utilities::Connection - Abstract the connection element
=head1 VERSION
version 8.9
=head1 SYNOPSIS
For most users, this code will never be called directly since this module
doesn't handle parameter parsing on the CLI. To get an object, instead call:
use App::ElasticSearch::Utilities qw(es_connect);
my $es = es_connect();
my $http_response_obj = $es->request('_search',
{
index=>'logstash',
uri_param => {
size => 10,
}
},
{
query => {
query_string => "program:sshd",
}
}
);
Though even this is overkill. The B<es_request> method maintains compatability with older versions and emulates
the API you'd expect from B<Elastijk>.
=head1 ATTRIBUTES
( run in 0.845 second using v1.01-cache-2.11-cpan-437f7b0c052 )