File Coverage

blib/lib/OpenSearch/Client/Role/Transport.pm
Criterion Covered Total %
statement 36 36 100.0
branch 11 12 91.6
condition 12 13 92.3
subroutine 6 6 100.0
pod 0 2 0.0
total 65 69 94.2


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.007002';
25 55     55   27335 use Moo::Role;
  55         102  
  55         332  
26              
27             requires qw(perform_request);
28              
29 55     55   23571 use Try::Tiny;
  55         102  
  55         3501  
30 55     55   259 use OpenSearch::Client::Util qw(parse_params is_compat);
  55         113  
  55         514  
31 55     55   16270 use namespace::clean;
  55         98  
  55         378  
32              
33             has 'serializer' => ( is => 'ro', required => 1 );
34             has 'logger' => ( is => 'ro', required => 1 );
35             has 'send_get_body_as' => ( is => 'ro', default => 'GET' );
36             has 'cxn_pool' => ( is => 'ro', required => 1 );
37              
38             #===================================
39             sub BUILD {
40             #===================================
41 100     100 0 79243 my $self = shift;
42 100         261 my $pool = $self->cxn_pool;
43 100         352 is_compat( 'cxn_pool', $self, $pool );
44 100         1382 is_compat( 'cxn', $self, $pool->cxn_factory->cxn_class );
45 100         1321 return $self;
46             }
47              
48             #===================================
49             sub tidy_request {
50             #===================================
51 175     175 0 18649 my ( $self, $params ) = parse_params(@_);
52 175   100     942 $params->{method} ||= 'GET';
53 175   100     656 $params->{path} ||= '/';
54 175   100     591 $params->{qs} ||= {};
55 175   100     619 $params->{ignore} ||= [];
56 175         271 my $body = $params->{body};
57 175 100       484 return $params unless defined $body;
58              
59 8   100     38 $params->{serialize} ||= 'std';
60             $params->{data}
61 8 100       61 = $params->{serialize} eq 'std'
62             ? $self->serializer->encode($body)
63             : $self->serializer->encode_bulk($body);
64              
65 8 50       141 if ( $params->{method} eq 'GET' ) {
66 8         21 my $send_as = $self->send_get_body_as;
67 8 100       35 if ( $send_as eq 'POST' ) {
    100          
68 1         2 $params->{method} = 'POST';
69             }
70             elsif ( $send_as eq 'source' ) {
71 1         3 $params->{qs}{source} = delete $params->{data};
72 1         2 delete $params->{body};
73             }
74             }
75            
76 8 100       22 if ( $params->{serialize} eq 'bulk' ) {
77 1         2 $params->{mime_type} = 'application/x-ndjson';
78             }
79            
80 8   66     41 $params->{mime_type} ||= $self->serializer->mime_type;
81 8         26 return $params;
82              
83             }
84              
85             1;
86              
87             __END__