SQL/Amazon/Request/Request.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 235 CC 25 SCAR 51.7
CC
25
Cov
0%
send_request
380.0
equals
20.0
check_cache
12.0
CRAP 175.6
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::Request;
25  
26 use LWP;
27 use IO::File;
28 use XML::Simple;
29 use DBI;
30  
31 use strict;
32 our $last_time;
33  
34 our %url_roots = (
35 'us', 'http://webservices.amazon.com/onca/xml?'
36 );
37 our %reqcache = ();
38  
39 use constant AMZN_CACHE_TIME_LIMIT => 1800;
40  
41 sub new {
42 my $class = shift;
43 my $obj = {};
44 $obj->{_lwp} = LWP::UserAgent->new
45 or return undef;
46 $obj->{_lwp}->timeout(60);
47 $obj->{_lwp}->agent( 'Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC)' );
48 $obj->{_lwp}->env_proxy;
49 bless $obj, $class;
50 return $obj;
51 }
52 sub populate_request {
53 my ($obj, $subid, $locale, $stmt) = @_;
54 $obj->{url_params}{SubscriptionId} = $subid;
55 $obj->{_locale} = $locale ||= 'us';
56 return $url_roots{$obj->{_locale}} ? $obj : undef;
57 }
58  
59 sub send_request {
60 my ($obj, $store, $reqids) = @_;
61 $obj->{_warnmsg} = undef;
62 $obj->{_errmsg} = undef;
63 my $url_params = $obj->{url_params};
64 my %reqhash = %$url_params;
65 foreach (keys %$url_params) {
66 delete $url_params->{$_}
67 unless (defined($url_params->{$_}) &&
68 ($url_params->{$_} ne ''));
69 }
70  
71 while (1) {
72 my $currpage = $url_params->{ItemPage};
73 last if ($currpage > $obj->{_max_pages});
74 my ($cache_req, $lastpage) = $obj->check_cache;
75
76 if ($cache_req) {
77 DBI->trace_msg("[SQL::Amazon::Request::Request::send_request] Request satisfied from cache.\n", 3)
78 if $ENV{DBD_AMZN_DEBUG};
79 $reqids->{$cache_req} = 1;
80 $obj->advance_request_page;
81 $lastpage ? last : next;
82 }
83 my $dbgname = $ENV{DBD_AMZN_SRC};
84  
85 if ($dbgname) {
86 $dbgname .= $url_params->{ResponseGroup} .
87 '/reqno' . $obj->{_reqno} . 'page' . $currpage . '.xml'
88 if (substr($dbgname, -1, 1) eq '/');
89 }
90 sleep 1
91 while (defined($last_time) && (time() - $last_time == 0));
92 my $xml;
93 if ($dbgname && -e $dbgname) {
94 DBI->trace_msg("[SQL::Amazon::Request::Request::send_request] Loading XML from $dbgname.\n", 3)
95 if $ENV{DBD_AMZN_DEBUG};
96  
97 eval { $xml = XMLin($dbgname); };
98 if ($@) {
99 print STDERR "Can't read local version of $dbgname: $@\n";
100 }
101 }
102  
103 unless (defined($xml)) {
104 if ($ENV{DBD_AMZN_DEBUG}) {
105 my $tracemsg = '';
106 $tracemsg .= "$_=$url_params->{$_}&"
107 foreach (keys %$url_params);
108 chop $tracemsg;
109 DBI->trace_msg("[SQL::Amazon::Request::Request::send_request] Posting ECS request:\n$tracemsg\n", 3);
110 }
111  
112 my $resp = $obj->{_lwp}->post($url_roots{$obj->{_locale}}, $url_params);
113
114 if ($dbgname && (! -e $dbgname)) {
115 open(XMLF, ">$dbgname") || die $!;
116 print XMLF $resp->decoded_content;
117 close XMLF;
118 }
119
120 $obj->{_errstr} = 'Amazon ECS request failed: Unknown reason.',
121 return undef
122 unless $resp;
123  
124 $obj->{_errstr} = 'Amazon ECS request failed: ' . $resp->status_line,
125 return undef
126 unless $resp->is_success;
127 $xml = XMLin($resp->decoded_content);
128 }
129
130 $obj->{_errstr} = 'Unable to parse Amazon ECS response.',
131 return undef
132 unless $xml;
133 $last_time = time();
134 return undef
135 if $obj->has_errors($xml);
136 last
137 unless $obj->process_results($xml, $store, $reqids);
138 }
139 $obj->{url_params} = \%reqhash;
140 return $obj;
141 }
142 sub check_cache {
143 my $obj = shift;
144
145 my $url_params = $obj->{url_params};
146
147 my @req = ();
148 push @req, $_ . '=' . $url_params->{$_}
149 foreach (sort keys %$url_params);
150 my $req = join('&', @req);
151  
152 return (undef, undef)
153 unless $reqcache{$req};
154
155 delete $reqcache{$req},
156 return (undef, undef)
157 if ($reqcache{$req}[2] < time());
158 $reqcache{$req}[2] = time() + AMZN_CACHE_TIME_LIMIT;
159 }
160 sub add_to_cache {
161 my ($obj, $reqid, $lastpage) = @_;
162
163 my $url_params = $obj->{url_params};
164
165 my @req = ();
166 push @req, $_ . '=' . $url_params->{$_}
167 foreach (sort keys %$url_params);
168  
169 my $req = join('&', @req);
170
171 $reqcache{$req} = [ $reqid, $lastpage, time() + AMZN_CACHE_TIME_LIMIT ];
172 return $obj;
173 }
174  
175 sub errstr { return shift->{_errstr}; }
176  
177 sub warnstr { return shift->{_warnstr}; }
178 sub equals {
179 my ($obj, $request) = @_;
180
181 return undef
182 unless (ref $obj eq ref $request);
183 my $myparms = $obj->{url_params};
184 my $otherparms = $request->{url_params};
185 foreach (%$myparms) {
186 return undef
187 unless ($otherparms->{$_} &&
188 ($myparms->{$_} eq $otherparms->{$_}));
189 }
190 foreach (%$otherparms) {
191 return undef
192 unless ($myparms->{$_} &&
193 ($myparms->{$_} eq $otherparms->{$_}));
194 }
195 return 1;
196 }
197  
198 sub has_errors {
199 my ($obj, $xml) = @_;
200 return undef;
201 }
202 sub more_results {
203 my ($obj, $xml) = @_;
204 return undef;
205 }
206 sub process_results {
207 my ($obj, $xml, $engine, $reqids) = @_;
208 return 1;
209 }
210 sub advance_request_page {
211 my $obj = shift;
212 return $obj;
213 }
214  
215 1;