| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::ElasticSearch::Utilities::HTTPRequest; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Allow for strange content elements for Elasticsearch APIs | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 4 |  |  | 4 |  | 49 | use v5.16; | 
|  | 4 |  |  |  |  | 29 |  | 
| 6 | 4 |  |  | 4 |  | 22 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 120 |  | 
| 7 | 4 |  |  | 4 |  | 22 | no warnings 'uninitialized'; | 
|  | 4 |  |  |  |  | 19 |  | 
|  | 4 |  |  |  |  | 216 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '8.6'; # VERSION | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 4 |  |  | 4 |  | 1897 | use JSON::MaybeXS; | 
|  | 4 |  |  |  |  | 38683 |  | 
|  | 4 |  |  |  |  | 254 |  | 
| 12 | 4 |  |  | 4 |  | 2024 | use Ref::Util qw(is_ref is_arrayref is_hashref); | 
|  | 4 |  |  |  |  | 6535 |  | 
|  | 4 |  |  |  |  | 333 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 4 |  |  | 4 |  | 1872 | use parent 'HTTP::Request'; | 
|  | 4 |  |  |  |  | 1326 |  | 
|  | 4 |  |  |  |  | 25 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub new { | 
| 17 | 0 |  |  | 0 | 1 |  | my $class = shift; | 
| 18 | 0 |  |  |  |  |  | my $self = $class->SUPER::new(@_); | 
| 19 | 0 |  |  |  |  |  | $self->header('Accept' => 'application/json'); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 0 |  |  |  |  |  | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub content { | 
| 25 | 0 |  |  | 0 | 1 |  | my ($self,$body) = @_; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 0 | 0 |  |  |  |  | if( is_arrayref($body) ) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Bulk does this | 
| 29 | 0 |  |  |  |  |  | my @body; | 
| 30 | 0 |  |  |  |  |  | foreach my $entry (@{ $body }) { | 
|  | 0 |  |  |  |  |  |  | 
| 31 | 0 | 0 |  |  |  |  | push @body, ref $entry ? encode_json($entry) : $entry; | 
| 32 |  |  |  |  |  |  | } | 
| 33 | 0 |  |  |  |  |  | $body = join '', map { "$_\n" } @body; | 
|  | 0 |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | $self->header('Content-Type' => 'application/x-ndjson'); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | elsif( is_hashref($body) ) { | 
| 37 | 0 |  |  |  |  |  | $self->header('Content-Type' => 'application/json'); | 
| 38 | 0 |  |  |  |  |  | $body = encode_json($body); | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | elsif( is_ref($body) ) { | 
| 41 |  |  |  |  |  |  | # We can't handle this | 
| 42 | 0 |  |  |  |  |  | warn sprintf "Invalid reference type '%s' passed to %s, discarding.", ref($body), __PACKAGE__; | 
| 43 | 0 |  |  |  |  |  | undef($body); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 | 0 |  |  |  |  | $self->{_content} = $body if defined $body; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | return $self->{_content}; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | 1; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | __END__ | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =pod | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head1 NAME | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | App::ElasticSearch::Utilities::HTTPRequest - Allow for strange content elements for Elasticsearch APIs | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =head1 VERSION | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | version 8.6 | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | This subclasses HTTP::Request and handles the B<content()> method invocation | 
| 68 |  |  |  |  |  |  | to allow passing content as expected by the Elasticsearch API.  You should not | 
| 69 |  |  |  |  |  |  | use this module in your code. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head1 AUTHOR | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Brad Lhotsky <brad@divisionbyzero.net> | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | This software is Copyright (c) 2023 by Brad Lhotsky. | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | This is free software, licensed under: | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | The (three-clause) BSD License | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =cut |