File Coverage

blib/lib/OpenSearch/Client/Role/Transport.pm
Criterion Covered Total %
statement 33 33 100.0
branch 8 8 100.0
condition 12 13 92.3
subroutine 6 6 100.0
pod 0 2 0.0
total 59 62 95.1


line stmt bran cond sub pod time code
1             # OpenSearch::Client is an unofficial client for OpenSearch.
2             # It is derived from Search::Elasticsearch version 7.714
3             # License details from the original work are contained in the
4             # NOTICE file distributed with this work.
5             #
6             #-----------------------------------------------------------------------
7             # OpenSearch::Client
8             #-----------------------------------------------------------------------
9             # Copyright 2026 Mark Dootson
10             #
11             # Licensed under the Apache License, Version 2.0 (the "License");
12             # you may not use this file except in compliance with the License.
13             # You may obtain a copy of the License at
14             #
15             # http://www.apache.org/licenses/LICENSE-2.0
16             #
17             # Unless required by applicable law or agreed to in writing, software
18             # distributed under the License is distributed on an "AS IS" BASIS,
19             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20             # See the License for the specific language governing permissions and
21             # limitations under the License.
22              
23             package OpenSearch::Client::Role::Transport;
24             $OpenSearch::Client::Role::Transport::VERSION = '3.007005';
25 56     56   28317 use Moo::Role;
  56         101  
  56         437  
26              
27             requires qw(perform_request);
28              
29 56     56   23625 use Try::Tiny;
  56         131  
  56         4661  
30 56     56   298 use OpenSearch::Client::Util qw(parse_params is_compat);
  56         1622  
  56         2081  
31 56     56   17543 use namespace::clean;
  56         1638  
  56         5162  
32              
33             has 'serializer' => ( is => 'ro', required => 1 );
34             has 'logger' => ( is => 'ro', required => 1 );
35             has 'send_body_as_source' => ( is => 'ro', default => 0 );
36             has 'cxn_pool' => ( is => 'ro', required => 1 );
37              
38             #===================================
39             sub BUILD {
40             #===================================
41 100     100 0 80696 my $self = shift;
42 100         317 my $pool = $self->cxn_pool;
43 100         421 is_compat( 'cxn_pool', $self, $pool );
44 100         1578 is_compat( 'cxn', $self, $pool->cxn_factory->cxn_class );
45 100         1533 return $self;
46             }
47              
48             #===================================
49             sub tidy_request {
50             #===================================
51 175     175 0 31152 my ( $self, $params ) = parse_params(@_);
52 175   100     867 $params->{method} ||= 'GET';
53 175   100     640 $params->{path} ||= '/';
54 175   100     647 $params->{qs} ||= {};
55 175   100     545 $params->{ignore} ||= [];
56 175         236 my $body = $params->{body};
57 175 100       473 return $params unless defined $body;
58              
59 8   100     60 $params->{serialize} ||= 'std';
60             $params->{data}
61 8 100       60 = $params->{serialize} eq 'std'
62             ? $self->serializer->encode($body)
63             : $self->serializer->encode_bulk($body);
64              
65 8 100       156 if ( $self->send_body_as_source ) {
66 1         5 $params->{qs}{source} = delete $params->{data};
67 1         3 delete $params->{body};
68             }
69            
70 8 100       19 if ( $params->{serialize} eq 'bulk' ) {
71 1         2 $params->{mime_type} = 'application/x-ndjson';
72             }
73            
74 8   66     48 $params->{mime_type} ||= $self->serializer->mime_type;
75 8         23 return $params;
76              
77             }
78              
79             1;
80              
81             __END__