Cxn/Mojo.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 95 CC 9 SCAR 27.6
CC
9
Cov
0%
perform_request
30.0
error_from_text
20.0
_build_handle
6.0
CRAP 15.8
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::Cxn::Mojo;
6 $Search::Elasticsearch::Cxn::Mojo::VERSION = '7.30_1'; # TRIAL
7  
8 $Search::Elasticsearch::Cxn::Mojo::VERSION = '7.301';
9 use Mojo::UserAgent();
10 use Promises qw(deferred);
11 use Try::Tiny;
12 use Moo;
13  
14 with 'Search::Elasticsearch::Role::Cxn::Async';
15 with 'Search::Elasticsearch::Role::Cxn',
16 'Search::Elasticsearch::Role::Is_Async';
17  
18 has 'connect_timeout' => ( is => 'ro', default => 2 );
19  
20 use namespace::clean;
21  
22 #===================================
23 sub perform_request {
24 #===================================
25 my ( $self, $params ) = @_;
26  
27 my $uri = $self->build_uri($params) . '';
28 my $method = $params->{method};
29 my %headers = ( %{ $self->default_headers } );
30  
31 my @args = ( $method, $uri, \%headers );
32 my $data = $params->{data};
33 if ( defined $data ) {
34 $headers{'Content-Type'} = $params->{mime_type};
35 $headers{'Content-Encoding'} = $params->{encoding}
36 if $params->{encoding};
37 push @args, $data;
38 }
39  
40 my $handle = $self->handle;
41 $handle->connect_timeout( $self->connect_timeout );
42 $handle->request_timeout( $params->{timeout} || $self->request_timeout );
43  
44 my $tx = $handle->build_tx(@args);
45  
46 my $deferred = deferred;
47 $tx = $handle->start(
48 $tx,
49 sub {
50 my ( $ua, $tx ) = @_;
51 my $res = $tx->res;
52 my $error;
53 if ( $error = $res->error ) {
54 $error = $error->{message}
55 if ref $error eq 'HASH';
56 }
57  
58 my $headers = $res->headers->to_hash;
59 $headers->{ lc($_) } = delete $headers->{$_} for keys %{$headers};
60 try {
61 my ( $code, $response ) = $self->process_response(
62 $params, # request
63 ( $res->code || 500 ), # status
64 $error, # reason
65 $res->body, # content
66 $headers, # headers
67 );
68 $deferred->resolve( $code, $response );
69 }
70 catch {
71 $deferred->reject($_);
72 };
73 }
74 );
75 $deferred->promise;
76 }
77  
78 #===================================
79 sub error_from_text {
80 #===================================
81 local $_ = $_[2];
82 return
83 /[Tt]imed out/ ? 'Timeout'
84 : /SSL connect attempt failed/ ? 'SSL'
85 : /Invalid argument/ ? 'Cxn'
86 : 'Request';
87 }
88  
89 #===================================
90 sub _build_handle {
91 #===================================
92 my $self = shift;
93 my %args = %{ $self->handle_args };
94 if ( $self->is_https && $self->has_ssl_options ) {
95 %args = ( %args, %{ $self->ssl_options } );
96 }
97 return Mojo::UserAgent->new(%args);
98 }
99 1;
100  
101 # ABSTRACT: An async Cxn implementation which uses Mojo::UserAgent
102  
103 __END__