| 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::Client::Direct; | ||||||||||||||||||||||||||||||||
| 6 | 55 | $Search::Elasticsearch::Role::Client::Direct::VERSION = '8.12'; | |||||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 55 | with 'Search::Elasticsearch::Role::Client'; | |||||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use Search::Elasticsearch::Util qw(load_plugin is_compat throw); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 55 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 55 | ▶ | use Package::Stash 0.34 (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 55 | ▶ | use Any::URI::Escape qw(uri_escape); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 55 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | #=================================== | ||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub parse_request { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_request: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 18 | #=================================== | ||||||||||||||||||||||||||||||||
| 19 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | my $defn = shift || {}; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | my $params = { ref $_[0] ? %{ shift() } : @_ }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 0 | my $request; | |||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 25 | $request = { | ||||||||||||||||||||||||||||||||
| 26 | ignore => delete $params->{ignore} || [], | ||||||||||||||||||||||||||||||||
| 27 | method => $defn->{method} || 'GET', | ||||||||||||||||||||||||||||||||
| 28 | serialize => $defn->{serialize} || 'std', | ||||||||||||||||||||||||||||||||
| 29 | path => $self->_parse_path( $defn, $params ), | ||||||||||||||||||||||||||||||||
| 30 | body => $self->_parse_body( $defn->{body}, $params ), | ||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | qs => $self->_parse_qs( $defn->{qs}, $params ), | ||||||||||||||||||||||||||||||
|
Condition0%0/6
delete $params->{'ignore'} || []
$defn->{'method'} || 'GET'
$defn->{'serialize'} || 'std'
MC/DC0%0/1
delete $params->{'ignore'} || []
delete $params->{'ignore'}
MC/DC0%0/1
$defn->{'method'} || 'GET'
$defn->{'method'}
MC/DC0%0/1
$defn->{'serialize'} || 'std'
$defn->{'serialize'}
Truth table0%0/2
A
A= delete $params->{'ignore'}
Truth table0%0/2
A
A= $defn->{'method'}
Truth table0%0/2
A
A= $defn->{'serialize'}
| |||||||||||||||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 35 | 0 | chomp $_; | |||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $name = $defn->{name} || '<unknown method>'; | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$defn->{'name'} || '<unknown method>'
MC/DC0%0/1
$defn->{'name'} || '<unknown method>'
$defn->{'name'}
Truth table0%0/2
A
A= $defn->{'name'}
| |||||||||||||||||||||||||||||||||
| 37 | 0 | $self->logger->throw_error( 'Param', "$_ in ($name) request. " ); | |||||||||||||||||||||||||||||||
| 38 | 0 | }; | |||||||||||||||||||||||||||||||
| 39 | 0 | return $request; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | #=================================== | ||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _parse_path { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_path: not called CC=13 SCAR=50.3
| |||||||||||||||||||||||||||||||||
| 44 | #=================================== | ||||||||||||||||||||||||||||||||
| 45 | 0 | my ( $self, $defn, $params ) = @_; | |||||||||||||||||||||||||||||||
| 46 | return delete $params->{path} | ||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if $params->{path}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 0 | my $paths = $defn->{paths}; | |||||||||||||||||||||||||||||||
| 49 | 0 | my $parts = $defn->{parts}; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 0 | my %args; | |||||||||||||||||||||||||||||||
| 52 | 0 | keys %$parts; | |||||||||||||||||||||||||||||||
| 53 | 55 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | while ( my ( $key, $req ) = each %$parts ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | my $val = delete $params->{$key}; | |||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( ref $val eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | die "Param ($key) must contain a single value\n" | ||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if @$val > 1 and not $req->{multi}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$val > 1 and not $req->{"multi"}
MC/DC0%0/2
@$val > 1 and not $req->{"multi"}
@$val > 1
not $req->{"multi"}
Truth table0%0/3
A and not(B)
A= @$val > 1B= not $req->{"multi"}
| |||||||||||||||||||||||||||||||||
| 59 | 0 | $val = join ",", @$val; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ( !length $val ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | die "Missing required param ($key)\n" | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if $req->{required}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 64 | 0 | next; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | 0 | utf8::encode($val); | |||||||||||||||||||||||||||||||
| 67 | 0 | $args{$key} = uri_escape($val); | |||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | 0 | PATH: for my $path (@$paths) { | |||||||||||||||||||||||||||||||
| 70 | 0 | my @keys = keys %{ $path->[0] }; | |||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | next PATH unless @keys == keys %args; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | for (@keys) { | |||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | next PATH unless exists $args{$_}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | 0 | my ( $pos, @parts ) = @$path; | |||||||||||||||||||||||||||||||
| 76 | 0 | for ( keys %$pos ) { | |||||||||||||||||||||||||||||||
| 77 | 0 | $parts[ $pos->{$_} ] = $args{$_}; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | 0 | return join "/", '', @parts; | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | throw( | ||||||||||||||||||||||||||||||||
| 83 | 0 | 'Internal', | |||||||||||||||||||||||||||||||
| 84 | "Couldn't determine path", | ||||||||||||||||||||||||||||||||
| 85 | { params => $params, defn => $defn } | ||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | #=================================== | ||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub _parse_body { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_body: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||
| 91 | #=================================== | ||||||||||||||||||||||||||||||||
| 92 | 0 | my ( $self, $defn, $params ) = @_; | |||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( defined $defn ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | die("Missing required param (body)\n") | ||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if $defn->{required} && !$params->{body}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$defn->{"required"} and not $params->{"body"}
MC/DC0%0/2
$defn->{"required"} and not $params->{"body"}
$defn->{"required"}
not $params->{"body"}
Truth table0%0/3
A and not(B)
A= $defn->{"required"}B= not $params->{"body"}
| |||||||||||||||||||||||||||||||||
| 96 | 0 | return delete $params->{body}; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | die("Unknown param (body)\n") if $params->{body}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 99 | 0 | return undef; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | #=================================== | ||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _parse_qs { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_qs: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 104 | #=================================== | ||||||||||||||||||||||||||||||||
| 105 | 0 | my ( $self, $handlers, $params ) = @_; | |||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | die "No (qs) defined\n" unless $handlers; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 0 | my %qs; | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ( my $raw = delete $params->{params} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | die("Arg (params) shoud be a hashref\n") | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | unless ref $raw eq 'HASH'; | ||||||||||||||||||||||||||||||||
| 112 | 0 | %qs = %$raw; | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | for my $key ( keys %$params ) { | |||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my $handler = $handlers->{$key} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | or die("Unknown param ($key)\n"); | ||||||||||||||||||||||||||||||||
| 118 | 0 | $qs{$key} = $handler->( delete $params->{$key} ); | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 0 | return \%qs; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | #=================================== | ||||||||||||||||||||||||||||||||
| 124 | 55 | ▶ | sub _install_api { | ||||||||||||||||||||||||||||||
|
Subroutine
_install_api: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||||||
| 125 | #=================================== | ||||||||||||||||||||||||||||||||
| 126 | 55 | my ( $class, $group ) = @_; | |||||||||||||||||||||||||||||||
| 127 | 55 | my $defns = $class->api; | |||||||||||||||||||||||||||||||
| 128 | 55 | my $stash = Package::Stash->new($class); | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 55 | ▶ | my $group_qr = $group ? qr/$group\./ : qr//; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 55 | for my $action ( keys %$defns ) { | |||||||||||||||||||||||||||||||
| 132 | 26455 | ▶ | my ($name) = ( $action =~ /^$group_qr([^.]+)$/ ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | or next; | ||||||||||||||||||||||||||||||||
| 134 | 2420 | ▶ | next if $stash->has_symbol( '&' . $name ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 2420 | my %defn = ( name => $name, %{ $defns->{$action} } ); | |||||||||||||||||||||||||||||||
| 137 | $stash->add_symbol( | ||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | '&' . $name => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 139 | 0 | shift->perform_request( \%defn, @_ ); | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | 2420 | ); | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | #=================================== | ||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub _build_namespace { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_namespace: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 147 | #=================================== | ||||||||||||||||||||||||||||||||
| 148 | 0 | my ( $self, $ns ) = @_; | |||||||||||||||||||||||||||||||
| 149 | 0 | my $class = load_plugin( $self->_namespace, [$ns] ); | |||||||||||||||||||||||||||||||
| 150 | 0 | return $class->new( | |||||||||||||||||||||||||||||||
| 151 | { transport => $self->transport, | ||||||||||||||||||||||||||||||||
| 152 | logger => $self->logger | ||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | #=================================== | ||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub _build_helper { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_helper: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 159 | #=================================== | ||||||||||||||||||||||||||||||||
| 160 | 0 | my ( $self, $name, $sub_class ) = @_; | |||||||||||||||||||||||||||||||
| 161 | 0 | my $class = load_plugin( 'Search::Elasticsearch', $sub_class ); | |||||||||||||||||||||||||||||||
| 162 | 0 | is_compat( $name . '_helper_class', $self->transport, $class ); | |||||||||||||||||||||||||||||||
| 163 | 0 | return $class; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 55 | 1; | |||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | # ABSTRACT: Request parsing for Direct clients | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||||||