1package Search::Elasticsearch::Role::Transport; 2$Search::Elasticsearch::Role::Transport::VERSION = '6.00'; 3use Moo::Role; 4 5requires qw(perform_request); 6 7use Try::Tiny; 8use Search::Elasticsearch::Util qw(parse_params is_compat); 9use namespace::clean; 10 11has 'serializer' => ( is => 'ro', required => 1 ); 12has 'logger' => ( is => 'ro', required => 1 ); 13has 'send_get_body_as' => ( is => 'ro', default => 'GET' ); 14has 'cxn_pool' => ( is => 'ro', required => 1 ); 15 16#=================================== 17sub BUILD { 18#=================================== 19 my $self = shift; 20 my $pool = $self->cxn_pool; 21 is_compat( 'cxn_pool', $self, $pool ); 22 is_compat( 'cxn', $self, $pool->cxn_factory->cxn_class ); 23 return $self; 24} 25 26#=================================== 27sub tidy_request { 28#=================================== 29 my ( $self, $params ) = parse_params(@_); 30 $params->{method} ||= 'GET'; 31 $params->{path} ||= '/'; 32 $params->{qs} ||= {}; 33 $params->{ignore} ||= []; 34 my $body = $params->{body}; 35 return $params unless defined $body; 36 37 $params->{serialize} ||= 'std'; 38 $params->{data} 39 = $params->{serialize} eq 'std' 40 ? $self->serializer->encode($body) 41 : $self->serializer->encode_bulk($body); 42 43 if ( $params->{method} eq 'GET' ) { 44 my $send_as = $self->send_get_body_as; 45 if ( $send_as eq 'POST' ) { 46 $params->{method} = 'POST'; 47 } 48 elsif ( $send_as eq 'source' ) { 49 $params->{qs}{source} = delete $params->{data}; 50 delete $params->{body}; 51 } 52 } 53 54 $params->{mime_type} ||= $self->serializer->mime_type; 55 return $params; 56 57} 58 591; 60 61#ABSTRACT: Transport role providing interface between the client class and the Elasticsearch cluster 62 63__END__ 64 65=pod 66 67=encoding UTF-8 68 69=head1 NAME 70 71Search::Elasticsearch::Role::Transport - Transport role providing interface between the client class and the Elasticsearch cluster 72 73=head1 VERSION 74 75version 6.00 76 77=head1 AUTHOR 78 79Clinton Gormley <drtech@cpan.org> 80 81=head1 COPYRIGHT AND LICENSE 82 83This software is Copyright (c) 2017 by Elasticsearch BV. 84 85This is free software, licensed under: 86 87 The Apache License, Version 2.0, January 2004 88 89=cut 90