File Coverage

blib/lib/Amazon/CreatorsAPI.pm
Criterion Covered Total %
statement 29 50 58.0
branch 4 14 28.5
condition 4 14 28.5
subroutine 8 14 57.1
pod 6 6 100.0
total 51 98 52.0


line stmt bran cond sub pod time code
1             package Amazon::CreatorsAPI;
2 2     2   279870 use strict;
  2         4  
  2         89  
3 2     2   10 use warnings;
  2         10  
  2         144  
4 2     2   12 use Carp qw/croak/;
  2         5  
  2         138  
5 2     2   1747 use JSON qw//;
  2         36911  
  2         87  
6 2     2   1743 use HTTP::Tiny;
  2         126526  
  2         130  
7 2     2   1144 use Amazon::CreatorsAPI::Auth;
  2         10  
  2         104  
8             use Class::Accessor::Lite (
9 2         12 ro => [qw/
10             credential_id
11             credential_secret
12             credential_version
13             partner_tag
14             marketplace
15             ua
16             auth_manager
17             operation_endpoint
18             /],
19 2     2   45 );
  2         5  
20              
21             our $VERSION = '0.01';
22              
23             our $JSON = JSON->new;
24              
25             sub new {
26 1     1 1 191915 my $class = shift;
27 1 50       6 my $credential_id = shift or croak 'credential_id is required';
28 1 50       17 my $credential_secret = shift or croak 'credential_secret is required';
29 1 50       3 my $credential_version = shift or croak 'credential_version is required';
30 1   50     6 my $opt = shift || +{};
31              
32 1 50       3 if (!$opt->{ua}) {
33 1         12 $opt->{ua} = HTTP::Tiny->new;
34             }
35              
36             bless +{
37             credential_id => $credential_id,
38             credential_secret => $credential_secret,
39             credential_version => $credential_version,
40             partner_tag => $opt->{partner_tag} || '',
41             marketplace => $opt->{marketplace} || 'www.amazon.com',
42             ua => $opt->{ua},
43             auth_manager => Amazon::CreatorsAPI::Auth->new(
44             $credential_id,
45             $credential_secret,
46             $credential_version,
47             $opt,
48             ),
49 1   50     129 operation_endpoint => $opt->{operation_endpoint} || 'https://creatorsapi.amazon/catalog/v1',
      50        
      50        
50             }, $class;
51             }
52              
53             sub get_browse_nodes {
54 0     0 1   return shift->operation('getBrowseNodes', @_);
55             }
56              
57             sub get_items {
58 0     0 1   return shift->operation('getItems', @_);
59             }
60              
61             sub get_variations {
62 0     0 1   return shift->operation('getVariations', @_);
63             }
64              
65             sub search_items {
66 0     0 1   return shift->operation('searchItems', @_);
67             }
68              
69             sub operation {
70 0     0 1   my $self = shift;
71 0   0       my $operation = shift || '';
72 0   0       my $params = shift || +{};
73              
74             my $res = $self->ua->request(
75             'POST',
76             $self->operation_endpoint . '/' . $operation,
77             {
78             'headers' => {
79             'Authorization' => $self->_auth_header,
80             'Content-Type' => 'application/json',
81             'x-marketplace' => $self->marketplace,
82             },
83             'content' => $JSON->encode({
84             partnerTag => $self->partner_tag,
85             marketplace => $self->marketplace,
86 0           %{$params},
  0            
87             }),
88             },
89             );
90              
91 0 0         if (!$res->{success}) {
92             croak "failed $operation status:$res->{status} "
93 0   0       . ($res->{reason} || 'reason_unknown')
94             . ", $res->{content}";
95             }
96              
97 0           my $res_data = +{};
98 0           eval {
99 0           $res_data = $JSON->decode($res->{content});
100             };
101 0 0         if (my $e = $@) {
102 0           croak "could not JSON decode, $e : " . $res->{content};
103             }
104              
105 0           return $res_data;
106             }
107              
108             sub _auth_header {
109 0     0     my $self = shift;
110              
111 0           my $am = $self->auth_manager;
112 0           my $token = $am->get_access_token;
113              
114 0 0         return "Bearer $token" . (
115             !$am->is_lwa ? ', Version ' . $self->credential_version : ''
116             );
117             }
118              
119             1;
120              
121             __END__