Amazon-PAApi5-Signature

 view release on metacpan or  search on metacpan

lib/Amazon/PAApi5/Payload.pm  view on Meta::CPAN

package Amazon::PAApi5::Payload;
use strict;
use warnings;
use Carp qw/croak/;
use String::CamelCase qw/decamelize/;
use JSON qw//;
use Class::Accessor::Lite (
    rw  => [qw/
        partner_tag
        marketplace
        partner_type
    /],
);

sub new {
    my $class       = shift;
    my $partner_tag = shift or croak 'partner_tag is required';
    my $marketplace = shift || 'www.amazon.com';
    my $opt         = shift || {};

    return bless {
        partner_tag  => $partner_tag,
        marketplace  => $marketplace,
        partner_type => $opt->{partner_type} || 'Associates',
    }, $class;
}

sub to_json {
    my ($self, $data) = @_;

    my $hash = {};

    for my $k (keys %{$data}) {
        $hash->{$k} = $data->{$k};
    }

    for my $k (qw/
        PartnerTag
        Marketplace
        PartnerType
    /) {
        my $method = decamelize($k);
        $hash->{$k} = $self->$method;
    }

    return JSON::to_json($hash, { utf8 => 0, canonical => 1 });
}

1;

__END__

=encoding UTF-8

=head1 NAME

Amazon::PAApi5::Payload - Handle request body


=head1 SYNOPSIS

    use Amazon::PAApi5::Payload;

    my $payload = Amazon::PAApi5::Payload->new(
        'PARTNER_TAG'
    );

    say $payload->to_json({
        Keywords    => 'Perl',
        SearchIndex => 'All',
        ItemCount   => 2,
        Resources   => [qw/
            ItemInfo.Title
        /],
    });

=head1 DESCRIPTION

Amazon::PAApi5::Payload handles request body

See B<example/> directory of this module.



( run in 2.339 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )