lib/Mojo/SOAP/Client.pm

filter: Statementstmt 87.7% 93 / 106 Branchbran 40.9% 9 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.8% 22 / 29 Podpod 66.6% 2 / 3 totaltotal 76.9% 127 / 165 CC 15 SCAR 11.7
CC
15
Cov
79%
__ANON__
7.3
__ANON__
6.0
call
6.0
CRAP 3.2
↑ Summary
1 package Mojo::SOAP::Client;
2  
3 =pod
4  
5 =begin markdown
6  
7 ![](https://github.com/oposs/mojo-soap-client/workflows/Unit%20Tests/badge.svg?branch=master)
8  
9 =end markdown
10  
11 =head1 NAME
12  
13 Mojo::SOAP::Client - Talk to SOAP Services mojo style
14  
15 =head1 SYNPOSYS
16  
17 use Mojo::SOAP::Client;
18 use Mojo::File qw(curfile);
19 my $client = Mojo::SOAP::Client->new(
20 wsdl => curfile->sibling('fancy.wsdl'),
21 xsds => [ curfile->sibling('fancy.xsd')],
22 port => 'FancyPort'
23 );
24  
25 $client->call_p('getFancyInfo',{
26 color => 'green'
27 })->then(sub {
28 my $answer = shift;
29 my $trace = shift;
30 });
31  
32 =head1 DESCRIPTION
33  
34 The Mojo::SOAP::Client is based on the L<XML::Compile::SOAP>
35 family of packages, and especially on L<XML::Compile::SOAP::Mojolicious>.
36  
37 =cut
38  
39 2 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 2 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use XML::Compile::WSDL11; # use WSDL version 1.1
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use XML::Compile::SOAP11; # use SOAP version 1.1
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use XML::Compile::SOAP12;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use XML::Compile::Transport::SOAPHTTP_MojoUA;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Mojo::Util qw(b64_encode dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 our $VERSION = '0.1.8';
53  
54 =head2 Properties
55  
56 The module provides the following properties to customize its behavior. Note that setting any properties AFTER using the C<call> or C<call_p> methods, will lead to undefined behavior.
57  
58 =head3 log
59  
60 a pointer to a L<Mojo::Log> instance
61  
62 =cut
63  
64 0 ▶has log => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 Mojo::Log->new;
66 2 };
67  
68 =head3 request_timeout
69  
70 How many seconds to wait for the soap server to respond. Defaults to 5 seconds.
71  
72 =cut
73  
74 2 has request_timeout => 5;
75  
76 =head3 insecure
77  
78 Set this to allow communication with a soap server that uses a
79 self-signed or otherwhise invalid certificate.
80  
81 =cut
82  
83 2 has insecure => 0;
84  
85 =head3 wsdl
86  
87 Where to load the wsdl file from. At the moment this MUST be a file.
88  
89 =cut
90  
91 0 ▶has 'wsdl' => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 croak "path to wsdl spec file must be provided in wsdl property";
93 2 };
94  
95 =head3 xsds
96  
97 A pointer to an array of xsd files to load for this service.
98  
99 =cut
100  
101 0 ▶has 'xsds' => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 [];
103 2 };
104  
105 =head3 port
106  
107 If the wsdl file defines multiple ports, pick the one to use here.
108  
109 =cut
110  
111 2 has 'port';
112  
113 =head3 endPoint
114  
115 The endPoint to talk to for reaching the SOAP service. This information
116 is normally encoded in the WSDL file, so you will not have to set this
117 explicitly.
118  
119 =cut
120  
121  
122 0 ▶has 'endPoint' => sub ($self) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
123 0 ▶ $self->wsdlCompiler->endPoint(
Branch: 1 branch
BranchTrueFalseTotal
$self->port ? : 0 0 0
124 $self->port ? ( port => $self->port) : ()
125 );
126 2 };
127  
128 =head3 ca
129  
130 The CA cert of the service. Only for special applications.
131  
132 =cut
133  
134 2 has 'ca';
135  
136 =head3 cert
137  
138 The client certificate to use when connecting to the soap service.
139  
140 =cut
141  
142 2 has 'cert';
143  
144 =head3 key
145  
146 The key matching the client cert.
147  
148 =cut
149  
150 2 has 'key';
151 2 has 'ua';
152  
153 1 ▶has wsdlCompiler => sub ($self) {
Subroutine __ANON__: called CC=2 SCAR=6.9
154 1 my $wc = XML::Compile::WSDL11->new($self->wsdl);
155 1 for my $xsd ( @{$self->xsds}) {
156 1 $wc->importDefinitions($xsd)
157 }
158 1 return $wc;
159 2 };
160  
161 1 ▶has httpUa => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.3
162 XML::Compile::Transport::SOAPHTTP_MojoUA->new(
163 address => $self->endPoint,
164 mojo_ua => $self->ua,
165 1 ▶ ua_start_callback => sub ($ua,$tx) {
Subroutine __ANON__: called CC=6 SCAR=19.9
166 1 ▶ $ua->ca($self->ca)
Branch: 1 branch
BranchTrueFalseTotal
if $self->ca 0 1 1
167 if $self->ca;
168 1 ▶ $ua->cert($self->cert)
Branch: 1 branch
BranchTrueFalseTotal
if $self->cert 0 1 1
169 if $self->cert;
170 1 ▶ $ua->key($self->key)
Branch: 1 branch
BranchTrueFalseTotal
if $self->key 0 1 1
171 if $self->key;
172 1 ▶ $ua->request_timeout($self->request_timeout)
Branch: 1 branch
BranchTrueFalseTotal
if $self->request_timeout 1 0 1
173 if $self->request_timeout;
174 1 ▶ $ua->insecure($self->insecure)
Branch: 1 branch
BranchTrueFalseTotal
if $self->insecure 0 1 1
175 if $self->insecure;
176 },
177 1 );
178 2 };
179  
180 =head3 uaProperties
181  
182 If special properties must be set on the UA you can set them here. For example a special authorization header was required, this would tbe the place to set it up.
183  
184 my $client = Mojo::SOAP::Client->new(
185 ...
186 uaProperties => {
187 header => HTTP::Headers->new(
188 Authorization => 'Basic '. b64_encode("$user:$password","")
189 })
190 }
191 );
192  
193 =cut
194  
195 1 ▶has uaProperties => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
196 {}
197 1 };
198  
199 1 ▶has transport => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
200 $self->httpUa->compileClient(
201 1 %{$self->uaProperties}
202 );
203 2 };
204  
205 1 ▶has clients => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 1 return {};
207 2 };
208  
209 =head2 Methods
210  
211 The module provides the following methods.
212  
213 =head3 call_p($operation,$params)
214  
215 Call a SOAP operation with parameters and return a L<Mojo::Promise>.
216  
217 $client->call_p('queryUsers',{
218 query => {
219 detailLevels => {
220 credentialDetailLevel => 'LOW',
221 userDetailLevel => 'MEDIUM',
222 userDetailLevel => 'LOW',
223 defaultDetailLevel => 'EXCLUDE'
224 },
225 user => {
226 loginId => 'aakeret'
227 }
228 numRecords => 100,
229 skipRecords => 0,
230 }
231 })->then(sub ($anwser,$trace) {
232 print Dumper $answer
233 });
234  
235 =cut
236  
237 1 ▶sub call_p ($self,$operation,$params={}) {
Subroutine call_p: called CC=4 SCAR=15.4
Pod documented
238 1 my $clients = $self->clients;
239 1 ▶ my $call = $clients->{$operation} //= $self->wsdlCompiler->compileClient(
Branch: 1 branch
BranchTrueFalseTotal
$self->port ? : 0 1 1
Condition33%1/3
$clients->{$operation} //= $self->wsdlCompiler->compileClient("operation", $operation, "transport", $self->transport, "async", 1, $self->port ? ("port", $self->port) : ())
l!l&&r!l&&!r
010
MC/DC0%0/2
$clients->{$operation} //= $self->wsdlCompiler->compileClient("operation", $operation, "transport", $self->transport, "async", 1, $self->port ? ("port", $self->port) : ())
$clients->{$operation} $self->wsdlCompiler->compileClient("operation", $operation, "transport", $self->transport, "async", 1, $self->port ? ("port", $self->port) : ())
Truth table33%1/3
A //= B
A= $clients->{$operation}B= $self->wsdlCompiler->compileClient("operation", $operation, "transport", $self->transport, "async", 1, $self->port ? ("port", $self->port) : ())
ABresult
1X1
011
000
240 operation => $operation,
241 transport => $self->transport,
242 async => 1,
243 # oddly repetitive, the port is mentioned in the endPoint
244 # selection as well as here ...
245 ( $self->port ? ( port => $self->port ) : () ),
246 );
247 1 $self->log->debug(__PACKAGE__ . " $operation called");
248 1 ▶ return Mojo::Promise->new(sub ($resolve,$reject) {
Subroutine __ANON__: called CC=1 SCAR=0.5
249 $call->(
250 %$params,
251 1 ▶ _callback => sub ($answer,$trace,@rest) {
Subroutine __ANON__: called CC=4 SCAR=16.1
252 1 my $res = $trace->response;
253 1 my $client_warning =
254 $res->headers->header('client-warning');
255 1 ▶ return $reject->($client_warning."\n".$self->trace_to_string($trace))
Branch: 1 branch
BranchTrueFalseTotal
if $client_warning 0 1 1
256 if $client_warning;
257 1 ▶ if (not $res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 1 0 1
258 1 ▶ if (my $f = $answer->{Fault}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $answer->{"Fault"}) 0 1 1
259 0 $self->log->error(__PACKAGE__ . " $operation - ".$f->{_NAME} .": ". $f->{faultstring});
260 0 return $reject->($f->{faultstring}."\n".$self->trace_to_string($trace));
261 }
262 1 return $reject->($self->endPoint.' - '.$res->code.' '.$res->message."\n".$self->trace_to_string($trace))
263 }
264 # $self->log->debug(__PACKAGE__ . " $operation completed - ".dumper($answer));
265 0 return $resolve->($answer,$trace);
266 }
267 1 );
268 1 });
269 }
270  
271 1 ▶sub trace_to_string ($self,$trace) {
Subroutine trace_to_string: called CC=1 SCAR=0.0
Pod undocumented
272 1 my $ret;
273 1 open my $fh, '>', \$ret;
274 1 $trace->printErrors($fh);
275 1 print $fh "\nRequest:\n";
276 1 $trace->printRequest($fh,pretty_print=>1);
277 1 print $fh "\nResponse:\n";
278 1 $trace->printResponse($fh,pretty_print=>1);
279 1 print $fh "\n";
280 1 $trace->printTimings($fh);
281 1 close $fh;
282 1 return $ret;
283 }
284  
285 =head3 call($operation,$paramHash)
286  
287 The same as C<call_p> but for syncronos applications. If there is a problem with the call it will raise a Mojo::SOAP::Exception which is a L<Mojo::Exception> child.
288  
289 =cut
290  
291 0 ▶sub call ($self,$operation,$params) {
Subroutine call: not called CC=2 SCAR=17.9
Pod documented
292 0 my ($ret,$err);
293 $self->call_p($operation,$params)
294 0 ▶ ->then(sub { $ret = shift })
Subroutine __ANON__: not called CC=1 SCAR=6.9
295 0 ▶ ->catch(sub { $err = shift })
Subroutine __ANON__: not called CC=1 SCAR=6.9
296 0 ->wait;
297 0 ▶ Mojo::SOAP::Exception->throw($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
298 0 return $ret;
299 }
300  
301 2 package Mojo::SOAP::Exception {
302 2 ▶ use Mojo::Base 'Mojo::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
303 }
304  
305 2 1;
306  
307 =head1 ACKNOWLEDGEMENT
308  
309 This is really just a very thin layer on top of Mark Overmeers great L<XML::Compile::SOAP> module. Thanks Mark!
310  
311 =head1 AUTHOR
312  
313 S<Tobias Oetiker, E<lt>tobi@oetiker.chE<gt>>
314  
315 =head1 COPYRIGHT
316  
317 Copyright OETIKER+PARTNER AG 2019
318  
319 =head1 LICENSE
320  
321 This library is free software; you can redistribute it and/or modify
322 it under the same terms as Perl itself, either Perl version 5.10 or,
323 at your option, any later version of Perl 5 you may have available.
324  
325 =cut
↑ Summary