| 1 | package eBay::API::Simple::Merchandising; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'eBay::API::SimpleBase'; | ||
| 7 | |||
| 8 | use HTTP::Request; | ||
| 9 | use HTTP::Headers; | ||
| 10 | use XML::Simple; | ||
| 11 | use utf8; | ||
| 12 | |||
| 13 | our $DEBUG = 0; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | eBay::API::Simple::Merchandising - Support for eBay's Merchandising web service | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This class provides support for eBay's Merchandising web services. | ||
| 22 | |||
| 23 | See http://developer.ebay.com/products/merchandising/ | ||
| 24 | |||
| 25 | =head1 USAGE | ||
| 26 | |||
| 27 | my $call = eBay::API::Simple::Merchandising->new( | ||
| 28 | { appid => '<your app id here>' } | ||
| 29 | ); | ||
| 30 | |||
| 31 | $call->execute( 'getMostWatchedItems', { maxResults => 3, categoryId => 267 } ); | ||
| 32 | |||
| 33 | if ( $call->has_error() ) { | ||
| 34 | die "Call Failed:" . $call->errors_as_string(); | ||
| 35 | } | ||
| 36 | |||
| 37 | # getters for the response DOM or Hash | ||
| 38 | my $dom = $call->response_dom(); | ||
| 39 | my $hash = $call->response_hash(); | ||
| 40 | |||
| 41 | print $call->nodeContent( 'timestamp' ); | ||
| 42 | print $call->nodeContent( 'totalEntries' ); | ||
| 43 | |||
| 44 | my @nodes = $dom->findnodes( | ||
| 45 | '//item' | ||
| 46 | ); | ||
| 47 | |||
| 48 | foreach my $n ( @nodes ) { | ||
| 49 | print $n->findvalue('title/text()') . "\n"; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head1 SANDBOX USAGE | ||
| 53 | |||
| 54 | my $call = eBay::API::Simple::Merchandising->new( { | ||
| 55 | appid => '<your app id here>' | ||
| 56 | domain => '', | ||
| 57 | } ); | ||
| 58 | |||
| 59 | $call->execute( 'getMostWatchedItems', { maxResults => 3, categoryId => 267 } ); | ||
| 60 | |||
| 61 | if ( $call->has_error() ) { | ||
| 62 | die "Call Failed:" . $call->errors_as_string(); | ||
| 63 | } | ||
| 64 | |||
| 65 | # getters for the response DOM or Hash | ||
| 66 | my $dom = $call->response_dom(); | ||
| 67 | my $hash = $call->response_hash(); | ||
| 68 | |||
| 69 | =head1 PUBLIC METHODS | ||
| 70 | |||
| 71 | =head2 new( { %options } } | ||
| 72 | |||
| 73 | Constructor for the Finding API call | ||
| 74 | |||
| 75 | my $call = eBay::API::Simple::Merchandising->new( { | ||
| 76 | appid => '<your app id here>' | ||
| 77 | ... | ||
| 78 | } ); | ||
| 79 | |||
| 80 | =head3 Options | ||
| 81 | |||
| 82 | =over 4 | ||
| 83 | |||
| 84 | =item appid (required) | ||
| 85 | |||
| 86 | This appid is required by the web service. App ids can be obtained at | ||
| 87 | http://developer.ebay.com | ||
| 88 | |||
| 89 | =item siteid | ||
| 90 | |||
| 91 | eBay site id to be supplied to the web service endpoint | ||
| 92 | |||
| 93 | defaults to EBAY-US | ||
| 94 | |||
| 95 | =item domain | ||
| 96 | |||
| 97 | domain for the web service endpoint | ||
| 98 | |||
| 99 | defaults to svcs.ebay.com | ||
| 100 | |||
| 101 | =item uri | ||
| 102 | |||
| 103 | endpoint URI | ||
| 104 | |||
| 105 | defaults to /MerchandisingService | ||
| 106 | |||
| 107 | =item version | ||
| 108 | |||
| 109 | Version to be supplied to the web service endpoint | ||
| 110 | |||
| 111 | defaults to 1.0.0 | ||
| 112 | |||
| 113 | =item https | ||
| 114 | |||
| 115 | Specifies is the API calls should be made over https. | ||
| 116 | |||
| 117 | defaults to 0 | ||
| 118 | |||
| 119 | =item enable_attributes | ||
| 120 | |||
| 121 | This flag adds support for attributes in the request. If enabled request | ||
| 122 | data notes much be defined like so, | ||
| 123 | |||
| 124 | myElement => { content => 'element content', myattr => 'attr value' } | ||
| 125 | |||
| 126 | defaults to 0 | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | =head3 ALTERNATE CONFIG VIA ebay.yaml | ||
| 131 | |||
| 132 | An ebay.yaml file can be used for configuring each | ||
| 133 | service endpoint. | ||
| 134 | |||
| 135 | YAML files can be placed at the below locations. The first | ||
| 136 | file found will be loaded. | ||
| 137 | |||
| 138 | ./ebay.yaml, ~/ebay.yaml, /etc/ebay.yaml | ||
| 139 | |||
| 140 | Sample YAML: | ||
| 141 | |||
| 142 | # Trading - External | ||
| 143 | api.ebay.com: | ||
| 144 | appid: <your appid> | ||
| 145 | certid: <your certid> | ||
| 146 | devid: <your devid> | ||
| 147 | token: <token> | ||
| 148 | |||
| 149 | # Shopping | ||
| 150 | open.api.ebay.com: | ||
| 151 | appid: <your appid> | ||
| 152 | certid: <your certid> | ||
| 153 | devid: <your devid> | ||
| 154 | version: 671 | ||
| 155 | |||
| 156 | # Finding/Merchandising | ||
| 157 | svcs.ebay.com: | ||
| 158 | appid: <your appid> | ||
| 159 | version: 1.0.0 | ||
| 160 | |||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub new { | ||
| 165 | my $class = shift; | ||
| 166 | my $self = $class->SUPER::new(@_); | ||
| 167 | |||
| 168 | $self->api_config->{domain} ||= 'svcs.ebay.com'; | ||
| 169 | $self->api_config->{uri} ||= '/MerchandisingService'; | ||
| 170 | #$self->api_config->{version} ||= '1.1.0'; | ||
| 171 | $self->api_config->{https} ||= 0; | ||
| 172 | $self->api_config->{siteid} ||= 'EBAY-US'; | ||
| 173 | $self->api_config->{response_encoding} ||= 'XML'; # JSON, NV, SOAP | ||
| 174 | $self->api_config->{request_encoding} ||= 'XML'; | ||
| 175 | |||
| 176 | $self->_load_yaml_defaults(); | ||
| 177 | |||
| 178 | if ( $DEBUG ) { | ||
| 179 | print STDERR sprintf( "API CONFIG:\n%s\n", | ||
| 180 | $self->api_config_dump() | ||
| 181 | ); | ||
| 182 | } | ||
| 183 | |||
| 184 | $self->_load_credentials(); | ||
| 185 | |||
| 186 | return $self; | ||
| 187 | } | ||
| 188 | |||
| 189 | =head2 prepare( $verb, $call_data ) | ||
| 190 | |||
| 191 | $self->prepare( 'getMostWatchedItems', { maxResults => 3, categoryId => 267 } ); | ||
| 192 | |||
| 193 | This method will construct the API request based on the $verb and | ||
| 194 | the $call_data. | ||
| 195 | |||
| 196 | =item $verb (required) | ||
| 197 | |||
| 198 | call verb, i.e. getMostWatchedItems | ||
| 199 | |||
| 200 | =item $call_data (required) | ||
| 201 | |||
| 202 | hashref of call_data that will be turned into xml. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub prepare { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | $self->{verb} = shift; | ||
| 210 | $self->{call_data} = shift; | ||
| 211 | |||
| 212 | if ( ! defined $self->{verb} || ! defined $self->{call_data} ) { | ||
| 213 | die "missing verb and call_data"; | ||
| 214 | } | ||
| 215 | |||
| 216 | # make sure we have appid | ||
| 217 | $self->_load_credentials(); | ||
| 218 | } | ||
| 219 | |||
| 220 | =head1 BASECLASS METHODS | ||
| 221 | |||
| 222 | =head2 request_agent | ||
| 223 | |||
| 224 | Accessor for the LWP::UserAgent request agent | ||
| 225 | |||
| 226 | =head2 request_object | ||
| 227 | |||
| 228 | Accessor for the HTTP::Request request object | ||
| 229 | |||
| 230 | =head2 request_content | ||
| 231 | |||
| 232 | Accessor for the complete request body from the HTTP::Request object | ||
| 233 | |||
| 234 | =head2 response_content | ||
| 235 | |||
| 236 | Accessor for the HTTP response body content | ||
| 237 | |||
| 238 | =head2 response_object | ||
| 239 | |||
| 240 | Accessor for the HTTP::Request response object | ||
| 241 | |||
| 242 | =head2 response_dom | ||
| 243 | |||
| 244 | Accessor for the LibXML response DOM | ||
| 245 | |||
| 246 | =head2 response_hash | ||
| 247 | |||
| 248 | Accessor for the hashified response content | ||
| 249 | |||
| 250 | =head2 nodeContent( $tag, [ $dom ] ) | ||
| 251 | |||
| 252 | Helper for LibXML that retrieves node content | ||
| 253 | |||
| 254 | =head2 errors | ||
| 255 | |||
| 256 | Accessor to the hashref of errors | ||
| 257 | |||
| 258 | =head2 has_error | ||
| 259 | |||
| 260 | Returns true if the call contains errors | ||
| 261 | |||
| 262 | =head2 errors_as_string | ||
| 263 | |||
| 264 | Returns a string of API errors if there are any. | ||
| 265 | |||
| 266 | =head1 PRIVATE METHODS | ||
| 267 | |||
| 268 | =head2 _get_request_body | ||
| 269 | |||
| 270 | This method supplies the XML body for the web service request | ||
| 271 | |||
| 272 | =cut | ||
| 273 | |||
| 274 | sub _get_request_body { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | my $xml = "<?xml version='1.0' encoding='utf-8'?>" | ||
| 278 | . "<" . $self->{verb} . "Request xmlns=\"http://www.ebay.com/marketplace/services\">" | ||
| 279 | . XMLout( | ||
| 280 | $self->{call_data}, | ||
| 281 | NoAttr => !$self->api_config->{enable_attributes}, | ||
| 282 | KeepRoot => 1, | ||
| 283 | RootName => undef | ||
| 284 | ) | ||
| 285 | . "</" . $self->{verb} . "Request>"; | ||
| 286 | |||
| 287 | return $xml; | ||
| 288 | } | ||
| 289 | |||
| 290 | =head2 _get_request_headers | ||
| 291 | |||
| 292 | This method supplies the HTTP::Headers obj for the web service request | ||
| 293 | |||
| 294 | =cut | ||
| 295 | |||
| 296 | sub _get_request_headers { | ||
| 297 | my $self = shift; | ||
| 298 | |||
| 299 | my $obj = HTTP::Headers->new(); | ||
| 300 | |||
| 301 | if ( defined $self->api_config->{version} ) { | ||
| 302 | $obj->push_header("X-EBAY-SOA-SERVICE-VERSION" | ||
| 303 | => $self->api_config->{version}); | ||
| 304 | } | ||
| 305 | $obj->push_header("X-EBAY-SOA-SERVICE-NAME" => "MerchandisingService" ); | ||
| 306 | $obj->push_header("EBAY-SOA-CONSUMER-ID" => $self->api_config->{appid}); | ||
| 307 | $obj->push_header("X-EBAY-SOA-GLOBAL-ID" => $self->api_config->{siteid}); | ||
| 308 | $obj->push_header("X-EBAY-SOA-OPERATION-NAME" => $self->{verb}); | ||
| 309 | $obj->push_header("X-EBAY-SOA-REQUEST-DATA-FORMAT" | ||
| 310 | => $self->api_config->{request_encoding}); | ||
| 311 | $obj->push_header("X-EBAY-SOA-RESPONSE-DATA-FORMAT" | ||
| 312 | => $self->api_config->{response_encoding}); | ||
| 313 | $obj->push_header("Content-Type" => "text/xml"); | ||
| 314 | |||
| 315 | return $obj; | ||
| 316 | } | ||
| 317 | |||
| 318 | =head2 _get_request_object | ||
| 319 | |||
| 320 | This method creates and returns the HTTP::Request object for the | ||
| 321 | web service call. | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub _get_request_object { | ||
| 326 | my $self = shift; | ||
| 327 | |||
| 328 | my $url = sprintf( 'http%s://%s%s', | ||
| 329 | ( $self->api_config->{https} ? 's' : '' ), | ||
| 330 | $self->api_config->{domain}, | ||
| 331 | $self->api_config->{uri} | ||
| 332 | ); | ||
| 333 | |||
| 334 | my $request_obj = HTTP::Request->new( | ||
| 335 | "POST", | ||
| 336 | $url, | ||
| 337 | $self->_get_request_headers, | ||
| 338 | $self->_get_request_body | ||
| 339 | ); | ||
| 340 | |||
| 341 | return $request_obj; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub _load_credentials { | ||
| 345 | my $self = shift; | ||
| 346 | |||
| 347 | # we only need to load credentials once | ||
| 348 | return if $self->{_credentials_loaded}; | ||
| 349 | |||
| 350 | my @missing; | ||
| 351 | |||
| 352 | # required by the API | ||
| 353 | for my $p ( qw/appid/ ) { | ||
| 354 | next if defined $self->api_config->{$p}; | ||
| 355 | |||
| 356 | if ( my $val = $self->_fish_ebay_ini( $p ) ) { | ||
| 357 | $self->api_config->{$p} = $val; | ||
| 358 | } | ||
| 359 | else { | ||
| 360 | push( @missing, $p ); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | # die if we didn't get everything | ||
| 365 | if ( scalar @missing > 0 ) { | ||
| 366 | die "missing API credential: " . join( ", ", @missing ); | ||
| 367 | } | ||
| 368 | |||
| 369 | $self->{_credentials_loaded} = 1; | ||
| 370 | return; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub _fish_ebay_ini { | ||
| 374 | my $self = shift; | ||
| 375 | my $arg = shift; | ||
| 376 | |||
| 377 | # initialize our hashref | ||
| 378 | $self->{_ebay_ini} ||= {}; | ||
| 379 | |||
| 380 | # revert eBay::API::Simple keys to standard keys | ||
| 381 | $arg = 'ApplicationKey' if $arg eq 'appid'; | ||
| 382 | |||
| 383 | # return it if we've already found it | ||
| 384 | return $self->{_ebay_ini}{$arg} if defined $self->{_ebay_ini}{$arg}; | ||
| 385 | |||
| 386 | # ini files in order of importance | ||
| 387 | my @files = ( | ||
| 388 | './ebay.ini', | ||
| 389 | "$ENV{HOME}/ebay.ini", | ||
| 390 | '/etc/ebay.ini', | ||
| 391 | ); | ||
| 392 | |||
| 393 | foreach my $file ( reverse @files ) { | ||
| 394 | if ( open( FILE, "<", $file ) ) { | ||
| 395 | |||
| 396 | while ( my $line = <FILE> ) { | ||
| 397 | chomp( $line ); | ||
| 398 | |||
| 399 | next if $line =~ m!^\s*\#!; | ||
| 400 | |||
| 401 | my( $k, $v ) = split( /=/, $line ); | ||
| 402 | |||
| 403 | if ( defined $k && defined $v) { | ||
| 404 | $v =~ s/^\s+//; | ||
| 405 | $v =~ s/\s+$//; | ||
| 406 | |||
| 407 | $self->{_ebay_ini}{$k} = $v; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | close FILE; | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | return $self->{_ebay_ini}{$arg} if defined $self->{_ebay_ini}{$arg}; | ||
| 416 | return undef; | ||
| 417 | } | ||
| 418 | |||
| 419 | =head1 AUTHOR | ||
| 420 | |||
| 421 | Tim Keefer <tim@timkeefer.com> | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | 1; |