package BookPub::RDAS::Request::ProductSearch::Amazon;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::RDAS::Request::ProductSearch';

use BookPub::RDAS::Parser::ProductSearch::Amazon;

sub userAgent { 'iPhone 3.0' }

sub _validOptions {
    my @options = shift->SUPER::_validOptions();
    push( @options, bookID );
    return @options;
}

sub _getParserObject {
    my $self = shift;
    return new BookPub::RDAS::Parser::ProductSearch::Amazon( @_, bookID => $self->bookID );
}
sub _getResponseObject { }

sub Author {
    my $self = shift || return;
    my $parser = $self->parser;

    return $parser ? $parser->Author : undef;
}

sub URL {
    my $self = shift || return;
    my $parser = $self->parser;

    return $parser ? $parser->URL : $self->redirectURL;
}

sub Format {
    my $self = shift || return;
    my $parser = $self->parser;

    return $parser ? $parser->Format : undef;
}

sub fetch {
    my $self  = shift;
    my $fetch = $self->_getFetchObject()
      || die "No fetch object created, can not continue";

    $self->{_content} = $fetch->content();

    if ( $fetch->success ) {
        return 1;
    } elsif ( $fetch->errorCode eq "302" ) {
        $self->{_redirectURL} = $fetch->redirectURL();
        return;
    } else {
        $self->fail( code => $fetch->errorCode );
        return;
    }
}

sub redirectURL { shift->{_redirectURL} }

1;
