SQL/Amazon/Request/ItemLookup.pm untested

filter: Statementstmt 0.0% 0 / 199 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 55 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 428 CC 60 SCAR 59.8
CC
60
Cov
0%
create_lookup_request
812.0
process_results
306.0
populate_request
72.0
CRAP 394.0
1 #
2 # Copyright (c) 2005, Presicient Corp., USA
3 #
4 # Permission is granted to use this software according to the terms of the
5 # Artistic License, as specified in the Perl README file,
6 # with the exception that commercial redistribution, either
7 # electronic or via physical media, as either a standalone package,
8 # or incorporated into a third party product, requires prior
9 # written approval of the author.
10 #
11 # This software is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 # Presicient Corp. reserves the right to provide support for this software
16 # to individual sites under a separate (possibly fee-based)
17 # agreement.
18 #
19 # History:
20 #
21 # 2005-Jan-27 D. Arnold
22 # Coded.
23 #
24 package SQL::Amazon::Request::ItemLookup;
25  
26 use SQL::Amazon::Request::Request;
27 use SQL::Amazon::Parser qw(:pred_node_codes);
28 use Clone qw(clone);
29 use base qw(SQL::Amazon::Request::Request);
30  
31 use Data::Dumper;
32  
33 use strict;
34  
35 our $errstr;
36  
37 sub errstr {
38 return $errstr
39 if $errstr && $errstr ne '';
40  
41 return shift->{_errstr};
42 }
43  
44 sub new {
45 my $class = shift;
46 my $req_attrs = {};
47 my $predicate;
48 if ($class eq 'SQL::Amazon::Request::ItemLookup') {
49 $errstr = undef;
50 $predicate = create_lookup_request($req_attrs, @_);
51  
52 return (undef, undef)
53 if $errstr;
54 return ($predicate, undef)
55 unless scalar keys %$req_attrs;
56 }
57 else {
58 $req_attrs = shift;
59 }
60  
61 my $obj = $class->SUPER::new();
62 my %url_params = (
63 'Service', 'AWSECommerceService',
64 'Operation', 'ItemLookup',
65 'SearchIndex', delete $req_attrs->{SearchIndex},
66 'ItemPage', 1,
67 'ResponseGroup', 'Large');
68  
69 $obj->{url_params} = \%url_params;
70 $obj->{req_attrs} = $req_attrs;
71 return wantarray ? ($predicate, $obj) : $obj;
72 }
73 sub create_lookup_request {
74 my ($request, $expr, $table, $parser) = @_;
75 $request->{SearchIndex} = $table;
76 my $conjoins = $expr->[0];
77 my @finalcjs = ();
78 foreach (@$conjoins) {
79 my ($op, $left, $right, $neg) = @$_;
80 my ($name, $value);
81 if ($op eq 'USER_DEFINED') {
82  
83 $name = (ref $left ne 'HASH') ?
84 $left->name() : $left->{name};
85  
86 push (@finalcjs, $_),
87 next
88 unless (uc $name eq 'AMZN_IN_ANY');
89  
90 if ($name eq 'AMZN_IN_ANY') {
91 $op = 'IN';
92 $value = (ref $left ne 'HASH') ?
93 $left->args()->[0]{value} :
94 $left->{value}{value}[0];
95 }
96 }
97  
98 push (@finalcjs, $_),
99 next
100 if ($neg && ($op eq 'IN'));
101 push (@finalcjs, $_),
102 next
103 if ((ref $right eq 'HASH') && ($right->{type} eq 'column'));
104  
105 $name = ((ref $left eq 'HASH') && ($left->{type} eq 'column')) ?
106 $left->{value} :
107 ($op eq 'IN') ? $value : $name;
108 $name = uc $name;
109 $table = uc $1
110 if ($name=~s/^([A-Z]\w*)\.(\w+)/$2/);
111 my $aliases = $parser->{struct}{column_aliases};
112 $name = $aliases->{$name}
113 if $aliases->{$name};
114
115 push (@finalcjs, $_),
116 next
117 unless ($name=~/^[A-Z]\w*$/);
118
119 if ($name=~/^ASIN|SKU|UPC|EAN$/) {
120 push (@finalcjs, $_),
121 next
122 if (($op eq '<>') || (($op eq 'IN') && $neg));
123  
124 $errstr = "Invalid range predicate for $name.",
125 return undef
126 if ($op =~/^[<>]/);
127  
128 $request->{Key}{Name} = $name;
129 $request->{Key}{Value} = ($op eq '=') ?
130 $right :
131 clone($_);
132 }
133 elsif ($name eq 'CONDITION') {
134 $errstr = "Unknown column CONDITION for table $table.",
135 return undef
136 unless (uc $table eq 'OFFERS');
137  
138 $errstr = 'Invalid range predicate for Condition.',
139 return undef
140 if (($op =~/^[<>]/) || ($op eq 'LIKE') || ($op eq 'CLIKE'));
141  
142 $request->{Condition}{Value} = $right;
143 $request->{Condition}{Complement} =
144 (($op eq '<>') || (($op eq 'IN') && $neg)) ? 1 : 0;
145 }
146 elsif ($name eq 'DELIVERYMETHOD') {
147 $errstr = 'Invalid range predicate for DeliveryMethod.',
148 return undef
149 if (($op =~/^[<>]/) || ($op eq 'LIKE') || ($op eq 'CLIKE'));
150 $request->{DeliveryMethod}{Value} = $right;
151 $request->{DeliveryMethod}{Complement} =
152 (($op eq '<>') || (($op eq 'IN') && $neg)) ? 1 : 0;
153 }
154 elsif ($name eq 'ISPUPOSTALCODE') {
155 $errstr = 'Invalid predicate: ISPUPOSTALCODE must be exact match.',
156 return undef
157 unless ($op eq '=');
158 $request->{ISPUPostalCode}{Value} = $right;
159 }
160 elsif ($name eq 'MERCHANTID') {
161 $errstr = 'Invalid predicate: MERCHANTID must be exact match.',
162 return undef
163 unless ($op eq '=');
164 $request->{MerchantId} = $right;
165 }
166 else {
167 push (@finalcjs, $_);
168 }
169 }
170 $expr->[0] = \@finalcjs;
171 return $expr;
172 }
173 sub populate_request {
174 my ($obj, $subid, $locale, $stmt, $max_pages, $resp_group) = @_;
175  
176 $obj->{url_params}->{ResponseGroup} = $resp_group;
177 $obj->{_max_pages} = $max_pages;
178
179 return undef
180 unless $obj->SUPER::populate_request($subid, $locale, $stmt);
181 return $obj
182 unless (ref $obj eq 'SQL::Amazon::Request::ItemLookup');
183
184 my $req_attrs = $obj->{req_attrs};
185 my $url_params = $obj->{url_params};
186  
187 my $keys = $req_attrs->{Key}{Value};
188 unless (ref $keys eq 'ARRAY') {
189 $url_params->{ItemId} =
190 $stmt->get_row_value($keys, undef, {});
191 }
192 else {
193  
194 warn "Not IN ANY!\n"
195 unless (($keys->[0] eq 'USER_DEFINED') &&
196 $keys->[1]->isa('SQL::Statement::Util::Function'));
197  
198 $url_params->{ItemId} = $stmt->get_row_value($keys->[1], undef, {});
199 }
200  
201 $url_params->{IdType} = $req_attrs->{Key}{Name};
202 delete $url_params->{SearchIndex}
203 if ($req_attrs->{Key}{Name} eq 'ASIN');
204  
205 foreach ('Condition', 'DeliveryMethod', 'ISPUPostalCode') {
206 $url_params->{$_} = $stmt->get_row_value($req_attrs->{$_}{Value}, undef, {})
207 if defined($req_attrs->{$_});
208 }
209  
210 $url_params->{MerchantId} = defined($req_attrs->{MerchantId}) ?
211 $stmt->get_row_value($req_attrs->{MerchantId}, undef, {}) : 'Amazon';
212  
213 return $obj;
214 }
215 sub has_errors {
216 my ($obj, $xml) = @_;
217
218 my $reqattrs = $obj->{url_params};
219 $obj->{_errstr} = "Amazon ECS request failed: $xml",
220 return 1
221 unless ref $xml;
222 $obj->{_errstr} = 'Amazon ECS request failed: ' .
223 $xml->{Items}{Request}{Errors}{Error}{Message},
224 return 1
225 if ($xml->{Items}{Request}{Errors} &&
226 ($xml->{Items}{Request}{Errors}{Error}{Code} ne
227 'AWS.ECommerceService.NoExactMatches'));
228  
229 return undef;
230 }
231 sub advance_request_page {
232 my $obj = shift;
233  
234 my $reqattrs = $obj->{url_params}{ItemPage}++;
235 return $obj;
236 }
237  
238 sub more_results {
239 my ($obj, $xml) = @_;
240  
241 my $reqattrs = $obj->{url_params};
242 my $pages = $xml->{Items}{TotalPages} || 1;
243  
244 $obj->{_warnmsg} =
245 "Maximum result pages exceeded; returning $reqattrs->{ItemPage} pages of $pages available.",
246 return undef
247 if ($reqattrs->{ItemPage} >= $obj->{_max_pages});
248  
249 return ($reqattrs->{ItemPage} < $pages) ?
250 ++$reqattrs->{ItemPage} : undef;
251 }
252 sub process_results {
253 my ($obj, $xml, $store, $reqids) = @_;
254 if (defined($xml->{Items}{TotalResults})) {
255 return undef
256 unless $xml->{Items}{TotalResults};
257 $obj->{_total_results} = $xml->{Items}{TotalResults};
258 $obj->{_total_pages} = $xml->{Items}{TotalPages};
259 }
260 else {
261 $obj->{_total_results} = 1;
262 $obj->{_total_pages} = 1;
263 }
264 my $offerstbl = $store->get_table('Offers');
265 my $offercols = $offerstbl->col_nums;
266  
267 my $listmaniatbl = $store->get_table('ListManiaLists');
268 my $editrvwtbl = $store->get_table('EditorialReviews');
269 my $custrvwtbl = $store->get_table('CustomerReviews');
270 my $similartbl = $store->get_table('SimilarProducts');
271 my $browsetbl = $store->get_table('BrowseNodes');
272 my ($reqtype, $resptype) = $obj->isa('SQL::Amazon::Request::ItemSearch') ?
273 ('ItemSearchRequest', 'ItemSearchResponse') :
274 ('ItemLookupRequest', 'ItemLookupResponse');
275 my $search_index = $xml->{Items}{Request}{$reqtype}{SearchIndex} ||
276 'Books';
277  
278 my $reqid = $xml->{OperationRequest}{RequestId};
279 my $table = $store->get_table($search_index);
280 $obj->add_to_cache($reqid,
281 ($obj->{_total_pages} == $obj->{url_params}{ItemPage}));
282 my $items = (ref $xml->{Items}{Item} eq 'ARRAY') ?
283 $xml->{Items}{Item} :
284 [ $xml->{Items}{Item} ];
285  
286 foreach my $item (@$items) {
287 my $asin = $item->{ASIN};
288 $table->insert($item, $asin, $reqid);
289 my $attrs;
290 if ($item->{Offers} && $item->{Offers}{Offer}) {
291 $attrs = $item->{Offers}{Offer};
292 if (ref $attrs eq 'ARRAY') {
293 $offerstbl->insert($_, $asin, $reqid)
294 foreach (@$attrs);
295 }
296 else {
297 $offerstbl->insert($attrs, $asin, $reqid);
298 }
299 }
300 $attrs = $item->{CustomerReviews};
301 if ($attrs) {
302 if (ref $attrs->{Review} eq 'ARRAY') {
303 $attrs = $attrs->{Review};
304 $_->{ASIN} = $asin,
305 $_->{ReviewDate} = $_->{Date},
306 delete $_->{Date},
307 $custrvwtbl->insert($_, $reqid)
308 foreach (@$attrs);
309 }
310 else {
311 $attrs->{Review}{ReviewDate} = $attrs->{Review}{Date};
312 delete $attrs->{Review}{Date};
313 $attrs->{Review}{ASIN} = $asin;
314 $custrvwtbl->insert($attrs->{Review}, $reqid);
315 }
316 }
317 $attrs = $item->{EditorialReviews};
318 if ($attrs) {
319 if (ref $attrs->{EditorialReview} eq 'ARRAY') {
320 $attrs = $attrs->{EditorialReview};
321 $_->{ASIN} = $asin,
322 $editrvwtbl->insert($_, $reqid)
323 foreach (@$attrs);
324 }
325 else {
326 $attrs->{EditorialReview}{ASIN} = $asin;
327 $editrvwtbl->insert($attrs->{EditorialReview}, $reqid);
328 }
329 }
330 $attrs = $item->{ListmaniaLists};
331 if ($attrs) {
332 if (ref $attrs->{ListManiaList} eq 'ARRAY') {
333 $attrs = $attrs->{ListManiaList};
334 $_->{ASIN} = $asin,
335 $listmaniatbl->insert($_, $reqid)
336 foreach (@$attrs);
337 }
338 else {
339 $attrs->{ListManiaLists}{ASIN} = $asin;
340 $listmaniatbl->insert($attrs->{ListManiaList}, $reqid);
341 }
342 }
343 $attrs = $item->{SimilarProducts};
344 if ($attrs) {
345 if (ref $attrs->{SimilarProduct} eq 'ARRAY') {
346 $attrs = $attrs->{SimilarProduct};
347 $_->{SimilarASIN} = $_->{ASIN},
348 $_->{ASIN} = $asin,
349 $similartbl->insert($_, $reqid)
350 foreach (@$attrs);
351 }
352 else {
353 $attrs->{SimilarProduct}{SimilarASIN} =
354 $attrs->{SimilarProduct}{ASIN};
355 $attrs->{SimilarProduct}{ASIN} = $asin;
356 $similartbl->insert($attrs->{SimilarProduct}, $reqid);
357 }
358 }
359 $attrs = $item->{BrowseNodes};
360 if ($attrs) {
361 if (ref $attrs->{BrowseNode} eq 'ARRAY') {
362 $attrs = $attrs->{BrowseNode}[0];
363 while ($attrs) {
364 $attrs = $attrs->{Ancestors}{BrowseNode};
365 $browsetbl->insert({
366 ASIN => $asin,
367 Name => $attrs->{Name},
368 BrowseNodeId => $attrs->{BrowseNodeId}
369 }, $reqid);
370 $attrs = $attrs->{Ancestors};
371 }
372 }
373 else {
374 $attrs = $attrs->{BrowseNode};
375 while ($attrs) {
376 $browsetbl->insert({
377 ASIN => $asin,
378 Name => $attrs->{Name},
379 BrowseNodeId => $attrs->{BrowseNodeId}
380 }, $reqid);
381 $attrs = $attrs->{Ancestors}{BrowseNode};
382 }
383 }
384 }
385 }
386 $reqids->{$reqid} = 1;
387 return $obj->more_results($xml);
388 }
389  
390 1;