eBay.pm untested

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 238 CC 14 SCAR 44.5
CC
14
Cov
0%
APICall
182.0
FormateBayDate
6.0
GeteBayTime
2.0
CRAP 85.9
1 use strict;
2 use warnings;
3 package WebService::eBay;
4  
5 # ABSTRACT: Interface with the eBay API
6  
7 use LWP::UserAgent;
8 use HTTP::Request;
9 use HTTP::Headers;
10 use XML::Simple;
11 use Data::UUID;
12 use DateTime;
13 use Encode;
14  
15 =head1 NAME
16  
17 WebService::eBay
18  
19 =head1 SYNOPSIS
20  
21 This package provides an interface to use the eBay Trading API. For more information about the eBay Trading API, visit https://developer.ebay.com/devzone/xml/docs/reference/ebay/index.html
22  
23 The "hello world" equivalent in the eBay API world is to send a call asking for the current eBay time. Here's an example of how to do that with this module:
24  
25 use WebService::eBay;
26
27 my $response = WebService::eBay::APICall("GeteBayOfficialTime");
28 print "$response->{Timestamp}\n";
29  
30 But this isn't a great example for this module because the GeteBayOfficialTime call doesn't take any parameters. A simple one that does though is the EndItem call. Here's how you would do that in this module:
31  
32 my $listingNumber = '142306955';
33 my $callDetails = "<ItemID>$listingNumber</ItemID>
34 <EndingReason>OtherListingError</EndingReason>";
35
36 if ( WebService::eBay::APICall( "EndItem", $callDetails ) ) {
37 print "\nDeleted $listingNumber from eBay\n";
38 } else {
39 print("\nUnable to end listing $listingNumber\n");
40 }
41  
42 =head1 DESCRIPTION
43
44 To use the eBay Trading API you need application keys and an authorization token. You can obtain these through your eBay developer account. You should create a file called "tokens" in the directory containing the program using this module. That file should look like this for the API sandbox and testing:
45  
46 APIURL https://api.sandbox.ebay.com/ws/api.dll
47 SiteID 0
48 DevName <Your Dev Name>
49 AppName <Your App Name>
50 CertName <Your Cert Name>
51 AuthToken <Your Auth Token>
52 Log true
53  
54 and this for the Production keys.
55  
56 APIURL https://api.ebay.com/ws/api.dll
57 SiteID 100
58 DevName <Your Dev Name>
59 AppName <Your App Name>
60 CertName <Your Cert Name>
61 AuthToken <Your Auth Token>
62
63 The last option "Log" is intended for testing and sandbox use as it logs all communication sent and received from the API, which includes your secret keys. To not use this option, leave the "Log" line out of the tokens file or set it to something other than 'true'.
64  
65  
66 =cut
67  
68 my %config;
69 my $tokens;
70 my $name;
71 my $value;
72 open( $tokens, './tokens' ) or die "Unable to find tokens file $!";
73 while (<$tokens>) {
74 chomp;
75 ( $name, $value ) = split / /;
76 $config{$name} = $value;
77 }
78 close($tokens);
79  
80 my $APIURL = $config{'APIURL'} or die "No Valid APIURL in tokens file";
81 my $DevName = $config{'DevName'} or die "No Valid DevName in tokens file";
82 my $AppName = $config{'AppName'} or die "No Valid AppName in tokens file";
83 my $CertName = $config{'CertName'} or die "No Valid CertName in tokens file";
84 my $AuthToken = $config{'AuthToken'} or die "No Valid AuthToken in tokens file";
85 my $SiteID = $config{'SiteID'} or die "No Valid SiteID in tokens file";
86  
87 my $loggingOn = undef;
88 if ( $config{'Log'} eq 'true' ) {
89 $loggingOn = 'true';
90 }
91  
92 =head1 SUBROUTINES
93
94 =head2 APICall
95
96 Sends an API call and returns the results as a hash. Optionally returns the raw XML. If the call encounters an HTTP error sending the call, it will try again twice before giving up. This is in accordance with the rules for the eBay API.
97  
98 =head3 Parameters
99  
100 =head4 CallName
101  
102 The name of the API Call that you wish to make. For a list of API calls and their parameters, visit https://developer.ebay.com/devzone/xml/docs/reference/ebay/index.html
103  
104 =head4 RequestDetails
105  
106 The details of the call, in XML. You do not need to provide the RequesterCredentials or the WarningLevel, but you do have to provide the rest of the call. This is where that goes. The RequesterCredentials are always the same and the WarningLevel doesn't usually need to be changed. For information on what details your call needs, visit https://developer.ebay.com/devzone/xml/docs/reference/ebay/index.html
107  
108 Some calls have no details, such as GeteBayOfficialTime.
109
110 =head4 Xml
111  
112 Pass any value here for true (pass "undef" for false) and the sub will return the raw XML from the API response, instead of the hash.
113  
114 =head4 FailOnWarning
115  
116 Pass any value here for true (pass "undef" for false) and the sub will return false ("fail") if the API call returns a warning. The default is to return the hash (which contains the warning) if the API call returns a warning.
117  
118 =cut
119  
120 sub APICall {
121 my $CallName = shift;
122 my $RequestDetails;
123 $RequestDetails = shift
124 or $RequestDetails = ' ';
125 my $Xml = shift
126 ; # Set to true to get function to return the raw XML instead of a hash, generally for debugging. Default is off.
127 my $FailOnWarning = shift
128 ; # Set to true if you want the function to return false if the API call returns a warning. The default is to return true if the API call returns a warning.
129 my $objUserAgent = LWP::UserAgent->new( timeout => 15 );
130 my $Header = HTTP::Headers->new;
131 my ( $ErrorResponse, @Errors, $Error, $Response );
132  
133 $CallName =~ s/\s+/ /g; #Remove consecutive spaces
134 $CallName =~ s/ $//gi; #Remove the last space
135 $CallName =~ s/^ //gi; #Remove the first space
136  
137 my $Request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
138 <$CallName" . "Request xmlns=\"urn:ebay:apis:eBLBaseComponents\">
139 <RequesterCredentials>
140 <eBayAuthToken>$AuthToken</eBayAuthToken>
141 </RequesterCredentials>
142 <WarningLevel>High</WarningLevel>
143 $RequestDetails
144 </$CallName" . "Request>";
145  
146 my $apiLog;
147 if ($loggingOn) {
148 open( $apiLog, ">>apiLog" ) or die $!;
149 binmode( $apiLog, ":utf8" );
150 print $apiLog FormateBayDate() . "\n\n$Request\n\n";
151 }
152  
153 $Header->push_header( 'X-EBAY-API-COMPATIBILITY-LEVEL' => '897' );
154 $Header->push_header(
155 'X-EBAY-API-SESSION-CERTIFICATE' => "$DevName$AppName$CertName" );
156 $Header->push_header( 'X-EBAY-API-DEV-NAME' => $DevName );
157 $Header->push_header( 'X-EBAY-API-APP-NAME' => $AppName );
158 $Header->push_header( 'X-EBAY-API-CERT-NAME' => $CertName );
159 $Header->push_header( 'X-EBAY-API-SITEID' => "$SiteID" );
160 $Header->push_header( 'Content-Type' => 'text/xml; charset=utf-8' );
161 $Header->push_header( 'X-EBAY-API-CALL_NAME' => "$CallName" );
162  
163 my $objRequest = HTTP::Request->new( "POST", $APIURL, $Header );
164 $objRequest->content_type("text/plain; charset='utf8'");
165 $objRequest->content( Encode::encode_utf8($Request) );
166 my $objResponse = $objUserAgent->request($objRequest) or return undef;
167  
168 if ( $objResponse->is_error ) {
169 warn "HTTP request error: "
170 . $objResponse->error_as_HTML
171 . " on API Call $CallName. Trying again...";
172 sleep 2;
173 $objResponse = $objUserAgent->request($objRequest);
174 if ( $objResponse->is_error ) {
175 warn ("HTTP request error again... Trying one more time...");
176 sleep 2;
177 $objResponse = $objUserAgent->request($objRequest);
178 if ( $objResponse->is_error ) {
179 die
180 "Unable to resolve the HTTP request error. API call FAILED.";
181 return undef;
182 }
183 }
184 }
185  
186 if ($Xml) { return $objResponse->{_content} }
187 if ($loggingOn) {
188 print $apiLog $objResponse->{_content}
189 . "\n**************************************************************************\n\n";
190 close $apiLog;
191 }
192  
193 my $xml = new XML::Simple;
194 $Response = $xml->XMLin( $objResponse->content );
195  
196 if ( $Response->{Ack} ) {
197 if ( $Response->{Ack} =~ /success/i ) {
198 return $Response;
199 } else { # We have an error!!
200 $ErrorResponse =
201 $xml->XMLin( $objResponse->content, ForceArray => 1 );
202 @Errors = @{ $ErrorResponse->{Errors} };
203 my $ErrorLine = "*** API Call $CallName returned " . scalar @Errors;
204 if ( $Response->{Ack} =~ /warning/i ) {
205 $ErrorLine .= " warning(s): ";
206 foreach $Error (@Errors) {
207 $ErrorLine .= "Warning: " . $Error->{LongMessage}->[0];
208 }
209 print($ErrorLine);
210 return $Response unless ($FailOnWarning);
211 } elsif ( $Response->{Ack} =~ /PartialFailure/i ) {
212 $ErrorLine .= " Partial Failures but did not fail. ";
213 foreach $Error (@Errors) {
214 $ErrorLine .= "Error: " . $Error->{LongMessage}->[0];
215 }
216 print $ErrorLine;
217 return $Response;
218 } elsif ( $Response->{Ack} =~ /failure/i ) {
219 $ErrorLine .= " error(s) and FAILED: *** ";
220 foreach $Error (@Errors) {
221 $ErrorLine .= " || Error: " . $Error->{LongMessage}->[0];
222 }
223 print($ErrorLine);
224 } else {
225 $ErrorLine .=
226 " errors with an undefined error response and FAILED. Error XML follows. \n\n$objResponse->{_content}\n\n";
227 print($ErrorLine);
228 }
229 }
230 } else {
231 print
232 "API Call \"$CallName\" did not return a result status and has FAILED.";
233 }
234  
235 # Arrival at this line consitutes an error!
236 # print "\n$Request\n\n$objResponse->{_content}\n\n";
237 return undef;
238 }
239  
240 =head2 GeteBayTime
241  
242 Returns eBay official time. Not local time.
243  
244 =head3 Parameters
245  
246 =cut
247  
248 sub GeteBayTime {
249 my $Response = APICall("GeteBayOfficialTime") or return undef;
250  
251 my $Time = $Response->{Timestamp};
252 my $Today = substr( $Time, 0, 10 );
253 $Time =~ s/(t|z)/ /ig;
254 $Time =~ s/(^\s+)|(\s+$)//g;
255 return $Time;
256 }
257  
258 =head2 FormateBayDate
259  
260 Returns the current date formatted for use in eBay API calls. Optionally will return a date offset from the current time, such as one hour from now or one day ago.
261  
262 =head3 Parameters
263  
264 =head4 Offset
265  
266 Number of seconds to offset the current time. Example: FormateBayDate(3600) returns the time one hour from now. This function is usually called as FormateBayDate(86400) to obtain the next day's date.
267  
268 =cut
269  
270 sub FormateBayDate {
271 my $Offset = shift;
272 $Offset = 0 unless ($Offset);
273  
274 my (
275 $Second, $Minute, $Hour, $Day, $Month,
276 $Year, $WeekDay, $DayOfYear, $IsDST
277 ) = localtime( time + $Offset );
278 $Year = $Year + 1900;
279 $Month = $Month + 1;
280  
281 $Day = sprintf( "%02s", $Day );
282 $Month = sprintf( "%02s", $Month );
283 $Hour = sprintf( "%02s", $Hour );
284 $Minute = sprintf( "%02s", $Minute );
285 $Second = sprintf( "%02s", $Second );
286 return
287 $Year . "-"
288 . $Month . "-"
289 . $Day . " "
290 . $Hour . ":"
291 . $Minute . ":"
292 . $Second;
293 }
294  
295 =head2 XMLClean
296  
297 Strips a string of characters recognized by XML code
298  
299 =head3 Parameters
300  
301 =head4 Line
302  
303 The string of characters you want to process.
304  
305 =cut
306  
307 sub XMLClean {
308 my $Line = shift;
309 $Line =~ s/ < / &lt; /gi;
310 $Line =~ s/ > / &gt; /gi;
311 $Line =~ s/ & / &amp; /gi;
312 $Line =~ s/\'/&apos;/gi;
313 $Line =~ s/\"/&quot;/gi;
314 return $Line;
315 }
316  
317 =head2 GetUUID
318  
319 Returns a UUID formatted for eBay standards
320  
321 =head3 Parameters
322  
323 none
324  
325 =cut
326  
327 sub GetUUID { # Returns a UUID formated for eBay standards
328 my $Ug = new Data::UUID;
329 my $Uuid = $Ug->create();
330 my $Guid = $Ug->to_string($Uuid);
331  
332 $Guid =~ s/-//gi;
333 $Guid =~ s/{|}//gi;
334  
335 return $Guid;
336 }
337  
338 =head2 ebayTimeToSqlTimestamp
339  
340 Converts an ebay time to a string properly formatted for insertion as an SQL timestamp.
341  
342 =head3 Parameters
343  
344 =head4 time
345  
346 The time you want to convert
347  
348 =cut
349  
350 sub ebayTimeToSqlTimestamp {
351 my $time = shift;
352 $time =~ s/[T|Z]/ /gi; # Remove "T and Z"
353 $time =~ s/\..*$//gi; # remove the first dot and everything after it
354 return $time;
355 }
356  
357 return 1;
358