File Coverage

blib/lib/Search/Elasticsearch/Cxn/AEHTTP.pm
Criterion Covered Total %
statement 17 22 77.2
branch 1 8 12.5
condition n/a
subroutine 6 7 85.7
pod 0 1 0.0
total 24 38 63.1


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V. under one or more contributor
2             # license agreements. See the NOTICE file distributed with
3             # this work for additional information regarding copyright
4             # ownership. Elasticsearch B.V. licenses this file to you under
5             # the Apache License, Version 2.0 (the "License"); you may
6             # not use this file except in compliance with the License.
7             # You may obtain a copy of the License at
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing,
12             # software distributed under the License is distributed on an
13             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14             # KIND, either express or implied. See the License for the
15             # specific language governing permissions and limitations
16             # under the License.
17              
18             package Search::Elasticsearch::Cxn::AEHTTP;
19             $Search::Elasticsearch::Cxn::AEHTTP::VERSION = '7.715';
20 24     24   1930024 use AnyEvent::HTTP qw(http_request);
  24         705544  
  24         1946  
21 24     24   239 use Promises qw(deferred);
  24         57  
  24         250  
22 24     24   7196 use Try::Tiny;
  24         56  
  24         1188  
23 24     24   140 use Moo;
  24         54  
  24         173  
24              
25             with 'Search::Elasticsearch::Role::Cxn::Async';
26             with 'Search::Elasticsearch::Role::Cxn',
27             'Search::Elasticsearch::Role::Is_Async';
28              
29             has '_tls_ctx' => ( is => 'lazy' );
30              
31 24     24   8364 use namespace::clean;
  24         54  
  24         196  
32              
33             #===================================
34             sub _build__tls_ctx {
35             #===================================
36 0     0   0 my $self = shift;
37 0 0       0 return 'low' unless $self->has_ssl_options;
38 0         0 require AnyEvent::TLS;
39 0         0 return AnyEvent::TLS->new( %{ $self->ssl_options } );
  0         0  
40             }
41              
42             #===================================
43             sub perform_request {
44             #===================================
45             my ( $self, $params ) = @_;
46             my $uri = $self->build_uri($params);
47             my $method = $params->{method};
48             my %headers = ( %{ $self->default_headers } );
49             my $data = $params->{data};
50             if ( defined $data ) {
51             $headers{'Content-Type'} = $params->{mime_type};
52             $headers{'Content-Encoding'} = $params->{encoding}
53             if $params->{encoding};
54             }
55              
56             my $deferred = deferred;
57              
58             http_request(
59             $method => $uri,
60             headers => \%headers,
61             timeout => $params->{timeout} || $self->request_timeout,
62             body => $data,
63             persistent => 0,
64             session => $self->_pid,
65             ( %{ $self->handle_args } ),
66             ( $self->is_https ? ( tls_ctx => $self->_tls_ctx ) : () ),
67             sub {
68             my ( $body, $headers ) = @_;
69             try {
70             my ( $code, $response ) = $self->process_response(
71             $params, # request
72             delete $headers->{Status}, # code
73             delete $headers->{Reason}, # msg
74             $body, # body
75             $headers # headers
76             );
77             $deferred->resolve( $code, $response );
78             }
79             catch {
80             $deferred->reject($_);
81             }
82              
83             }
84             );
85             $deferred->promise;
86             }
87              
88             #===================================
89             sub error_from_text {
90             #===================================
91 1     1 0 15037 local $_ = $_[2];
92             return
93 1 0       8 /[Tt]imed out/ ? 'Timeout'
    0          
    50          
94             : /certificate verify failed/ ? 'SSL'
95             : /Invalid argument/ ? 'Cxn'
96             : 'Request';
97             }
98              
99             1;
100              
101             # ABSTRACT: An async Cxn implementation which uses AnyEvent::HTTP
102              
103             __END__