Client/Role/Client/Direct.pm

filter: Statementstmt 81.9% 95 / 116 Branchbran 59.2% 32 / 54 Conditioncond 47.2% 17 / 36 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 1 totaltotal 65.7% 161 / 245 CC 48 SCAR 23.9
CC
48
Cov
70%
_parse_method
18.2
_parse_path
15.5
_parse_qs
12.2
CRAP 10.9
« Client/Transport/TryCatch.pm ↑ Summary Client/Core/3_0/Helper/Scroll.pm »
1 # OpenSearch::Client is an unofficial client for OpenSearch.
2 # It is derived from Search::Elasticsearch version 7.714
3 # License details from the original work are contained in the
4 # NOTICE file distributed with this work.
5 #
6 #-----------------------------------------------------------------------
7 # OpenSearch::Client
8 #-----------------------------------------------------------------------
9 # Copyright 2026 Mark Dootson
10 #
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14 #
15 # http://www.apache.org/licenses/LICENSE-2.0
16 #
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22  
23 package OpenSearch::Client::Role::Client::Direct;
24 56 $OpenSearch::Client::Role::Client::Direct::VERSION = '3.007011';
25 56 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 56 with 'OpenSearch::Client::Role::Client';
27 56 ▶use OpenSearch::Client::Util qw(load_plugin is_compat throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 56 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 56 ▶use Package::Stash 0.34 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 56 ▶use Any::URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 56 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 #===================================
35 8 ▶sub parse_request {
Subroutine parse_request: called CC=3 SCAR=13.1
Pod undocumented
36 #===================================
37 8 my $self = shift;
38 8 ▶ my $defn = shift || {};
Condition50%1/2
shift() || {}
l!l
80
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
39 8 ▶ my $params = { ref $_[0] ? %{ shift() } : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 0 8
40  
41 8 my $request;
42 8 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=12.7
43 $request = {
44 ignore => delete $params->{ignore} || [],
45 method => $self->_parse_method ( $defn, $params ),
46 serialize => $defn->{serialize} || 'std',
47 path => $self->_parse_path( $defn, $params ),
48 body => $self->_parse_body( $defn->{body}, $params ),
49 8 ▶ qs => $self->_parse_qs( $defn->{qs}, $params ),
Condition50%2/4
delete $params->{'ignore'} || []
l!l
08
$defn->{'serialize'} || 'std'
l!l
08
MC/DC0%0/1
delete $params->{'ignore'} || []
delete $params->{'ignore'}
MC/DC0%0/1
$defn->{'serialize'} || 'std'
$defn->{'serialize'}
Truth table50%1/2
A
A= delete $params->{'ignore'}
Aresult
11
00
Truth table50%1/2
A
A= $defn->{'serialize'}
Aresult
11
00
50 };
51 }
52 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
53 0 chomp $_;
54 0 ▶ my $name = $defn->{name} || '<unknown method>';
Condition0%0/2
$defn->{'name'} || '<unknown method>'
l!l
00
MC/DC0%0/1
$defn->{'name'} || '<unknown method>'
$defn->{'name'}
Truth table0%0/2
A
A= $defn->{'name'}
Aresult
11
00
55 0 $self->logger->throw_error( 'Param', "$_ in ($name) request. " );
56 8 };
57 8 return $request;
58 }
59  
60 #===================================
61 8 ▶sub _parse_method {
Subroutine _parse_method: called CC=16 SCAR=29.0
62 #===================================
63 8 my ( $self, $defn, $params ) = @_;
64
65 8 ▶ my $method = $defn->{method} || 'GET';
Condition50%1/2
$defn->{'method'} || 'GET'
l!l
80
MC/DC0%0/1
$defn->{'method'} || 'GET'
$defn->{'method'}
Truth table50%1/2
A
A= $defn->{'method'}
Aresult
11
00
66 8 ▶ return $method unless( $method eq 'detect' );
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "detect" 0 8 8
67 8 my $dp = $defn->{detect};
68 8 $method = $dp->{method};
69
70 8 ▶ return $method unless(defined($params) && ref($params) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params and ref $params eq "HASH" 0 8 8
Condition33%1/3
defined $params and ref $params eq "HASH"
!ll&&!rl&&r
008
MC/DC0%0/2
defined $params and ref $params eq "HASH"
defined $params ref $params eq "HASH"
Truth table33%1/3
A and B
A= defined $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
71
72 ## is there a body param
73 8 ▶ if ( $dp->{check}->{body} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dp->{"check"}{"body"}) 4 4 8
74 4 ▶ return $dp->{alternate} if(exists($params->{body}) && defined($params->{body}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"body"} and defined $params->{"body"} 2 2 4
Condition66%2/3
exists $params->{"body"} and defined $params->{"body"}
!ll&&!rl&&r
202
MC/DC50%1/2
exists $params->{"body"} and defined $params->{"body"}
exists $params->{"body"} defined $params->{"body"}
Truth table66%2/3
A and B
A= exists $params->{"body"}B= defined $params->{"body"}
ABresult
0X0
100
111
75 }
76
77 ## are there any optional params
78 6 ▶ if ( $dp->{check}->{paths}
Branch: 1 branch
BranchTrueFalseTotal
if ($dp->{"check"}{"paths"} and exists $defn->{"parts"} and defined $defn->{"parts"} and ref $defn->{"parts"} eq "HASH") 4 2 6
Condition66%6/9
$dp->{"check"}{"paths"} and exists $defn->{"parts"} and defined $defn->{"parts"} and ref $defn->{"parts"} eq "HASH"
!ll&&!rl&&r
204
$dp->{"check"}{"paths"} and exists $defn->{"parts"} and defined $defn->{"parts"}
!ll&&!rl&&r
204
$dp->{"check"}{"paths"} and exists $defn->{"parts"}
!ll&&!rl&&r
204
MC/DC25%1/4
$dp->{"check"}{"paths"} and exists $defn->{"parts"} and defined $defn->{"parts"} and ref $defn->{"parts"} eq "HASH"
$dp->{"check"}{"paths"} exists $defn->{"parts"} defined $defn->{"parts"} ref $defn->{"parts"} eq "HASH"
Truth table40%2/5
A and B and C and D
A= $dp->{"check"}{"paths"}B= exists $defn->{"parts"}C= defined $defn->{"parts"}D= ref $defn->{"parts"} eq "HASH"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
79 && exists($defn->{parts})
80 && defined($defn->{parts})
81 && ref($defn->{parts}) eq 'HASH'
82 ) {
83
84 4 my @pathparts = keys %{ $defn->{parts} };
85
86 4 for my $pathpart ( @pathparts ) {
87 7 ▶ next if $defn->{parts}->{$pathpart}->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $defn->{"parts"}{$pathpart}{"required"} 3 4 7
88 4 ▶ return $dp->{alternate} if(exists($params->{$pathpart}) && defined($params->{$pathpart}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{$pathpart} and defined $params->{$pathpart} 2 2 4
Condition66%2/3
exists $params->{$pathpart} and defined $params->{$pathpart}
!ll&&!rl&&r
202
MC/DC50%1/2
exists $params->{$pathpart} and defined $params->{$pathpart}
exists $params->{$pathpart} defined $params->{$pathpart}
Truth table66%2/3
A and B
A= exists $params->{$pathpart}B= defined $params->{$pathpart}
ABresult
0X0
100
111
89 }
90 }
91
92 4 return $method;
93  
94 }
95  
96  
97 #===================================
98 8 ▶sub _parse_path {
Subroutine _parse_path: called CC=13 SCAR=27.4
99 #===================================
100 8 my ( $self, $defn, $params ) = @_;
101 return delete $params->{path}
102 8 ▶ if $params->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"path"} 0 8 8
103 8 my $paths = $defn->{paths};
104 8 my $parts = $defn->{parts};
105  
106 8 my %args;
107 8 keys %$parts;
108 56 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 8 ▶ while ( my ( $key, $req ) = each %$parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$parts) 14 8 22
110 14 my $val = delete $params->{$key};
111 14 ▶ if ( ref $val eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 0 14 14
112 die "Param ($key) must contain a single value\n"
113 0 ▶ if @$val > 1 and not $req->{multi};
Branch: 1 branch
BranchTrueFalseTotal
if @$val > 1 and not $req->{"multi"} 0 0 0
Condition0%0/3
@$val > 1 and not $req->{"multi"}
!ll&&!rl&&r
000
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"}
ABresult
0X0
100
111
114 0 $val = join ",", @$val;
115 }
116 14 ▶ if ( !length $val ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $val) 2 12 14
117 die "Missing required param ($key)\n"
118 2 ▶ if $req->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"required"} 0 2 2
119 2 next;
120 }
121 12 utf8::encode($val);
122 12 $args{$key} = uri_escape($val);
123 }
124 8 PATH: for my $path (@$paths) {
125 10 my @keys = keys %{ $path->[0] };
126 10 ▶ next PATH unless @keys == keys %args;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys == keys %args 2 8 10
127 8 for (@keys) {
128 12 ▶ next PATH unless exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$_} 0 12 12
129 }
130 8 my ( $pos, @parts ) = @$path;
131 8 for ( keys %$pos ) {
132 12 $parts[ $pos->{$_} ] = $args{$_};
133 }
134 8 return join "/", '', @parts;
135 }
136  
137 throw(
138 0 'Internal',
139 "Couldn't determine path",
140 { params => $params, defn => $defn }
141 );
142 }
143  
144 #===================================
145 8 ▶sub _parse_body {
Subroutine _parse_body: called CC=5 SCAR=21.7
146 #===================================
147 8 my ( $self, $defn, $params ) = @_;
148 8 ▶ if ( defined $defn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defn) 8 0 8
149 die("Missing required param (body)\n")
150 8 ▶ if $defn->{required} && !$params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $defn->{"required"} and not $params->{"body"} 0 8 8
Condition33%1/3
$defn->{"required"} and not $params->{"body"}
!ll&&!rl&&r
800
MC/DC0%0/2
$defn->{"required"} and not $params->{"body"}
$defn->{"required"} not $params->{"body"}
Truth table33%1/3
A and not(B)
A= $defn->{"required"}B= not $params->{"body"}
ABresult
0X0
100
111
151 8 return delete $params->{body};
152 }
153 0 ▶ die("Unknown param (body)\n") if $params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"body"} 0 0 0
154 0 return undef;
155 }
156  
157 #===================================
158 8 ▶sub _parse_qs {
Subroutine _parse_qs: called CC=6 SCAR=25.0
159 #===================================
160 8 my ( $self, $handlers, $params ) = @_;
161 8 ▶ die "No (qs) defined\n" unless $handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless $handlers 0 8 8
162 8 my %qs;
163  
164 8 ▶ if ( my $raw = delete $params->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $raw = delete $params->{"params"}) 0 8 8
165 0 ▶ die("Arg (params) shoud be a hashref\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "HASH" 0 0 0
166 unless ref $raw eq 'HASH';
167 0 %qs = %$raw;
168 }
169  
170 8 for my $key ( keys %$params ) {
171 0 ▶ my $handler = $handlers->{$key}
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $handlers->{$key} or die "Unknown param ($key)\n" 0 0 0
172 or die("Unknown param ($key)\n");
173 0 $qs{$key} = $handler->( delete $params->{$key} );
174 }
175 8 return \%qs;
176 }
177  
178 #===================================
179 56 ▶sub _install_api {
Subroutine _install_api: called CC=7 SCAR=20.3
180 #===================================
181 56 my ( $class, $group ) = @_;
182 56 my $defns = $class->api;
183 56 my $stash = Package::Stash->new($class);
184
185 56 my $_os_version_stash = {};
186
187 56 ▶ my $group_qr = $group ? qr/$group\./ : qr//;
Branch: 1 branch
BranchTrueFalseTotal
$group ? : 56 0 56
188 56 for my $action ( keys %$defns ) {
189 27104 ▶ my ($name) = ( $action =~ /^$group_qr([^.]+)$/ )
Branch: 1 branch
BranchTrueFalseTotal
my($name) = $action =~ /^$group_qr([^.]+)$/ or next 2408 24696 27104
190 or next;
191 2408 ▶ next if $stash->has_symbol( '&' . $name );
Branch: 1 branch
BranchTrueFalseTotal
if $stash->has_symbol('&' . $name) 0 2408 2408
192  
193 2408 my %defn = ( name => $name, %{ $defns->{$action} } );
194 2408 ▶ if (exists($defn{os_version})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $defn{'os_version'}) { } 2408 0 2408
195 2408 ▶ $_os_version_stash->{$name} = $defn{os_version} || '1.000000';
Condition50%1/2
$defn{'os_version'} || '1.000000'
l!l
24080
MC/DC0%0/1
$defn{'os_version'} || '1.000000'
$defn{'os_version'}
Truth table50%1/2
A
A= $defn{'os_version'}
Aresult
11
00
196 } else {
197 0 $_os_version_stash->{$name} = '1.000000';
198 }
199
200 $stash->add_symbol(
201 0 ▶ '&' . $name => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 0 shift->perform_request( \%defn, @_ );
203 }
204 2408 );
205 }
206
207 56 $stash->add_symbol( '%_api_method_supported_version_stash' => $_os_version_stash );
208 }
209  
210 #===================================
211 0 ▶sub _build_namespace {
Subroutine _build_namespace: not called CC=1 SCAR=6.9
212 #===================================
213 0 my ( $self, $ns ) = @_;
214 0 my $class = load_plugin( $self->_namespace, [$ns] );
215 0 return $class->new(
216 { transport => $self->transport,
217 logger => $self->logger
218 }
219 );
220 }
221  
222 #===================================
223 0 ▶sub _build_helper {
Subroutine _build_helper: not called CC=1 SCAR=6.9
224 #===================================
225 0 my ( $self, $name, $sub_class ) = @_;
226 0 my $class = load_plugin( 'OpenSearch::Client', $sub_class );
227 0 is_compat( $name . '_helper_class', $self->transport, $class );
228 0 return $class;
229 }
230  
231 56 1;
232  
233 __END__
« Client/Transport/TryCatch.pm ↑ Summary Client/Core/3_0/Helper/Scroll.pm »