| 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::AEHTTP; | ||||||||||||||||||
| 6 | 24 | $Search::Elasticsearch::Cxn::AEHTTP::VERSION = '7.30_1'; # TRIAL | |||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 24 | $Search::Elasticsearch::Cxn::AEHTTP::VERSION = '7.301'; | |||||||||||||||||
| 9 | 24 | ▶ | use AnyEvent::HTTP qw(http_request); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 24 | ▶ | use Promises qw(deferred); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 24 | ▶ | use Try::Tiny; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | 24 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 24 | with 'Search::Elasticsearch::Role::Cxn::Async'; | |||||||||||||||||
| 15 | 24 | with 'Search::Elasticsearch::Role::Cxn', | |||||||||||||||||
| 16 | 'Search::Elasticsearch::Role::Is_Async'; | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 24 | has '_tls_ctx' => ( is => 'lazy' ); | |||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 24 | ▶ | use namespace::clean; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | #=================================== | ||||||||||||||||||
| 23 | sub _build__tls_ctx { | ||||||||||||||||||
| 24 | #=================================== | ||||||||||||||||||
| 25 | 0 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
_build__tls_ctx: not called CC=2 SCAR=17.9
| |||||||||||||||||||
| 26 | 0 | ▶ | return 'low' unless $self->has_ssl_options; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 27 | 0 | require AnyEvent::TLS; | |||||||||||||||||
| 28 | 0 | return AnyEvent::TLS->new( %{ $self->ssl_options } ); | |||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | #=================================== | ||||||||||||||||||
| 32 | sub perform_request { | ||||||||||||||||||
| 33 | #=================================== | ||||||||||||||||||
| 34 | my ( $self, $params ) = @_; | ||||||||||||||||||
| 35 | my $uri = $self->build_uri($params); | ||||||||||||||||||
| 36 | my $method = $params->{method}; | ||||||||||||||||||
| 37 | my %headers = ( %{ $self->default_headers } ); | ||||||||||||||||||
| 38 | my $data = $params->{data}; | ||||||||||||||||||
| 39 | if ( defined $data ) { | ||||||||||||||||||
| 40 | $headers{'Content-Type'} = $params->{mime_type}; | ||||||||||||||||||
| 41 | $headers{'Content-Encoding'} = $params->{encoding} | ||||||||||||||||||
| 42 | if $params->{encoding}; | ||||||||||||||||||
| 43 | } | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | my $deferred = deferred; | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | http_request( | ||||||||||||||||||
| 48 | $method => $uri, | ||||||||||||||||||
| 49 | headers => \%headers, | ||||||||||||||||||
| 50 | timeout => $params->{timeout} || $self->request_timeout, | ||||||||||||||||||
| 51 | body => $data, | ||||||||||||||||||
| 52 | persistent => 0, | ||||||||||||||||||
| 53 | session => $self->_pid, | ||||||||||||||||||
| 54 | ( %{ $self->handle_args } ), | ||||||||||||||||||
| 55 | ( $self->is_https ? ( tls_ctx => $self->_tls_ctx ) : () ), | ||||||||||||||||||
| 56 | sub { | ||||||||||||||||||
| 57 | my ( $body, $headers ) = @_; | ||||||||||||||||||
| 58 | try { | ||||||||||||||||||
| 59 | my ( $code, $response ) = $self->process_response( | ||||||||||||||||||
| 60 | $params, # request | ||||||||||||||||||
| 61 | delete $headers->{Status}, # code | ||||||||||||||||||
| 62 | delete $headers->{Reason}, # msg | ||||||||||||||||||
| 63 | $body, # body | ||||||||||||||||||
| 64 | $headers # headers | ||||||||||||||||||
| 65 | ); | ||||||||||||||||||
| 66 | $deferred->resolve( $code, $response ); | ||||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | catch { | ||||||||||||||||||
| 69 | $deferred->reject($_); | ||||||||||||||||||
| 70 | } | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | ); | ||||||||||||||||||
| 74 | $deferred->promise; | ||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | |||||||||||||||||||
| 77 | #=================================== | ||||||||||||||||||
| 78 | sub error_from_text { | ||||||||||||||||||
| 79 | #=================================== | ||||||||||||||||||
| 80 | 0 | ▶ | local $_ = $_[2]; | ||||||||||||||||
|
Subroutine
error_from_text: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||
| 81 | return | ||||||||||||||||||
| 82 | 0 | ▶ | /[Tt]imed out/ ? 'Timeout' | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 83 | : /certificate verify failed/ ? 'SSL' | ||||||||||||||||||
| 84 | : /Invalid argument/ ? 'Cxn' | ||||||||||||||||||
| 85 | : 'Request'; | ||||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 24 | 1; | |||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | # ABSTRACT: An async Cxn implementation which uses AnyEvent::HTTP | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||