| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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
|
|
|
|
|
|
|
$Search::Elasticsearch::Role::Client::Direct::VERSION = '8.00'; |
|
7
|
55
|
|
|
55
|
|
30644
|
use Moo::Role; |
|
|
55
|
|
|
|
|
146
|
|
|
|
55
|
|
|
|
|
321
|
|
|
8
|
|
|
|
|
|
|
with 'Search::Elasticsearch::Role::Client'; |
|
9
|
55
|
|
|
55
|
|
18517
|
use Search::Elasticsearch::Util qw(load_plugin is_compat throw); |
|
|
55
|
|
|
|
|
127
|
|
|
|
55
|
|
|
|
|
377
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
55
|
|
|
55
|
|
24015
|
use Try::Tiny; |
|
|
55
|
|
|
|
|
118
|
|
|
|
55
|
|
|
|
|
3140
|
|
|
12
|
55
|
|
|
55
|
|
346
|
use Package::Stash 0.34 (); |
|
|
55
|
|
|
|
|
1251
|
|
|
|
55
|
|
|
|
|
1173
|
|
|
13
|
55
|
|
|
55
|
|
20792
|
use Any::URI::Escape qw(uri_escape); |
|
|
55
|
|
|
|
|
164774
|
|
|
|
55
|
|
|
|
|
3334
|
|
|
14
|
55
|
|
|
55
|
|
394
|
use namespace::clean; |
|
|
55
|
|
|
|
|
115
|
|
|
|
55
|
|
|
|
|
393
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#=================================== |
|
17
|
|
|
|
|
|
|
sub parse_request { |
|
18
|
|
|
|
|
|
|
#=================================== |
|
19
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
|
20
|
0
|
|
0
|
|
|
0
|
my $defn = shift || {}; |
|
21
|
0
|
0
|
|
|
|
0
|
my $params = { ref $_[0] ? %{ shift() } : @_ }; |
|
|
0
|
|
|
|
|
0
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
0
|
my $request; |
|
24
|
|
|
|
|
|
|
try { |
|
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
|
|
0
|
0
|
|
0
|
qs => $self->_parse_qs( $defn->{qs}, $params ), |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
32
|
|
|
|
|
|
|
}; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
catch { |
|
35
|
0
|
|
|
0
|
|
0
|
chomp $_; |
|
36
|
0
|
|
0
|
|
|
0
|
my $name = $defn->{name} || ''; |
|
37
|
0
|
|
|
|
|
0
|
$self->logger->throw_error( 'Param', "$_ in ($name) request. " ); |
|
38
|
0
|
|
|
|
|
0
|
}; |
|
39
|
0
|
|
|
|
|
0
|
return $request; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
#=================================== |
|
43
|
|
|
|
|
|
|
sub _parse_path { |
|
44
|
|
|
|
|
|
|
#=================================== |
|
45
|
0
|
|
|
0
|
|
0
|
my ( $self, $defn, $params ) = @_; |
|
46
|
|
|
|
|
|
|
return delete $params->{path} |
|
47
|
0
|
0
|
|
|
|
0
|
if $params->{path}; |
|
48
|
0
|
|
|
|
|
0
|
my $paths = $defn->{paths}; |
|
49
|
0
|
|
|
|
|
0
|
my $parts = $defn->{parts}; |
|
50
|
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
0
|
my %args; |
|
52
|
0
|
|
|
|
|
0
|
keys %$parts; |
|
53
|
55
|
|
|
55
|
|
30510
|
no warnings 'uninitialized'; |
|
|
55
|
|
|
|
|
129
|
|
|
|
55
|
|
|
|
|
49216
|
|
|
54
|
0
|
|
|
|
|
0
|
while ( my ( $key, $req ) = each %$parts ) { |
|
55
|
0
|
|
|
|
|
0
|
my $val = delete $params->{$key}; |
|
56
|
0
|
0
|
|
|
|
0
|
if ( ref $val eq 'ARRAY' ) { |
|
57
|
|
|
|
|
|
|
die "Param ($key) must contain a single value\n" |
|
58
|
0
|
0
|
0
|
|
|
0
|
if @$val > 1 and not $req->{multi}; |
|
59
|
0
|
|
|
|
|
0
|
$val = join ",", @$val; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
0
|
0
|
|
|
|
0
|
if ( !length $val ) { |
|
62
|
|
|
|
|
|
|
die "Missing required param ($key)\n" |
|
63
|
0
|
0
|
|
|
|
0
|
if $req->{required}; |
|
64
|
0
|
|
|
|
|
0
|
next; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
0
|
|
|
|
|
0
|
utf8::encode($val); |
|
67
|
0
|
|
|
|
|
0
|
$args{$key} = uri_escape($val); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
0
|
|
|
|
|
0
|
PATH: for my $path (@$paths) { |
|
70
|
0
|
|
|
|
|
0
|
my @keys = keys %{ $path->[0] }; |
|
|
0
|
|
|
|
|
0
|
|
|
71
|
0
|
0
|
|
|
|
0
|
next PATH unless @keys == keys %args; |
|
72
|
0
|
|
|
|
|
0
|
for (@keys) { |
|
73
|
0
|
0
|
|
|
|
0
|
next PATH unless exists $args{$_}; |
|
74
|
|
|
|
|
|
|
} |
|
75
|
0
|
|
|
|
|
0
|
my ( $pos, @parts ) = @$path; |
|
76
|
0
|
|
|
|
|
0
|
for ( keys %$pos ) { |
|
77
|
0
|
|
|
|
|
0
|
$parts[ $pos->{$_} ] = $args{$_}; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
0
|
|
|
|
|
0
|
return join "/", '', @parts; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
throw( |
|
83
|
0
|
|
|
|
|
0
|
'Internal', |
|
84
|
|
|
|
|
|
|
"Couldn't determine path", |
|
85
|
|
|
|
|
|
|
{ params => $params, defn => $defn } |
|
86
|
|
|
|
|
|
|
); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
#=================================== |
|
90
|
|
|
|
|
|
|
sub _parse_body { |
|
91
|
|
|
|
|
|
|
#=================================== |
|
92
|
0
|
|
|
0
|
|
0
|
my ( $self, $defn, $params ) = @_; |
|
93
|
0
|
0
|
|
|
|
0
|
if ( defined $defn ) { |
|
94
|
|
|
|
|
|
|
die("Missing required param (body)\n") |
|
95
|
0
|
0
|
0
|
|
|
0
|
if $defn->{required} && !$params->{body}; |
|
96
|
0
|
|
|
|
|
0
|
return delete $params->{body}; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
0
|
0
|
|
|
|
0
|
die("Unknown param (body)\n") if $params->{body}; |
|
99
|
0
|
|
|
|
|
0
|
return undef; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
#=================================== |
|
103
|
|
|
|
|
|
|
sub _parse_qs { |
|
104
|
|
|
|
|
|
|
#=================================== |
|
105
|
0
|
|
|
0
|
|
0
|
my ( $self, $handlers, $params ) = @_; |
|
106
|
0
|
0
|
|
|
|
0
|
die "No (qs) defined\n" unless $handlers; |
|
107
|
0
|
|
|
|
|
0
|
my %qs; |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
0
|
if ( my $raw = delete $params->{params} ) { |
|
110
|
0
|
0
|
|
|
|
0
|
die("Arg (params) shoud be a hashref\n") |
|
111
|
|
|
|
|
|
|
unless ref $raw eq 'HASH'; |
|
112
|
0
|
|
|
|
|
0
|
%qs = %$raw; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
0
|
for my $key ( keys %$params ) { |
|
116
|
0
|
0
|
|
|
|
0
|
my $handler = $handlers->{$key} |
|
117
|
|
|
|
|
|
|
or die("Unknown param ($key)\n"); |
|
118
|
0
|
|
|
|
|
0
|
$qs{$key} = $handler->( delete $params->{$key} ); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
0
|
|
|
|
|
0
|
return \%qs; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
#=================================== |
|
124
|
|
|
|
|
|
|
sub _install_api { |
|
125
|
|
|
|
|
|
|
#=================================== |
|
126
|
55
|
|
|
55
|
|
203
|
my ( $class, $group ) = @_; |
|
127
|
55
|
|
|
|
|
296
|
my $defns = $class->api; |
|
128
|
55
|
|
|
|
|
733
|
my $stash = Package::Stash->new($class); |
|
129
|
|
|
|
|
|
|
|
|
130
|
55
|
50
|
|
|
|
502
|
my $group_qr = $group ? qr/$group\./ : qr//; |
|
131
|
55
|
|
|
|
|
3424
|
for my $action ( keys %$defns ) { |
|
132
|
22825
|
100
|
|
|
|
83784
|
my ($name) = ( $action =~ /^$group_qr([^.]+)$/ ) |
|
133
|
|
|
|
|
|
|
or next; |
|
134
|
2365
|
50
|
|
|
|
10571
|
next if $stash->has_symbol( '&' . $name ); |
|
135
|
|
|
|
|
|
|
|
|
136
|
2365
|
|
|
|
|
4261
|
my %defn = ( name => $name, %{ $defns->{$action} } ); |
|
|
2365
|
|
|
|
|
12912
|
|
|
137
|
|
|
|
|
|
|
$stash->add_symbol( |
|
138
|
|
|
|
|
|
|
'&' . $name => sub { |
|
139
|
0
|
|
|
0
|
|
|
shift->perform_request( \%defn, @_ ); |
|
140
|
|
|
|
|
|
|
} |
|
141
|
2365
|
|
|
|
|
18895
|
); |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
#=================================== |
|
146
|
|
|
|
|
|
|
sub _build_namespace { |
|
147
|
|
|
|
|
|
|
#=================================== |
|
148
|
0
|
|
|
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
|
|
|
|
|
|
|
sub _build_helper { |
|
159
|
|
|
|
|
|
|
#=================================== |
|
160
|
0
|
|
|
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
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# ABSTRACT: Request parsing for Direct clients |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
__END__ |