| 1 | package eBay::API::Simple::Parallel; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'LWP::Parallel::UserAgent'; | ||
| 7 | use utf8; | ||
| 8 | |||
| 9 | our $DEBUG = 0; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | eBay::API::Simple::Parallel - Support for parallel requests | ||
| 14 | |||
| 15 | =head1 USAGE | ||
| 16 | |||
| 17 | my $pua = eBay::API::Simple::Parallel->new(); | ||
| 18 | |||
| 19 | my $call1 = eBay::API::Simple::RSS->new( { | ||
| 20 | parallel => $pua, | ||
| 21 | } ); | ||
| 22 | |||
| 23 | $call1->execute( | ||
| 24 | 'http://worldofgood.ebay.com/Clothes-Shoes-Men/43/list?format=rss', | ||
| 25 | ); | ||
| 26 | |||
| 27 | my $call2 = eBay::API::Simple::RSS->new( { | ||
| 28 | parallel => $pua, | ||
| 29 | } ); | ||
| 30 | |||
| 31 | $call2->execute( | ||
| 32 | 'http://worldofgood.ebay.com/Home-Garden/46/list?format=rss' | ||
| 33 | ); | ||
| 34 | |||
| 35 | $pua->wait(); | ||
| 36 | |||
| 37 | if ( $pua->has_error() ) { | ||
| 38 | print "ONE OR MORE FAILURES!\n"; | ||
| 39 | } | ||
| 40 | |||
| 41 | print $call1->request_content() . "\n"; | ||
| 42 | print $call2->response_content() "\n"; | ||
| 43 | |||
| 44 | |||
| 45 | =head1 PUBLIC METHODS | ||
| 46 | |||
| 47 | =head2 new() | ||
| 48 | |||
| 49 | my $pua = ebay::API::Simple::Parallel->new(); | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my $class = shift; | ||
| 55 | my $self = $class->SUPER::new( @_ ); | ||
| 56 | |||
| 57 | $self->{has_error} = 0; | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | =head2 wait( $timeout ) | ||
| 63 | |||
| 64 | $pua->wait(); | ||
| 65 | |||
| 66 | This method will wait for all requests to complete with an optional timeout. | ||
| 67 | |||
| 68 | An array of object instances will be returned. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | sub wait { | ||
| 73 | my $self = shift; | ||
| 74 | my $timeout = shift; | ||
| 75 | |||
| 76 | my @objects; | ||
| 77 | my $entries = $self->SUPER::wait( $timeout ); | ||
| 78 | |||
| 79 | for my $key ( keys %$entries ) { | ||
| 80 | push( @objects, $entries->{$key}->request->{_ebay_api_simple_instance} ); | ||
| 81 | delete( $entries->{$key}->request->{_ebay_api_simple_instance}->{parallel} ); | ||
| 82 | delete( $entries->{$key}->request->{_ebay_api_simple_instance} ); | ||
| 83 | } | ||
| 84 | |||
| 85 | return \@objects; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 has_error | ||
| 89 | |||
| 90 | Returns true if any of the calls contain an error. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub has_error { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | return $self->{has_error}; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub on_connect { | ||
| 101 | my $self = shift; | ||
| 102 | my $request = shift; | ||
| 103 | my $response = shift; | ||
| 104 | my $entry = shift; | ||
| 105 | |||
| 106 | if( $DEBUG ) { | ||
| 107 | print STDERR "Parallel Connect: " . $request->url . "\n"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub on_return { | ||
| 112 | my $self = shift; | ||
| 113 | my $request = shift; | ||
| 114 | my $response = shift; | ||
| 115 | my $entry = shift; | ||
| 116 | |||
| 117 | if( $DEBUG ) { | ||
| 118 | print STDERR "Parallel Return: " . $request->url . "\n"; | ||
| 119 | } | ||
| 120 | |||
| 121 | $request->{_ebay_api_simple_instance}->_process_http_request( $response ); | ||
| 122 | $request->{_ebay_api_simple_instance}->process(); | ||
| 123 | |||
| 124 | if( $request->{_ebay_api_simple_instance}->has_error ) { | ||
| 125 | $self->{has_error} = 1; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub on_failure { | ||
| 130 | my $self = shift; | ||
| 131 | my $request = shift; | ||
| 132 | my $response = shift; | ||
| 133 | my $entry = shift; | ||
| 134 | |||
| 135 | if( $DEBUG ) { | ||
| 136 | print STDERR "Parallel Failure: " . $request->url . "\n"; | ||
| 137 | } | ||
| 138 | |||
| 139 | $request->{_ebay_api_simple_instance}->_process_http_request( $response ); | ||
| 140 | $request->{_ebay_api_simple_instance}->process(); | ||
| 141 | |||
| 142 | $self->{has_error} = 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | =head1 AUTHOR | ||
| 148 | |||
| 149 | Brian Gontowski <bgontowski@gmail.com> | ||
| 150 | |||
| 151 | =cut |