URI::Amazon::APA - URI to access Amazon Product Advertising API


URI-Amazon-APA documentation Contained in the URI-Amazon-APA distribution.

Index


Code Index:

NAME

Top

URI::Amazon::APA - URI to access Amazon Product Advertising API

VERSION

Top

$Id: APA.pm,v 0.4 2011/05/21 21:53:23 dankogai Exp dankogai $

SYNOPSIS

Top

  # self-explanatory
  use strict;
  use warnings;
  use URI::Amazon::APA;
  use LWP::UserAgent;
  use XML::Simple;
  use YAML::Syck;

  use URI::Amazon::APA; # instead of URI
  my $u = URI::Amazon::APA->new('http://webservices.amazon.com/onca/xml');
  $u->query_form(
    Service     => 'AWSECommerceService',
    Operation   => 'ItemSearch',
    Title       => shift || 'Perl',
    SearchIndex => 'Books',
  );
  $u->sign(
    key    => $public_key,
    secret => $private_key,
  );

  my $ua = LWP::UserAgent->new;
  my $r  = $ua->get($u);
  if ( $r->is_success ) {
    print YAML::Syck::Dump( XMLin( $r->content ) );
  }
  else {
    print $r->status_line, $r->as_string;
  }

EXPORT

Top

None.

METHODS

Top

This adds the following methods to URI object

sign

Sings the URI accordingly to the Amazon Product Advertising API.

  $u->sign(
    key    => $public_key,
    secret => $private_key,
  );

signature

Checks the signature within the URI;

  print "The signature is " : $u->signature;

AUTHOR

Top

Dan Kogai, <dankogai at dan.co.jp>

BUGS

Top

Please report any bugs or feature requests to bug-uri-amazon-apa at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-Amazon-APA. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc URI::Amazon::APA




You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=URI-Amazon-APA

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/URI-Amazon-APA

* CPAN Ratings

http://cpanratings.perl.org/d/URI-Amazon-APA

* Search CPAN

http://search.cpan.org/dist/URI-Amazon-APA/

ACKNOWLEDGEMENTS

Top

http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html

COPYRIGHT & LICENSE

Top


URI-Amazon-APA documentation Contained in the URI-Amazon-APA distribution.

package URI::Amazon::APA;
use warnings;
use strict;
our $VERSION = sprintf "%d.%02d", q$Revision: 0.4 $ =~ /(\d+)/g;
use Carp;
use Digest::SHA qw(hmac_sha256_base64);
use URI::Escape;
use Encode qw/decode_utf8/;
use base 'URI::http';

sub new{
    my $class = shift;
    my $self  = URI->new(@_);
    ref $self eq 'URI::http' or carp "must be http";
    bless $self, $class;
}

sub sign {
    my $self  = shift;
    my (%arg) = @_;
    my %eq    = map { split /=/, $_ } split /&/, $self->query();
    my %q     = map { $_ => decode_utf8( uri_unescape( $eq{$_} ) ) } keys %eq;
    $q{AWSAccessKeyId} = $arg{key};
    $q{Timestamp} ||= do {
        my ( $ss, $mm, $hh, $dd, $mo, $yy ) = gmtime();
        join '',
          sprintf( '%04d-%02d-%02d', $yy + 1900, $mo + 1, $dd ), 'T',
          sprintf( '%02d:%02d:%02d', $hh,        $mm,     $ss ), 'Z';
    };
    $q{Version} ||= '2010-09-01';
    my $sq = join '&',
      map { $_ . '=' . uri_escape_utf8( $q{$_}, "^A-Za-z0-9\-_.~" ) }
      sort keys %q;
    my $tosign = join "\n", 'GET', $self->host, $self->path, $sq;
    my $signature = hmac_sha256_base64( $tosign, $arg{secret} );
    $signature .= '=' while length($signature) % 4;    # padding required
    $q{Signature} = $signature;
    $self->query_form( \%q );
    $self;
}

sub signature {
    my $self  = shift;
    my (%arg) = @_;
    my %eq = map { split /=/, $_ } split /&/, $self->query();
    my %q = map { $_ => uri_unescape( $eq{$_} ) } keys %eq;
    $q{Signature};
}

1; # End of URI::Amazon::APA