| 1 | package URI::Amazon::APA; | ||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.5 $ =~ /(\d+)/g; | |||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Digest::SHA qw(hmac_sha256_base64); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use Encode qw/decode_utf8/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use base 'URI::http'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | sub new{ | ||||||||||||||||||||
| 12 | 1 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 13 | 1 | my $self = URI->new(@_); | |||||||||||||||||||
| 14 | 1 | ▶ | ref $self eq 'URI::http' or carp "must be http"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 15 | 1 | bless $self, $class; | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | sub sign { | ||||||||||||||||||||
| 19 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
sign: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||
| 20 | 1 | my (%arg) = @_; | |||||||||||||||||||
| 21 | 1 | my %eq = map { split /=/, $_ } split /&/, $self->query(); | |||||||||||||||||||
| 22 | 1 | my %q = map { $_ => decode_utf8( uri_unescape( $eq{$_} ) ) } keys %eq; | |||||||||||||||||||
| 23 | 1 | ▶ | $q{Keywords} =~ s/\+/ /g if $q{Keywords}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 1 | $q{AWSAccessKeyId} = $arg{key}; | |||||||||||||||||||
| 25 | 1 | ▶ | $q{Timestamp} ||= do { | ||||||||||||||||||
|
Condition33%1/3
$q{"Timestamp"} ||= do {
() = gmtime();
join "", sprintf("%04d-%02d-%02d", $yy + 1900, $mo + 1, $dd), "T", sprintf("%02d:%02d:%02d", $hh, $mm, $ss), "Z"
}
MC/DC0%0/2
$q{"Timestamp"} ||= do {
() = gmtime();
join "", sprintf("%04d-%02d-%02d", $yy + 1900, $mo + 1, $dd), "T", sprintf("%02d:%02d:%02d", $hh, $mm, $ss), "Z"
}
$q{"Timestamp"}
do {
() = gmtime();
join "", sprintf("%04d-%02d-%02d", $yy + 1900, $mo + 1, $dd), "T", sprintf("%02d:%02d:%02d", $hh, $mm, $ss), "Z"
}
Truth table33%1/3
A ||= B
A= $q{"Timestamp"}B= do {
() = gmtime();
join "", sprintf("%04d-%02d-%02d", $yy + 1900, $mo + 1, $dd), "T", sprintf("%02d:%02d:%02d", $hh, $mm, $ss), "Z"
}
| |||||||||||||||||||||
| 26 | 0 | my ( $ss, $mm, $hh, $dd, $mo, $yy ) = gmtime(); | |||||||||||||||||||
| 27 | 0 | join '', | |||||||||||||||||||
| 28 | sprintf( '%04d-%02d-%02d', $yy + 1900, $mo + 1, $dd ), 'T', | ||||||||||||||||||||
| 29 | sprintf( '%02d:%02d:%02d', $hh, $mm, $ss ), 'Z'; | ||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||
| 31 | 1 | ▶ | $q{Version} ||= '2010-09-01'; | ||||||||||||||||||
|
Condition50%1/2
$q{"Version"} ||= "2010-09-01"
MC/DC0%0/1
$q{"Version"} ||= "2010-09-01"
$q{"Version"}
Truth table50%1/2
A
A= $q{"Version"}
| |||||||||||||||||||||
| 32 | my $sq = join '&', | ||||||||||||||||||||
| 33 | 1 | map { $_ . '=' . uri_escape_utf8( $q{$_}, "^A-Za-z0-9\-_.~" ) } | |||||||||||||||||||
| 34 | sort keys %q; | ||||||||||||||||||||
| 35 | 1 | my $tosign = join "\n", 'GET', $self->host, $self->path, $sq; | |||||||||||||||||||
| 36 | 1 | my $signature = hmac_sha256_base64( $tosign, $arg{secret} ); | |||||||||||||||||||
| 37 | 1 | ▶ | $signature .= '=' while length($signature) % 4; # padding required | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 1 | $q{Signature} = $signature; | |||||||||||||||||||
| 39 | 1 | $self->query_form( \%q ); | |||||||||||||||||||
| 40 | 1 | $self; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | sub signature { | ||||||||||||||||||||
| 44 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
signature: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 45 | 1 | my (%arg) = @_; | |||||||||||||||||||
| 46 | 1 | my %eq = map { split /=/, $_ } split /&/, $self->query(); | |||||||||||||||||||
| 47 | 1 | my %q = map { $_ => uri_unescape( $eq{$_} ) } keys %eq; | |||||||||||||||||||
| 48 | 1 | $q{Signature}; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | 1; # End of URI::Amazon::APA | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 NAME | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | URI::Amazon::APA - URI to access Amazon Product Advertising API | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =head1 VERSION | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | $Id: APA.pm,v 0.5 2013/07/16 18:31:07 dankogai Exp $ | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head1 SYNOPSIS | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # self-explanatory | ||||||||||||||||||||
| 64 | use strict; | ||||||||||||||||||||
| 65 | use warnings; | ||||||||||||||||||||
| 66 | use URI::Amazon::APA; | ||||||||||||||||||||
| 67 | use LWP::UserAgent; | ||||||||||||||||||||
| 68 | use XML::Simple; | ||||||||||||||||||||
| 69 | use YAML::Syck; | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | use URI::Amazon::APA; # instead of URI | ||||||||||||||||||||
| 72 | my $u = URI::Amazon::APA->new('http://webservices.amazon.com/onca/xml'); | ||||||||||||||||||||
| 73 | $u->query_form( | ||||||||||||||||||||
| 74 | Service => 'AWSECommerceService', | ||||||||||||||||||||
| 75 | Operation => 'ItemSearch', | ||||||||||||||||||||
| 76 | Title => shift || 'Perl', | ||||||||||||||||||||
| 77 | SearchIndex => 'Books', | ||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||
| 79 | $u->sign( | ||||||||||||||||||||
| 80 | key => $public_key, | ||||||||||||||||||||
| 81 | secret => $private_key, | ||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | my $ua = LWP::UserAgent->new; | ||||||||||||||||||||
| 85 | my $r = $ua->get($u); | ||||||||||||||||||||
| 86 | if ( $r->is_success ) { | ||||||||||||||||||||
| 87 | print YAML::Syck::Dump( XMLin( $r->content ) ); | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||
| 90 | print $r->status_line, $r->as_string; | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =head1 EXPORT | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | None. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =head1 METHODS | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | This adds the following methods to L<URI> object | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =head2 sign | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Sings the URI accordingly to the Amazon Product Advertising API. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | $u->sign( | ||||||||||||||||||||
| 106 | key => $public_key, | ||||||||||||||||||||
| 107 | secret => $private_key, | ||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 signature | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Checks the signature within the URI; | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | print "The signature is " : $u->signature; | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | =head1 AUTHOR | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | Dan Kogai, C<< <dankogai at dan.co.jp> >> | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =head1 BUGS | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Please report any bugs or feature requests to C<bug-uri-amazon-apa at rt.cpan.org>, or through | ||||||||||||||||||||
| 123 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-Amazon-APA>. I will be notified, and then you'll | ||||||||||||||||||||
| 124 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 SUPPORT | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | perldoc URI::Amazon::APA | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | You can also look for information at: | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =over 4 | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =item * RT: CPAN's request tracker | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=URI-Amazon-APA> | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | L<http://annocpan.org/dist/URI-Amazon-APA> | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =item * CPAN Ratings | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | L<http://cpanratings.perl.org/d/URI-Amazon-APA> | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =item * Search CPAN | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | L<http://search.cpan.org/dist/URI-Amazon-APA/> | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | L<http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html> | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Copyright 2009 Dan Kogai, all rights reserved. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 164 | under the same terms as Perl itself. | ||||||||||||||||||||