Client.pm

filter: Statementstmt 47.2% 68 / 144 Branchbran 22.8% 16 / 70 Conditioncond 21.7% 5 / 23 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 84.6% 11 / 13 Podpod 100% 2 / 2 totaltotal 38.7% 103 / 266 CC 49 SCAR 52.7
CC
49
Cov
38%
_process_node
342.0
fetch
254.8
new
13.0
CRAP 195.3
↑ Summary Client/DotNet.pm »
1 package SOAP::XML::Client;
2 2 $SOAP::XML::Client::VERSION = '2.8';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use XML::LibXML 0.6;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use SOAP::Lite 0.67;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use SOAP::Data::Builder 0.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Encode qw( decode );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use vars qw($DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 my @methods = qw(results results_xml uri xmlns proxy soapversion timeout error
16 strip_default_xmlns encoding header transport status);
17  
18 # wsdk
19  
20 2 __PACKAGE__->mk_accessors(@methods);
21  
22 2 $DEBUG = 0;
23  
24 # Get an XML Parser
25 2 my $parser = XML::LibXML->new();
26 2 $parser->validation(0);
27 2 $parser->expand_entities(0);
28  
29 # which methods should be set on object constructor
30 my @config_methods
31 2 = qw(uri xmlns proxy soapversion strip_default_xmlns encoding timeout);
32  
33 5 ▶sub new {
Subroutine new: called CC=11 SCAR=25.7
Pod documented
34 5 my ( $proto, $conf ) = @_;
35 5 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
36 5 my $self = {};
37 5 bless( $self, $class );
38  
39 # Set up default soapversion and timeout
40 5 ▶ $conf->{soapversion} = '1.1' unless defined $conf->{soapversion};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"soapversion"} 2 3 5
41 5 ▶ $conf->{timeout} = '30' unless defined $conf->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"timeout"} 1 4 5
42 $conf->{strip_default_xmlns} = 1
43 5 ▶ unless defined $conf->{strip_default_xmlns};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"strip_default_xmlns"} 2 3 5
44 5 ▶ $conf->{encoding} ||= 'utf-8';
Condition100%2/2
$conf->{"encoding"} ||= "utf-8"
l!l
32
MC/DC100%1/1
$conf->{"encoding"} ||= "utf-8"
$conf->{"encoding"}
Truth table100%2/2
A
A= $conf->{"encoding"}
Aresult
11
00
45  
46 # There is a WDSL file - process it
47 5 ▶ if ( defined $conf->{wsdl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"wsdl"}) 0 5 5
48 0 $self->wsdl( $conf->{wsdl} );
49 0 $self->_process_wsdl();
50 }
51  
52 5 ▶ if ( $conf->{disable_base64} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"disable_base64"}) 0 5 5
53 0 ▶ *SOAP::Serializer::as_base64Binary = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 0 my $self = shift;
55 0 my ( $value, $name, $type, $attr ) = @_;
56 return [
57 0 $name,
58 { 'xsi:type' => 'xsd:string', %$attr },
59 SOAP::Utils::encode_data($value)
60 ];
61 0 };
62 }
63  
64 # Read in the required params
65 5 foreach my $soap_conf (@config_methods) {
66 20 ▶ unless ( defined $conf->{$soap_conf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{$soap_conf}) { } 17 3 20
67 3 croak "$soap_conf is required";
68 } else {
69 17 $self->$soap_conf( $conf->{$soap_conf} );
70 }
71 }
72  
73 2 ▶ $self->header( $conf->{header} ) if $conf->{header};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"header"} 0 2 2
74  
75 # Set up the SOAP object
76 2 $self->{soap} = SOAP::Lite->new;
77  
78 # We want the raw XML back
79 2 $self->{soap}->outputxml(1);
80  
81 2 return $self;
82  
83 }
84  
85 #sub _process_wsdl {
86 # my $self = shift;
87 # my $services = SOAP::Schema->schema_url($self->wsdl())->parse()->services();
88 # use Data::Dumper; #print STDERR Dumper($services);
89 #
90 # foreach my $class (values %$services) {
91 # print "C: $class\n";
92 # foreach my $method (keys %$class) {
93 # print "M: $method\n";
94 # print Dumper($class->{$method});
95 # $self->{proxies}->{$method} = $class->{$method}->{endpoint}->value();
96 # $self->{uris}->{$method} = $class->{$method}->{uri}->value();
97 # }
98 # }
99 #}
100  
101 1 ▶sub fetch {
Subroutine fetch: called CC=22 SCAR=55.4
Pod documented
102 1 my ( $self, $conf ) = @_;
103  
104 # Reset the error so that the object ca be reused
105 1 $self->error(undef);
106  
107 # Got to have a method!
108 1 ▶ if ( !defined $conf->{method} or $conf->{method} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $conf->{"method"} or $conf->{"method"} eq "") 0 1 1
Condition33%1/3
not defined $conf->{"method"} or $conf->{"method"} eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $conf->{"method"} or $conf->{"method"} eq ""
not defined $conf->{"method"} $conf->{"method"} eq ""
Truth table33%1/3
not(A) or B
A= not defined $conf->{"method"}B= $conf->{"method"} eq ""
ABresult
1X1
011
000
109 0 $self->error('You must supply a method name');
110 0 return undef;
111 }
112  
113 # Got to get xml from somewhere!
114 1 ▶ if ( !defined $conf->{xml} && !defined $conf->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf->{"xml"} or defined $conf->{"filename"}) 0 1 1
Condition33%1/3
defined $conf->{"xml"} or defined $conf->{"filename"}
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $conf->{"xml"} or defined $conf->{"filename"}
defined $conf->{"xml"} defined $conf->{"filename"}
Truth table33%1/3
A or B
A= defined $conf->{"xml"}B= defined $conf->{"filename"}
ABresult
1X1
011
000
115 0 $self->error(
116 "You must supply either the 'xml' or the 'filename' to use");
117 0 return undef;
118 }
119  
120 # Check the filename if supplied
121 1 ▶ if ( defined $conf->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"filename"}) 0 1 1
122  
123 # Got a filename, see if it is readable
124 0 ▶ unless ( -r $conf->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $conf->{'filename'}) { } 0 0 0
125 0 $self->error( "Unable to read: " . $conf->{filename} );
126 0 return undef;
127 } else {
128  
129 # Ok, read it in
130 0 my $file_xml = read_file( $conf->{filename} );
131 0 $conf->{xml} = $file_xml;
132 }
133 }
134  
135 # create a builder
136 1 $self->{sdb} = SOAP::Data::Builder->new();
137  
138 1 ▶ unless ( $conf->{xml} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{"xml"} eq "") 1 0 1
139  
140 # add some wrapping paper so XML::LibXML likes it with no top level
141 my $xml_data
142 1 = '<soap_lite_wrapper>' . $conf->{xml} . '</soap_lite_wrapper>';
143 1 my $xml;
144 1 eval { $xml = $parser->parse_string($xml_data) };
145 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
146 1 $self->error( 'Error parsing your XML: ' . $@ );
147 1 return undef;
148 }
149  
150 # Create the SOAP data from the XML
151 0 my $nodes = $xml->childNodes;
152 0 my $top = $nodes->get_node(1); # our wrapper
153 0 ▶ if ( my $nodes = $top->childNodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nodes = $top->childNodes) 0 0 0
154 0 foreach my $node ( @{$nodes} ) {
155 0 $self->_process_node( { node => $node } );
156 }
157 }
158 }
159  
160 ################
161 ## Execute the call and get the result back
162 ################
163  
164 0 ▶ carp "About to run _call()" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
165  
166 #use Data::Dumper;
167 #print Dumper($self->{sdb}->to_soap_data());
168 #my $serialized_xml = SOAP::Serializer->autotype(0)->serialize( $self->{sdb}->to_soap_data() );
169 #carp "IF WE GET HERE IT WORKED!!!!!!!";
170 #print Dumper($self->{sdb}->elems());
171  
172 # execute the call in the relevant style done by the child object
173 0 my ( $res, $transport ) = $self->_call( $conf->{method} );
174  
175 0 $self->transport( $transport );
176 0 $self->status( $transport->status );
177  
178 # TODO: actually need to specify encoding expected in return (or parse from response?)
179 0 $res = decode( $self->{encoding}, $res );
180  
181 0 ▶ carp "After run _call()" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
182  
183 0 ▶ if ( !defined $res or $res =~ /^\d/ or !$transport->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $res or $res =~ /^\d/ or not $transport->is_success) { } 0 0 0
Condition0%0/6
not defined $res or $res =~ /^\d/ or not $transport->is_success
l!l&&r!l&&!r
000
not defined $res or $res =~ /^\d/
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $res or $res =~ /^\d/ or not $transport->is_success
not defined $res $res =~ /^\d/ not $transport->is_success
Truth table0%0/4
not(A) or B or not(C)
A= not defined $resB= $res =~ /^\d/C= not $transport->is_success
ABCresult
1XX1
01X1
0011
0000
184  
185 # Got a web error - if it was XML it wouldn't start with a digit!
186 0 $self->error($res);
187 0 return undef;
188 } else {
189  
190 # Strip out default name space stuff as it makes it hard
191 # to parse and there's no reason for it I can see!
192 0 ▶ $res =~ s/xmlns=".*?"//g if $self->strip_default_xmlns();
Branch: 1 branch
BranchTrueFalseTotal
if $self->strip_default_xmlns 0 0 0
193  
194 # Generate xml object from the responce
195 0 my $res_xml;
196 0 eval { $res_xml = $parser->parse_string($res) };
197 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
198  
199 # Not valid xml
200 0 $self->error( 'Unable to parse returned data as XML: ' . $@ );
201 0 return undef;
202 } else {
203  
204 # Now look for faults
205 0 ▶ if ( my $nodes = $res_xml->findnodes("//faultstring") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nodes = $res_xml->findnodes("//faultstring")) 0 0 0
206  
207 # loop through faultstrings - checking it's parent is 'Fault'
208 # We do not care about namespaces
209 0 foreach my $node ( $nodes->get_nodelist() ) {
210 0 my $parentnode = $node->parentNode();
211 0 ▶ if ( $parentnode->nodeName() =~ /Fault/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parentnode->nodeName =~ /Fault/) 0 0 0
212  
213 # There is a "(*:)Fault/faultstring"
214 # get the human readable string
215 0 $self->error(
216 $nodes->get_node(1)->findvalue( '.', $nodes ) );
217 0 last;
218 }
219 }
220 }
221  
222 # See if there was a fault
223 0 ▶ return undef if $self->error();
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
224  
225 # All looking good
226 0 $self->results_xml($res_xml);
227 0 $self->results($res);
228  
229 # I tried just return; but it didn't like it!
230 0 return 1;
231 }
232 }
233 }
234  
235 ### Private methods
236  
237 # Convert the XML to SOAP::Data::Builder
238 0 ▶sub _process_node {
Subroutine _process_node: not called CC=18 SCAR=58.3
239 0 my ( $self, $conf ) = @_;
240  
241 # We never access text nodes directly, only via the parent node
242 0 ▶ return if $conf->{node}->nodeType == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"node"}->nodeType == 3 0 0 0
243  
244 0 ▶ carp "PROCESSING: " . $conf->{node}->nodeName() if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
245  
246 # Set up the parent if there was one
247 0 my $parent = undef;
248 0 ▶ $parent = $conf->{parent} if defined $conf->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"parent"} 0 0 0
249  
250 0 ▶ if ( $DEBUG && defined $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SOAP::XML::Client::DEBUG and defined $parent) 0 0 0
Condition0%0/3
$SOAP::XML::Client::DEBUG and defined $parent
!ll&&!rl&&r
000
MC/DC0%0/2
$SOAP::XML::Client::DEBUG and defined $parent
$SOAP::XML::Client::DEBUG defined $parent
Truth table0%0/3
A and B
A= $SOAP::XML::Client::DEBUGB= defined $parent
ABresult
0X0
100
111
251 0 carp "PARENT NAME:" . $parent->{fullname};
252 }
253  
254 0 my $type = undef;
255  
256 # Extract the attributes from the node
257 0 my %attribs;
258 0 foreach my $att ( $conf->{node}->attributes() ) {
259  
260 # skip anything which isn't defined!
261 0 ▶ next unless defined $att;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $att 0 0 0
262  
263 # Check if it's our 'special' value
264 0 ▶ if ( $att->name() eq '_value_type' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($att->name eq '_value_type') { } 0 0 0
265 0 $type = $att->value();
266 } else {
267 0 $attribs{ $att->name() } = $att->value();
268 }
269 }
270  
271 0 my @t = $conf->{node}->childNodes();
272  
273 # If we have 1 child and that child is text then use the content
274 # of the child as our value we must also be at the end of the tree
275 0 ▶ if ( scalar(@t) == 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @t == 1 and $conf->{'node'}->childNodes->get_node(1)->nodeType == 3) { } 0 0 0
Condition0%0/3
scalar @t == 1 and $conf->{"node"}->childNodes->get_node(1)->nodeType == 3
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @t == 1 and $conf->{"node"}->childNodes->get_node(1)->nodeType == 3
scalar @t == 1 $conf->{"node"}->childNodes->get_node(1)->nodeType == 3
Truth table0%0/3
A and B
A= scalar @t == 1B= $conf->{"node"}->childNodes->get_node(1)->nodeType == 3
ABresult
0X0
100
111
276 && $conf->{node}->childNodes()->get_node(1)->nodeType() == 3 )
277 {
278  
279 #return;
280 0 my $value = $conf->{node}->childNodes()->get_node(1)->textContent();
281 0 ▶ carp "ADDING : " . $conf->{node}->nodeName . " Value: $value"
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
282 if $DEBUG;
283 $self->{sdb}->add_elem(
284 name => $conf->{node}->nodeName,
285 0 attributes => \%attribs,
286 parent => $parent,
287 value => $value,
288 type => $type,
289 );
290  
291 0 ▶ carp "END OF THE LINE BUDDY!" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
292 } else {
293 0 ▶ carp "- FOUND CHILD NODES" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
294  
295 # Add it - it's a node without a value, but has child nodes
296 0 my $obj;
297 0 ▶ if ( defined $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) { } 0 0 0
298 carp "ADDING ELEMENT WITH PARENT: " . $conf->{node}->nodeName
299 0 ▶ if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
300  
301 # Add with the parent
302 $obj = $self->{sdb}->add_elem(
303 name => $conf->{node}->nodeName,
304 0 attributes => \%attribs,
305 parent => $parent,
306 );
307 } else {
308 carp "ADDING ELEMENT WITH NO PARENT: " . $conf->{node}->nodeName
309 0 ▶ if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $SOAP::XML::Client::DEBUG 0 0 0
310  
311 # Add with the parent
312 # Add without parent
313 $obj = $self->{sdb}->add_elem(
314 name => $conf->{node}->nodeName,
315 0 attributes => \%attribs,
316 );
317 }
318  
319 0 foreach my $node ( $conf->{node}->childNodes() ) {
320  
321 # process each child node as long as it's not
322 # a text node (type 3)
323 0 $self->_process_node(
324 { 'node' => $node,
325 'parent' => $obj,
326 }
327 );
328 }
329 }
330 }
331  
332 2 1;
333  
334 __END__
↑ Summary Client/DotNet.pm »