Amazon-CreatorsAPI

 view release on metacpan or  search on metacpan

lib/Amazon/CreatorsAPI.pm  view on Meta::CPAN

use Amazon::CreatorsAPI::Auth;
use Class::Accessor::Lite (
    ro  => [qw/
        credential_id
        credential_secret
        credential_version
        partner_tag
        marketplace
        ua
        auth_manager
        operation_endpoint
    /],
);

our $VERSION = '0.01';

our $JSON = JSON->new;

sub new {
    my $class              = shift;
    my $credential_id      = shift or croak 'credential_id is required';

lib/Amazon/CreatorsAPI.pm  view on Meta::CPAN

        credential_version => $credential_version,
        partner_tag        => $opt->{partner_tag} || '',
        marketplace        => $opt->{marketplace} || 'www.amazon.com',
        ua                 => $opt->{ua},
        auth_manager       => Amazon::CreatorsAPI::Auth->new(
            $credential_id,
            $credential_secret,
            $credential_version,
            $opt,
        ),
        operation_endpoint => $opt->{operation_endpoint} || 'https://creatorsapi.amazon/catalog/v1',
    }, $class;
}

sub get_browse_nodes {
    return shift->operation('getBrowseNodes', @_);
}

sub get_items {
    return shift->operation('getItems', @_);
}

lib/Amazon/CreatorsAPI.pm  view on Meta::CPAN

    return shift->operation('searchItems', @_);
}

sub operation {
    my $self      = shift;
    my $operation = shift || '';
    my $params    = shift || +{};

    my $res = $self->ua->request(
        'POST',
        $self->operation_endpoint . '/' . $operation,
        {
            'headers' => {
                'Authorization' => $self->_auth_header,
                'Content-Type' => 'application/json',
                'x-marketplace' => $self->marketplace,
            },
            'content' => $JSON->encode({
                partnerTag => $self->partner_tag,
                marketplace => $self->marketplace,
                %{$params},

lib/Amazon/CreatorsAPI/Auth.pm  view on Meta::CPAN

use Carp qw/croak/;
use JSON qw//;
use HTTP::Tiny;
use WWW::Form::UrlEncoded qw/build_urlencoded/;
use Class::Accessor::Lite (
    ro => [qw/
        credential_id
        credential_secret
        credential_version
        is_lwa
        auth_endpoint
        ua
        grant_type
    /],
    rw => [qw/
        access_token
        expires_at
    /],
);

our $JSON = JSON->new;

lib/Amazon/CreatorsAPI/Auth.pm  view on Meta::CPAN

    my $credential_id      = shift or croak 'credential_id is required';
    my $credential_secret  = shift or croak 'credential_secret is required';
    my $credential_version = shift or croak 'credential_version is required';
    my $opt                = shift || +{};

    return bless +{
        credential_id      => $credential_id,
        credential_secret  => $credential_secret,
        credential_version => $credential_version,
        is_lwa             => !!($credential_version =~ m!^3\.!),
        auth_endpoint      => $opt->{auth_endpoint} || _auth_endpoint($credential_version),
        ua                 => $opt->{ua} || HTTP::Tiny->new,
        grant_type         => $opt->{grant_type} || 'client_credentials',
        access_token       => '',
        expires_at         => 0,
    }, $class;
}

sub get_access_token {
    my $self = shift;

lib/Amazon/CreatorsAPI/Auth.pm  view on Meta::CPAN

    return $self->access_token && $self->expires_at && time() < $self->expires_at;
}

sub _refresh_token {
    my $self = shift;

    my $res;
    if ($self->is_lwa) {
        $res = $self->ua->request(
            'POST',
            $self->auth_endpoint,
            {
                'headers' => {
                    'Content-Type' => 'application/json',
                },
                'content' => $JSON->encode({
                    'grant_type' => $self->grant_type,
                    'client_id'  => $self->credential_id,
                    'client_secret' => $self->credential_secret,
                    'scope' => 'creatorsapi::default',
                }),
            },
        );
    }
    else {
        $res = $self->ua->request(
            'POST',
            $self->auth_endpoint,
            {
                'headers' => {
                    'Content-Type' => 'application/x-www-form-urlencoded',
                },
                'content' => build_urlencoded(
                    'grant_type' => $self->grant_type,
                    'client_id'  => $self->credential_id,
                    'client_secret' => $self->credential_secret,
                    'scope' => 'creatorsapi/default',
                ),

lib/Amazon/CreatorsAPI/Auth.pm  view on Meta::CPAN


my $AUTH_ENDPOINT_MAP = {
    '2.1' => 'https://creatorsapi.auth.us-east-1.amazoncognito.com/oauth2/token',
    '2.2' => 'https://creatorsapi.auth.eu-south-2.amazoncognito.com/oauth2/token',
    '2.3' => 'https://creatorsapi.auth.us-west-2.amazoncognito.com/oauth2/token',
    '3.1' => 'https://api.amazon.com/auth/o2/token',
    '3.2' => 'https://api.amazon.co.uk/auth/o2/token',
    '3.3' => 'https://api.amazon.co.jp/auth/o2/token',
};

sub _auth_endpoint {
    my $version = shift;

    if (!$version || !exists $AUTH_ENDPOINT_MAP->{$version}) {
        croak "Unsupported version: "
            . ($version || 'unknown')
            . ", Supported: " . join(", ", sort keys %{$AUTH_ENDPOINT_MAP});
    }

    return $AUTH_ENDPOINT_MAP->{$version};
}



( run in 2.047 seconds using v1.01-cache-2.11-cpan-524268b4103 )