App-ElasticSearch-Utilities

 view release on metacpan or  search on metacpan

lib/App/ElasticSearch/Utilities/HTTPRequest.pm  view on Meta::CPAN

package App::ElasticSearch::Utilities::HTTPRequest;
# ABSTRACT: Allow for strange content elements for Elasticsearch APIs


use v5.16;
use warnings;
no warnings 'uninitialized';

our $VERSION = '8.8'; # VERSION

use JSON::MaybeXS;
use Ref::Util qw(is_ref is_arrayref is_hashref);

use parent 'HTTP::Request';

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    $self->header('Accept' => 'application/json');

    return $self;
}

sub content {
    my ($self,$body) = @_;

    if( is_arrayref($body) ) {
        # Bulk does this
        my @body;
        foreach my $entry (@{ $body }) {
            push @body, ref $entry ? encode_json($entry) : $entry;
        }
        $body = join '', map { "$_\n" } @body;
        $self->header('Content-Type' => 'application/x-ndjson');
    }
    elsif( is_hashref($body) ) {
        $self->header('Content-Type' => 'application/json');
        $body = encode_json($body);
    }
    elsif( is_ref($body) ) {
        # We can't handle this
        warn sprintf "Invalid reference type '%s' passed to %s, discarding.", ref($body), __PACKAGE__;
        undef($body);
    }

    $self->{_content} = $body if defined $body;

    return $self->{_content};
}

1;

__END__

=pod

=head1 NAME

App::ElasticSearch::Utilities::HTTPRequest - Allow for strange content elements for Elasticsearch APIs

=head1 VERSION

version 8.8

=head1 SYNOPSIS

This subclasses HTTP::Request and handles the B<content()> method invocation
to allow passing content as expected by the Elasticsearch API.  You should not
use this module in your code.

=head1 AUTHOR



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