MWS/Routines.pm

filter: Statementstmt 67.9% 140 / 206 Branchbran 47.0% 32 / 68 Conditioncond 29.1% 14 / 48 MC/DCmcdc 6.9% 2 / 29 Subroutinesub 95.2% 20 / 21 Podpod 0.0% 0 / 6 totaltotal 55.0% 208 / 378 CC 58 SCAR 44.8
CC
58
Cov
58%
__ANON__
153.5
new
105.7
force_array
12.0
CRAP 88.0
« MWS/XML/Product.pm ↑ Summary MWS/XML/Response/FeedSubmissionResult.pm »
1 package Amazon::MWS::Routines;
2  
3 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Digest::MD5 qw(md5_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Amazon::MWS::TypeMap qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Amazon::MWS::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 our @EXPORT_OK = qw(define_api_method new sign_request convert force_array);
18 8 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
19  
20 474 ▶sub slurp_kwargs { ref $_[0] eq 'HASH' ? shift : { @_ } }
Subroutine slurp_kwargs: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 474 474
21  
22 472 ▶sub define_api_method {
Subroutine define_api_method: called CC=2 SCAR=8.8
Pod undocumented
23 472 my $method_name = shift;
24 472 my $spec = slurp_kwargs(@_);
25 472 my $params = $spec->{parameters};
26  
27 2 ▶ my $method = sub {
Subroutine __ANON__: called CC=31 SCAR=50.3
28  
29 2 my $self = shift;
30 2 my $args = slurp_kwargs(@_);
31 2 my $body = '';
32  
33 my %form = (
34 Action => $method_name,
35 AWSAccessKeyId => $self->{access_key_id},
36 Merchant => $self->{merchant_id},
37 SellerId => $self->{merchant_id},
38 2 SignatureVersion => 2,
39 SignatureMethod => 'HmacSHA256',
40 Timestamp => to_amazon('datetime', DateTime->now),
41 );
42  
43 2 foreach my $name (keys %$params) {
44 4 my $param = $params->{$name};
45 4 ▶ unless (exists $args->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$name}) 1 3 4
46 1 ▶ Amazon::MWS::Exception::MissingArgument->throw(name => $name) if $param->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"required"} 0 1 1
47 1 next;
48 }
49  
50 3 my $type = $param->{type};
51 3 my $array_names = $param->{array_names};
52 3 my $value = $args->{$name};
53  
54 3 ▶ if ($type =~ /^List$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^List$/) 0 3 3
55 0 my %valuehash;
56 0 @valuehash{@{$param->{values}}}=();
57 0 ▶ Amazon::MWS::Exception::Invalid->throw(field => $name, value=>$value) unless (exists ($valuehash{$value}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valuehash{$value} 0 0 0
58 0 $form{$name} = $value;
59 0 next;
60 }
61  
62 # Odd 'structured list' notation handled here
63 3 ▶ if ($type =~ /(\w+)List/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(\w+)List/) 0 3 3
64 0 my $list_type = $1;
65 0 ▶ Amazon::MWS::Exception::Invalid->throw(field => $name, value=>$value, message=>"$name should be of type ARRAY") unless (ref $value eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
66 0 my $counter = 1;
67  
68 0 foreach my $sub_value (@$value) {
69 0 my $listKey = "$name.$list_type." . $counter++;
70 0 $form{$listKey} = $sub_value;
71 }
72 0 next;
73 }
74  
75 3 ▶ if ($type =~ /(\w+)Array/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /(\w+)Array/) 0 3 3
76 0 ▶ Amazon::MWS::Exception::Invalid->throw(field => $name, value=>$value, message=>"$name should be of type ARRAY") unless (ref $value eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
77 0 my $list_type = $1;
78 0 my $counter = 0;
79 0 foreach my $sub_value (@$value) {
80 0 $counter++;
81 0 my $arr_col=0;
82 0 foreach my $array_name (@{$array_names}) {
83 0 ▶ if ( ! defined $sub_value->[$arr_col] ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub_value->[$arr_col]) 0 0 0
84 0 my $listKey = "$name.$list_type." . $counter;
85 0 $listKey .= ".$array_name";
86 0 $form{$listKey} = $sub_value->[$arr_col++];
87 }
88 }
89 0 next;
90 }
91 3 ▶ if ($type eq 'HTTP-BODY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HTTP-BODY') { } 1 2 3
92 1 $body = $value;
93 }
94 else {
95 2 $form{$name} = to_amazon($type, $value);
96 }
97 }
98  
99 2 ▶ $form{Version} = $spec->{version} || '2010-01-01';
Condition50%1/2
$spec->{'version'} || '2010-01-01'
l!l
20
MC/DC0%0/1
$spec->{'version'} || '2010-01-01'
$spec->{'version'}
Truth table50%1/2
A
A= $spec->{'version'}
Aresult
11
00
100  
101 2 ▶ my $endpoint = ( $spec->{service} ) ? "$self->{endpoint}$spec->{service}" : $self->{endpoint};
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'service'} ? : 1 1 2
102  
103 2 my $uri = URI->new($endpoint);
104  
105 2 my $request = HTTP::Request->new;
106 2 $request->protocol('HTTP/1.0');
107  
108 2 my ($response, $content);
109  
110 2 ▶ $spec->{method} = 'GET' unless $spec->{method};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"method"} 2 0 2
111  
112 2 ▶ if ($spec->{method} eq 'POST') {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'method'} eq 'POST') { } 0 2 2
elsif ($body) { } 1 1 2
113 0 $request->uri($uri);
114 0 $request->method('POST');
115 0 $request->content($body);
116 0 ▶ $request->content_type($args->{content_type}||'application/x-www-form-urlencoded');
Condition0%0/2
$args->{'content_type'} || 'application/x-www-form-urlencoded'
l!l
00
MC/DC0%0/1
$args->{'content_type'} || 'application/x-www-form-urlencoded'
$args->{'content_type'}
Truth table0%0/2
A
A= $args->{'content_type'}
Aresult
11
00
117 0 my $signature = $self->sign_request($request, %form);
118  
119 0 $response = $self->agent->request($request);
120 0 $content = $response->content;
121 } elsif ($body) {
122 1 $request->uri($uri);
123 1 $request->method('POST');
124 1 $request->content($body);
125 1 $request->header('Content-MD5' => md5_base64($body) . '==');
126 1 ▶ $request->content_type($args->{content_type}||'text/plain');
Condition50%1/2
$args->{'content_type'} || 'text/plain'
l!l
10
MC/DC0%0/1
$args->{'content_type'} || 'text/plain'
$args->{'content_type'}
Truth table50%1/2
A
A= $args->{'content_type'}
Aresult
11
00
127  
128 1 $self->sign_request($request, %form);
129 1 $request->content($body);
130 1 $response = $self->agent->request($request);
131 1 $content = $response->content;
132 } else {
133 1 $uri->query_form(\%form);
134 1 $request->uri($uri);
135 1 $request->method('GET');
136  
137 1 $self->sign_request($request);
138
139 1 $response = $self->agent->request($request);
140 1 $content = $response->content;
141  
142 }
143  
144  
145 2 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 2 2
146 0 open LOG, ">>$self->{logfile}";
147 0 print LOG Dumper($response);
148 }
149  
150 2 my $xs = XML::Simple->new( KeepRoot => 1 );
151  
152 2 ▶ if ($response->code == 400 || $response->code == 403) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 400 or $response->code == 403) 0 2 2
Condition33%1/3
$response->code == 400 or $response->code == 403
l!l&&r!l&&!r
002
MC/DC0%0/2
$response->code == 400 or $response->code == 403
$response->code == 400 $response->code == 403
Truth table33%1/3
A or B
A= $response->code == 400B= $response->code == 403
ABresult
1X1
011
000
153 0 my $hash = $xs->xml_in($content);
154 0 my $root = $hash->{ErrorResponse};
155 0 force_array($root, 'Error');
156 0 Amazon::MWS::Exception::Response->throw(errors => $root->{Error}, xml => $content);
157 }
158  
159 2 ▶ if ($response->code == 503) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 503) 0 2 2
160 0 my $hash = $xs->xml_in($content);
161 0 my $root = $hash->{ErrorResponse};
162 0 force_array($root, 'Error');
163 0 Amazon::MWS::Exception::Throttled->throw(errors => $root->{Error}, xml => $content);
164 }
165  
166 2 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 2 2
167 0 Amazon::MWS::Exception::Transport->throw(request => $request, response => $response);
168 }
169  
170 2 ▶ if (my $md5 = $response->header('Content-MD5')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $md5 = $response->header("Content-MD5")) 0 2 2
171 0 ▶ Amazon::MWS::Exception::BedChecksum->throw(response => $response)
Branch: 1 branch
BranchTrueFalseTotal
unless $md5 eq md5_base64($content) . '==' 0 0 0
172 unless ($md5 eq md5_base64($content) . '==');
173 }
174  
175 2 ▶ return $content if ($spec->{raw_body} || $args->{raw_body});
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"raw_body"} or $args->{"raw_body"} 0 2 2
Condition33%1/3
$spec->{"raw_body"} or $args->{"raw_body"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$spec->{"raw_body"} or $args->{"raw_body"}
$spec->{"raw_body"} $args->{"raw_body"}
Truth table33%1/3
A or B
A= $spec->{"raw_body"}B= $args->{"raw_body"}
ABresult
1X1
011
000
176  
177 2 my $hash = $xs->xml_in($content);
178  
179 my $root = $hash->{$method_name . 'Response'}
180 2 ->{$method_name . 'Result'};
181  
182 2 return $spec->{respond}->($root);
183 472 };
184  
185 472 ▶ my $module_name = $spec->{module_name} || 'Amazon::MWS::Client';
Condition100%2/2
$spec->{'module_name'} || 'Amazon::MWS::Client'
l!l
40432
MC/DC100%1/1
$spec->{'module_name'} || 'Amazon::MWS::Client'
$spec->{'module_name'}
Truth table100%2/2
A
A= $spec->{'module_name'}
Aresult
11
00
186 472 my $fqn = join '::', "$module_name", $method_name;
187  
188 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 472 *$fqn = $method;
190  
191 }
192  
193 0 ▶sub force_array {
Subroutine force_array: not called CC=3 SCAR=24.8
Pod undocumented
194  
195 0 my ($hash, $key) = @_;
196 0 my $val = $hash->{$key};
197  
198 0 ▶ if (!defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (ref $val ne 'ARRAY') { } 0 0 0
199 0 $val = [];
200 }
201 elsif (ref $val ne 'ARRAY') {
202 0 $val = [ $val ];
203 }
204  
205 0 $hash->{$key} = $val;
206 }
207  
208 2 ▶sub sign_request {
Subroutine sign_request: called CC=6 SCAR=18.0
Pod undocumented
209 2 my ($self, $request, %form) = @_;
210  
211 2 my $uri = $request->uri;
212 2 ▶ my %params = ($request->method eq 'GET' ) ? $uri->query_form : %form;
Branch: 1 branch
BranchTrueFalseTotal
$request->method eq 'GET' ? : 1 1 2
213  
214 my $canonical = join '&', map {
215 2 my $param = uri_escape($_);
216 18 my $value = uri_escape($params{$_});
217 18 "$param=$value";
218 } sort keys %params;
219  
220 2 ▶ my $path = $uri->path || '/';
Condition100%2/2
$uri->path || '/'
l!l
11
MC/DC100%1/1
$uri->path || '/'
$uri->path
Truth table100%2/2
A
A= $uri->path
Aresult
11
00
221 2 my $string = $request->method . "\n"
222 . $uri->authority . "\n"
223 . $path . "\n"
224 . $canonical;
225  
226 2 $params{Signature} = Digest::SHA::hmac_sha256_base64($string, $self->{secret_key});
227 2 ▶ while (length($params{Signature}) % 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($params{"Signature"}) % 4) 2 2 4
228 2 $params{Signature} .= '=';
229 }
230  
231 2 ▶ if ($request->{_method} eq 'GET' || $request->{_content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{'_method'} eq 'GET' or $request->{'_content'}) { } 2 0 2
Condition66%2/3
$request->{"_method"} eq "GET" or $request->{"_content"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$request->{"_method"} eq "GET" or $request->{"_content"}
$request->{"_method"} eq "GET" $request->{"_content"}
Truth table66%2/3
A or B
A= $request->{"_method"} eq "GET"B= $request->{"_content"}
ABresult
1X1
011
000
232 2 $uri->query_form(\%params);
233 } else {
234 0 $request->{_content} = "$canonical&Signature=$params{Signature}";
235 }
236 2 $request->uri($uri);
237 2 return $request;
238  
239 }
240  
241 1 ▶sub convert {
Subroutine convert: called CC=1 SCAR=0.0
Pod undocumented
242 1 my ($hash, $key, $type) = @_;
243 1 $hash->{$key} = from_amazon($type, $hash->{$key});
244 }
245  
246  
247 2 ▶sub new {
Subroutine new: called CC=19 SCAR=46.6
Pod undocumented
248
249 2 my($pkg, %opts) = @_;
250  
251 2 ▶ $opts{configfile} ||= 'amazon.xml';
Condition50%1/2
$opts{"configfile"} ||= "amazon.xml"
l!l
02
MC/DC0%0/1
$opts{"configfile"} ||= "amazon.xml"
$opts{"configfile"}
Truth table50%1/2
A
A= $opts{"configfile"}
Aresult
11
00
252  
253 2 ▶ if (-r $opts{configfile} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $opts{"configfile"}) 0 2 2
254  
255 0 my $xmlconfig = XML::Simple::XMLin("$opts{configfile}");
256  
257 0 ▶ $opts{access_key_id} ||= $xmlconfig->{access_key_id};
Condition0%0/3
$opts{"access_key_id"} ||= $xmlconfig->{"access_key_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"access_key_id"} ||= $xmlconfig->{"access_key_id"}
$opts{"access_key_id"} $xmlconfig->{"access_key_id"}
Truth table0%0/3
A ||= B
A= $opts{"access_key_id"}B= $xmlconfig->{"access_key_id"}
ABresult
1X1
011
000
258 0 ▶ $opts{secret_key} ||= $xmlconfig->{secret_key};
Condition0%0/3
$opts{"secret_key"} ||= $xmlconfig->{"secret_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"secret_key"} ||= $xmlconfig->{"secret_key"}
$opts{"secret_key"} $xmlconfig->{"secret_key"}
Truth table0%0/3
A ||= B
A= $opts{"secret_key"}B= $xmlconfig->{"secret_key"}
ABresult
1X1
011
000
259 0 ▶ $opts{merchant_id} ||= $xmlconfig->{merchant_id};
Condition0%0/3
$opts{"merchant_id"} ||= $xmlconfig->{"merchant_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"merchant_id"} ||= $xmlconfig->{"merchant_id"}
$opts{"merchant_id"} $xmlconfig->{"merchant_id"}
Truth table0%0/3
A ||= B
A= $opts{"merchant_id"}B= $xmlconfig->{"merchant_id"}
ABresult
1X1
011
000
260 0 ▶ $opts{marketplace_id} ||= $xmlconfig->{marketplace_id};
Condition0%0/3
$opts{"marketplace_id"} ||= $xmlconfig->{"marketplace_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"marketplace_id"} ||= $xmlconfig->{"marketplace_id"}
$opts{"marketplace_id"} $xmlconfig->{"marketplace_id"}
Truth table0%0/3
A ||= B
A= $opts{"marketplace_id"}B= $xmlconfig->{"marketplace_id"}
ABresult
1X1
011
000
261 0 ▶ $opts{endpoint} ||= $xmlconfig->{endpoint};
Condition0%0/3
$opts{"endpoint"} ||= $xmlconfig->{"endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"endpoint"} ||= $xmlconfig->{"endpoint"}
$opts{"endpoint"} $xmlconfig->{"endpoint"}
Truth table0%0/3
A ||= B
A= $opts{"endpoint"}B= $xmlconfig->{"endpoint"}
ABresult
1X1
011
000
262 0 ▶ $opts{debug} ||= $xmlconfig->{debug};
Condition0%0/3
$opts{"debug"} ||= $xmlconfig->{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"debug"} ||= $xmlconfig->{"debug"}
$opts{"debug"} $xmlconfig->{"debug"}
Truth table0%0/3
A ||= B
A= $opts{"debug"}B= $xmlconfig->{"debug"}
ABresult
1X1
011
000
263 0 ▶ $opts{logfile} ||= $xmlconfig->{logfile};
Condition0%0/3
$opts{"logfile"} ||= $xmlconfig->{"logfile"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"logfile"} ||= $xmlconfig->{"logfile"}
$opts{"logfile"} $xmlconfig->{"logfile"}
Truth table0%0/3
A ||= B
A= $opts{"logfile"}B= $xmlconfig->{"logfile"}
ABresult
1X1
011
000
264 }
265  
266 2 my $attr = $opts->{agent_attributes};
267 2 $attr->{Language} = 'Perl';
268  
269 2 my $attr_str = join ';', map { "$_=$attr->{$_}" } keys %$attr;
270 2 ▶ my $appname = $opts{Application} || 'Amazon::MWS::Client';
Condition50%1/2
$opts{'Application'} || 'Amazon::MWS::Client'
l!l
02
MC/DC0%0/1
$opts{'Application'} || 'Amazon::MWS::Client'
$opts{'Application'}
Truth table50%1/2
A
A= $opts{'Application'}
Aresult
11
00
271 2 ▶ my $version = $opts{Version} || 0.5;
Condition50%1/2
$opts{'Version'} || 0.5
l!l
02
MC/DC0%0/1
$opts{'Version'} || 0.5
$opts{'Version'}
Truth table50%1/2
A
A= $opts{'Version'}
Aresult
11
00
272  
273 2 my $agent_string = "$appname/$version ($attr_str)";
274  
275 2 ▶ die 'No access key id' unless $opts{access_key_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"access_key_id"} 0 2 2
276 2 ▶ die 'No secret key' unless $opts{secret_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"secret_key"} 0 2 2
277 2 ▶ die 'No merchant id' unless $opts{merchant_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"merchant_id"} 0 2 2
278 2 ▶ die 'No marketplace id' unless $opts{marketplace_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"marketplace_id"} 0 2 2
279  
280 2 ▶ if ($opts{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 0 2 2
281 0 ▶ open LOG, ">$opts{logfile}" or die "Cannot open logfile.";
Branch: 1 branch
BranchTrueFalseTotal
open LOG, qq[>$opts{"logfile"}] or die "Cannot open logfile." 0 0 0
282 0 print LOG DateTime->now();
283 0 print LOG "\nNew instance created. \n";
284 0 print LOG Dumper(\%opts);
285 0 close LOG;
286 }
287  
288 # https://github.com/interchange/Amazon-MWS/issues/9
289 2 ▶ $opts{endpoint} ||= 'https://mws.amazonaws.com';
Condition50%1/2
$opts{"endpoint"} ||= "https://mws.amazonaws.com"
l!l
02
MC/DC0%0/1
$opts{"endpoint"} ||= "https://mws.amazonaws.com"
$opts{"endpoint"}
Truth table50%1/2
A
A= $opts{"endpoint"}
Aresult
11
00
290 # strip the trailing slashes
291 2 $opts{endpoint} =~ s/\/+\z//;
292  
293 bless {
294 package => "$pkg",
295 agent => LWP::UserAgent->new(agent => $agent_string),
296 endpoint => $opts{endpoint},
297 access_key_id => $opts{access_key_id},
298 secret_key => $opts{secret_key},
299 merchant_id => $opts{merchant_id},
300 marketplace_id => $opts{marketplace_id},
301 debug => $opts{debug},
302 logfile => $opts{logfile},
303 2 }, $pkg;
304  
305 }
306  
307 8 1;
308  
309 # Local Variables:
310 # tab-width: 8
311 # End:
312  
« MWS/XML/Product.pm ↑ Summary MWS/XML/Response/FeedSubmissionResult.pm »