File Coverage

blib/lib/Search/Elasticsearch/Role/Transport.pm
Criterion Covered Total %
statement 34 34 100.0
branch 9 10 90.0
condition 12 13 92.3
subroutine 6 6 100.0
pod 0 2 0.0
total 61 65 93.8


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V under one or more agreements.
2             # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3             # See the LICENSE file in the project root for more information
4              
5             package Search::Elasticsearch::Role::Transport;
6             $Search::Elasticsearch::Role::Transport::VERSION = '7.717';
7 55     55   27286 use Moo::Role;
  55         102  
  55         531  
8              
9             requires qw(perform_request);
10              
11 55     55   15785 use Try::Tiny;
  55         107  
  55         2846  
12 55     55   298 use Search::Elasticsearch::Util qw(parse_params is_compat);
  55         87  
  55         553  
13 55     55   15803 use namespace::clean;
  55         102  
  55         260  
14              
15             has 'serializer' => ( is => 'ro', required => 1 );
16             has 'logger' => ( is => 'ro', required => 1 );
17             has 'send_get_body_as' => ( is => 'ro', default => 'GET' );
18             has 'cxn_pool' => ( is => 'ro', required => 1 );
19              
20             #===================================
21             sub BUILD {
22             #===================================
23 100     100 0 72314 my $self = shift;
24 100         293 my $pool = $self->cxn_pool;
25 100         364 is_compat( 'cxn_pool', $self, $pool );
26 100         1501 is_compat( 'cxn', $self, $pool->cxn_factory->cxn_class );
27 100         1372 return $self;
28             }
29              
30             #===================================
31             sub tidy_request {
32             #===================================
33 175     175 0 19745 my ( $self, $params ) = parse_params(@_);
34 175   100     835 $params->{method} ||= 'GET';
35 175   100     590 $params->{path} ||= '/';
36 175   100     608 $params->{qs} ||= {};
37 175   100     601 $params->{ignore} ||= [];
38 175         264 my $body = $params->{body};
39 175 100       468 return $params unless defined $body;
40              
41 8   100     41 $params->{serialize} ||= 'std';
42             $params->{data}
43 8 100       108 = $params->{serialize} eq 'std'
44             ? $self->serializer->encode($body)
45             : $self->serializer->encode_bulk($body);
46              
47 8 50       142 if ( $params->{method} eq 'GET' ) {
48 8         35 my $send_as = $self->send_get_body_as;
49 8 100       40 if ( $send_as eq 'POST' ) {
    100          
50 1         4 $params->{method} = 'POST';
51             }
52             elsif ( $send_as eq 'source' ) {
53 1         3 $params->{qs}{source} = delete $params->{data};
54 1         5 delete $params->{body};
55             }
56             }
57              
58 8   66     81 $params->{mime_type} ||= $self->serializer->mime_type;
59 8         30 return $params;
60              
61             }
62              
63             1;
64              
65             #ABSTRACT: Transport role providing interface between the client class and the Elasticsearch cluster
66              
67             __END__