blib/lib/Paws/Net/RestXmlCaller.pm

filter: Statementstmt 95.9% 165 / 172 Branchbran 96.6% 58 / 60 Conditioncond 93.7% 15 / 16 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 2 totaltotal 94.9% 261 / 275 CC 47 SCAR 19.3
CC
47
Cov
96%
_to_header_params
10.1
_to_querycaller_params
10.0
_to_xml_body
9.0
CRAP 6.9
« blib/lib/Paws/Config.pm ↑ Summary blib/lib/Paws/Net/V2Signature.pm »
1 package Paws::Net::RestXmlCaller;
2 17 ▶ use Paws;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶ use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶ use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶ use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶ use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶ use Paws::Net::RestXMLResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has response_to_object => (
13 is => 'ro',
14 94 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
15 94 Paws::Net::RestXMLResponse->new;
16 }
17 17 );
18  
19  
20 51 ▶ sub array_flatten_string {
Subroutine array_flatten_string: called CC=2 SCAR=6.9
Pod undocumented
21 51 my $self = shift;
22 51 ▶ return ($self->flattened_arrays)?'%s.%d':'%s.member.%d';
Branch: 1 branch
BranchTrueFalseTotal
$self->flattened_arrays ? : 29 22 51
23 }
24  
25 # converts the objects that represent the call into parameters that the API can understand
26 210 ▶ sub _to_querycaller_params {
Subroutine _to_querycaller_params: called CC=10 SCAR=23.0
27 210 my ($self, $params) = @_;
28  
29  
30 210 my %p;
31 210 foreach my $att (grep { $_ !~ m/^_/ } $params->meta->get_attribute_list) {
32  
33 # e.g. S3 metadata objects, which are passed in the header
34 2474 ▶ next if $params->meta->get_attribute($att)->does('Paws::API::Attribute::Trait::ParamInHeaders');
Branch: 1 branch
BranchTrueFalseTotal
if $params->meta->get_attribute($att)->does("Paws::API::Attribute::Trait::ParamInHeaders") 51 2423 2474
35  
36 2423 ▶ my $key = $params->meta->get_attribute($att)->does('Paws::API::Attribute::Trait::ParamInQuery')?$params->meta->get_attribute($att)->query_name:$att;
Branch: 1 branch
BranchTrueFalseTotal
$params->meta->get_attribute($att)->does('Paws::API::Attribute::Trait::ParamInQuery') ? : 277 2146 2423
37 2423 ▶ if (defined $params->$att) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->$att) 480 1943 2423
38 480 my $att_type = $params->meta->get_attribute($att)->type_constraint;
39  
40 480 ▶ if (Paws->is_internal_type($att_type)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Paws'->is_internal_type($att_type)) { } 407 73 480
elsif ($att_type =~ /^ArrayRef\[(.*)\]/) { } 28 45 73
41 407 $p{ $key } = $params->{$att};
42 } elsif ($att_type =~ m/^ArrayRef\[(.*)\]/) {
43 28 ▶ if (Paws->is_internal_type("$1")){
Branch: 1 branch
BranchTrueFalseTotal
if ('Paws'->is_internal_type("$1")) { } 7 21 28
44 7 my $i = 1;
45 7 foreach my $value (@{ $params->$att }){
46 10 $p{ sprintf($self->array_flatten_string, $key, $i) } = $value;
47 10 $i++
48 }
49 } else {
50 21 my $i = 1;
51 21 foreach my $value (@{ $params->$att }){
52 21 my %complex_value = $self->_to_querycaller_params($value);
53 21 map { $p{ sprintf($self->array_flatten_string . ".%s", $key, $i, $_) } = $complex_value{$_} } keys %complex_value;
54 21 $i++
55 }
56 }
57 } else {
58 45 my %complex_value = $self->_to_querycaller_params($params->$att);
59 45 map { $p{ "$key.$_" } = $complex_value{$_} } keys %complex_value;
60 }
61 }
62 }
63 210 return %p;
64 }
65  
66 287 ▶ sub _call_uri {
Subroutine _call_uri: called CC=6 SCAR=17.9
67 287 my ($self, $call) = @_;
68 287 my $uri_template = $call->meta->name->_api_uri; # in auto-lib/<service>/<method>.pm
69  
70 287 my @uri_attribs = $uri_template =~ /{(.+?)}/g;
71 287 my $vars = {};
72  
73 287 my %uri_attrib_is_greedy;
74 287 foreach my $attrib ( @uri_attribs ) {
75 485 my ($att_name, $greedy) = $attrib =~ /(\w+)(\+?)/;
76 485 $uri_attrib_is_greedy{$att_name} = $greedy;
77 }
78  
79 287 foreach my $attribute ($call->meta->get_all_attributes)
80 {
81 4669 ▶ if ($attribute->does('Paws::API::Attribute::Trait::ParamInURI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->does("Paws::API::Attribute::Trait::ParamInURI")) 415 4254 4669
82 415 my $att_name = $attribute->name;
83 415 my $non_print = join('', map { chr($_) } (128..255) );
84 415 ▶ if ($call->$att_name =~ /[{^}`\[\]><#%'"~|\\$non_print]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call->$att_name =~ /[{^}`\[\]><#%'"~|\\$non_print]/) 143 272 415
85 143 return Paws::Exception->throw(
86 message => "Found unacceptable content in $att_name parameter",
87 code => 'InvalidInput',
88 request_id => '',
89 );
90 }
91 272 ▶ if ($uri_attrib_is_greedy{$att_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_attrib_is_greedy{$att_name}) { } 62 210 272
92 62 $vars->{ $attribute->uri_name } = uri_escape_utf8($call->$att_name, q[^A-Za-z0-9\-\._~/]);
93 62 $uri_template =~ s{$att_name\+}{\+$att_name}g;
94 } else {
95 210 $vars->{ $attribute->uri_name } = $call->$att_name;
96 }
97 }
98 }
99  
100 144 my $t = URI::Template->new( $uri_template );
101 144 my $uri = $t->process($vars);
102 144 return $uri;
103 }
104  
105 144 ▶ sub _to_header_params {
Subroutine _to_header_params: called CC=10 SCAR=23.2
106 144 my ($self, $request, $call) = @_;
107 144 foreach my $attribute ($call->meta->get_all_attributes) {
108 2270 ▶ if ($attribute->does('Paws::API::Attribute::Trait::AutoInHeader')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->does("Paws::API::Attribute::Trait::AutoInHeader")) 71 2199 2270
109 71 ▶ if ( $attribute->auto eq 'MD5' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->auto eq "MD5") 71 0 71
110 71 require MIME::Base64;
111 71 require Digest::MD5;
112 71 my $value;
113 71 ▶ if ( $attribute->has_value($call) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_value($call)) { } 0 71 71
114 0 $value = $attribute->get_value($call);
115 }
116 else {
117 71 ▶ $value = MIME::Base64::encode_base64( Digest::MD5::md5( $request->content // '' ) );
Condition50%1/2
$request->content // ""
l!l
710
MC/DC0%0/1
$request->content // ""
$request->content
Truth table50%1/2
A
A= $request->content
Aresult
11
00
118 71 chomp $value;
119 }
120 71 $request->headers->header( $attribute->header_name => $value );
121 }
122 71 next;
123 }
124 2199 ▶ next unless $attribute->has_value($call);
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute->has_value($call) 1810 389 2199
125 389 ▶ if ($attribute->does('Paws::API::Attribute::Trait::ParamInHeader')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attribute->does('Paws::API::Attribute::Trait::ParamInHeader')) { } 1 388 389
elsif ($attribute->does('Paws::API::Attribute::Trait::ParamInHeaders')) { } 1 387 388
126 1 my $value = $attribute->get_value($call);
127 1 $request->headers->header( $attribute->header_name => $value );
128 }
129 elsif ($attribute->does('Paws::API::Attribute::Trait::ParamInHeaders')) {
130 1 my $map = $attribute->get_value($call)->Map;
131 1 my $prefix = $attribute->header_prefix;
132 1 for my $header (keys %{$map}) {
133 2 my $header_name = $prefix . $header;
134 2 $request->headers->header( $header_name => $map->{$header} );
135 }
136 }
137 }
138 }
139  
140 # URI escaping adapted from URI::Escape
141 #c.f. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
142 # perl 5.6 ready UTF-8 encoding adapted from JSON::PP
143 17 our %escapes = map { chr($_) => sprintf("%%%02X", $_) } 0..255;
144 17 our $unsafe_char = qr/[^A-Za-z0-9\-\._~]/;
145  
146 0 ▶ sub _uri_escape {
Subroutine _uri_escape: not called CC=1 SCAR=6.9
147 0 my ($self, $str) = @_;
148 0 utf8::encode($str);
149 0 $str =~ s/($unsafe_char)/$escapes{$1}/ge;
150 0 $str =~ s/ /+/go;
151 0 return $str;
152 }
153  
154 189 ▶ sub _attribute_to_xml {
Subroutine _attribute_to_xml: called CC=6 SCAR=17.9
155 189 my ($self, $attribute, $value) = @_;
156 189 my $xml;
157 189 my $att_name = $attribute->name;
158 189 ▶ my $location = $attribute->does('NameInRequest') ? $attribute->request_name : $att_name;
Branch: 1 branch
BranchTrueFalseTotal
$attribute->does('NameInRequest') ? : 30 159 189
159 189 ▶ if (Moose::Util::find_meta($attribute->type_constraint->name))
Branch: 3 branches
BranchTrueFalseTotal
if (Moose::Util::find_meta($attribute->type_constraint->name)) { } 45 144 189
elsif ($attribute->type_constraint eq 'ArrayRef[Str|Undef]') { } 7 137 144
elsif ($attribute->type_constraint =~ /^ArrayRef\[(.*?\:\:.*)\]/) { } 21 116 137
160 {
161 45 $xml = sprintf '<%s>%s</%s>', $location, $self->_to_xml($value), $location;
162 }
163 elsif ($attribute->type_constraint eq 'ArrayRef[Str|Undef]')
164 {
165 7 my $req_name = $attribute->request_name;
166 7 $xml = "<${att_name}>" . ( join '', map { sprintf '<%s>%s</%s>', $req_name, $_, $req_name } @{ $value } ) . "</${att_name}>";
167 }
168 elsif ($attribute->type_constraint =~ m/^ArrayRef\[(.*?\:\:.*)\]/)
169 { #assume it's an array of Paws API objects
170 21 $xml = ( join '', map { sprintf '<%s>%s</%s>', $location, $self->_to_xml($_), $location } @{ $value } );
171 21 ▶ if( !$self->flattened_arrays ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->flattened_arrays) 6 15 21
172 6 $xml = sprintf( '<%s>%s</%s>', $att_name, $xml, $att_name );
173 }
174 }
175 else
176 {
177 116 $xml = sprintf '<%s>%s</%s>', $location, $value, $location;
178 }
179 189 return $xml;
180 }
181  
182 66 ▶ sub _to_xml {
Subroutine _to_xml: called CC=3 SCAR=11.0
183 66 my ($self, $value) = @_;
184  
185 66 my $xml = '';
186 66 foreach my $attribute (sort { $a->name cmp $b->name } $value->meta->get_all_attributes) {
187 205 ▶ next if (not $attribute->has_value($value));
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute->has_value($value) 113 92 205
188 92 $xml .= $self->_attribute_to_xml($attribute, $attribute->get_value($value));
189 }
190 66 return $xml;
191 }
192  
193 144 ▶ sub _to_xml_body {
Subroutine _to_xml_body: called CC=9 SCAR=22.0
194 144 my ($self, $call) = @_;
195  
196 144 my $xml = '';
197 144 foreach my $attribute (sort { $a->name cmp $b->name } $call->meta->get_all_attributes) {
198 2270 ▶ if ($attribute->has_value($call) and
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") and not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery") and not $attribute->does("Paws::API::Attribute::Trait::ParamInURI") and not $attribute->type_constraint eq "Paws::S3::Metadata") 97 2173 2270
Condition100%12/12
$attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") and not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery") and not $attribute->does("Paws::API::Attribute::Trait::ParamInURI") and not $attribute->type_constraint eq "Paws::S3::Metadata"
!ll&&!rl&&r
2172197
$attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") and not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery") and not $attribute->does("Paws::API::Attribute::Trait::ParamInURI")
!ll&&!rl&&r
197319998
$attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") and not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery")
!ll&&!rl&&r
188291297
$attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader")
!ll&&!rl&&r
18811388
MC/DC100%5/5
$attribute->has_value($call) and not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") and not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery") and not $attribute->does("Paws::API::Attribute::Trait::ParamInURI") and not $attribute->type_constraint eq "Paws::S3::Metadata"
$attribute->has_value($call) not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader") not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery") not $attribute->does("Paws::API::Attribute::Trait::ParamInURI") not $attribute->type_constraint eq "Paws::S3::Metadata"
Truth table100%6/6
A and not(B) and not(C) and not(D) and not(E)
A= $attribute->has_value($call)B= not $attribute->does("Paws::API::Attribute::Trait::ParamInHeader")C= not $attribute->does("Paws::API::Attribute::Trait::ParamInQuery")D= not $attribute->does("Paws::API::Attribute::Trait::ParamInURI")E= not $attribute->type_constraint eq "Paws::S3::Metadata"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
199 not $attribute->does('Paws::API::Attribute::Trait::ParamInHeader') and
200 not $attribute->does('Paws::API::Attribute::Trait::ParamInQuery') and
201 not $attribute->does('Paws::API::Attribute::Trait::ParamInURI') and
202 # not $attribute->does('Paws::API::Attribute::Trait::ParamInBody') and
203 not $attribute->type_constraint eq 'Paws::S3::Metadata'
204 ) {
205 97 $xml .= $self->_attribute_to_xml($attribute, $attribute->get_value($call));
206 }
207 }
208 # Extra level of top-level wrapping, if set on the call object
209 144 ▶ if ($call->can('_top_level_element')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call->can("_top_level_element")) 9 135 144
210 9 $xml = sprintf('<%s xmlns="%s">%s</%s>',
211 $call->_top_level_element,
212 $call->_top_level_namespace,
213 $xml,
214 $call->_top_level_element
215 );
216 }
217  
218 144 ▶ return undef if (not $xml);
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 59 85 144
219 85 return $xml;
220 }
221  
222 287 ▶ sub prepare_request_for_call {
Subroutine prepare_request_for_call: called CC=8 SCAR=20.8
Pod undocumented
223 287 my ($self, $call) = @_;
224  
225 287 my $request;
226 287 ▶ if ($self->isa('Paws::S3')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('Paws::S3')) { } 273 14 287
227 273 require Paws::Net::S3APIRequest;
228 273 $request = Paws::Net::S3APIRequest->new();
229 } else {
230 14 $request = Paws::Net::APIRequest->new();
231 }
232  
233 287 my $uri = $self->_call_uri($call); #in RestXmlCaller
234  
235 144 my $qparams = { $uri->query_form };
236 144 foreach my $attribute ($call->meta->get_all_attributes) {
237 2270 my $att_name = $attribute->name;
238 2270 ▶ if ($attribute->does('Paws::API::Attribute::Trait::ParamInQuery')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->does("Paws::API::Attribute::Trait::ParamInQuery")) 277 1993 2270
239 277 ▶ $qparams->{ $attribute->query_name } = $call->$att_name if (defined $call->$att_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $call->$att_name 91 186 277
240 }
241 }
242 144 $uri->query_form(%$qparams);
243  
244 144 $request->uri($uri->as_string);
245  
246 144 my $url = $self->_api_endpoint . $uri; #in Paws::API::EndPointResolver
247  
248 #TODO: I'm not sure if any of the REST style APIs want things as query parameters
249 144 $request->parameters({ $self->_to_querycaller_params($call) });
250  
251 144 $request->url($url);
252 144 $request->method($call->_api_method);
253  
254 144 ▶ if (my $xml_body = $self->_to_xml_body($call)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $xml_body = $self->_to_xml_body($call)) 85 59 144
255 85 $request->content($xml_body);
256 }
257  
258 144 ▶ if ($call->can('_stream_param')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call->can("_stream_param")) 52 92 144
259 52 my $param_name = $call->_stream_param;
260 52 ▶ my $content = $call->$param_name // '';
Condition100%2/2
$call->$param_name // ""
l!l
502
MC/DC100%1/1
$call->$param_name // ""
$call->$param_name
Truth table100%2/2
A
A= $call->$param_name
Aresult
11
00
261 52 $request->content($content);
262 52 $request->headers->header( 'content-length' => $request->content_length );
263 #$request->headers->header( 'content-type' => $self->content_type );
264 }
265  
266 144 $self->_to_header_params($request, $call);
267  
268 144 $self->sign($request);
269  
270 144 return $request;
271 }
272 17 1;
« blib/lib/Paws/Config.pm ↑ Summary blib/lib/Paws/Net/V2Signature.pm »