blib/lib/eBay/Client/OpenAPI3.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package eBay::Client::OpenAPI3;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use URI ();
8 use HTTP::Tiny;
9 use HTTP::Status;
10 use MIME::Base64;
11 use Util::H2O::More qw/baptise d2o ddd HTTPTiny2h2o h2o ini2h2o o2h/;
12  
13 our $VERSION = '0.01';
14 our $EBAY_ENDPOINT_BASE = q{https://api.ebay.com};
15  
16 sub new {
17 my $pkg = shift;
18 my %opts = @_;
19 my $self = baptise \%opts, $pkg, qw/config next token total/;
20 die qq{configuration file not found\n} if not -e $self->config;
21 my $config_file = $self->config; # initially, ->config is just a string file name
22 $self->config(ini2h2o $config_file); # then it becomes an actual Config::Tiny object with accessors
23 return $self;
24 }
25  
26 # Construction seam used by tests/subclasses; default behavior remains HTTP::Tiny.
27 sub _new_ua {
28 my ($self, %opts) = @_;
29 return HTTP::Tiny->new(%opts);
30 }
31  
32 sub oauth2 {
33 my ($self) = @_;
34 my $ua = $self->_new_ua();
35 my $URL = sprintf qq{%s/%s}, $EBAY_ENDPOINT_BASE, q{identity/v1/oauth2/token};
36 my $authorization = sprintf qq{%s:%s}, $self->config->eBay->client_id, $self->config->eBay->client_secret;
37 my $auth_token = sprintf qq{Basic %s}, encode_base64( $authorization, q{} );
38 my $options = {
39 headers => {
40 'Content-Type' => q{application/x-www-form-urlencoded},
41 'Authorization' => $auth_token,
42 },
43  
44 # define API scopes enabled by this token
45 content => q{grant_type=client_credentials&scope=https://api.ebay.com/oauth/api_scope},
46 };
47  
48 my $resp = h2o $ua->post($URL, $options);
49  
50 my $full = h2o JSON::from_json $resp->content;
51  
52 # set token member
53 $self->token($full);
54  
55 # return $self, for chaining
56 return $self;
57 }
58  
59 sub warn_if_exists {
60 my ($self, $headers, $header) = @_;
61 if (exists $headers->{$header}) {
62 warn sprintf("WARNING: '%s' detected: %s\n\n", $header, $headers->{$header})
63 }
64 }
65  
66 sub _defined_params {
67 my (%params) = @_;
68 return map { defined $params{$_} ? ( $_ => $params{$_} ) : () } keys %params;
69 }
70  
71 sub _error_message_from_json {
72 my ($json) = @_;
73 return eval { $json->errors->get(0)->longMessage }
74 // eval { $json->{errors}->[0]{longMessage} }
75 // q{Unknown error};
76 }
77  
78 # Call limit for all Browse APIs is 5,000 / day
79 # https://developer.ebay.com/develop/apis/api-call-limits
80  
81 sub get_ua {
82 my ($self) = @_;
83 my $auth_token = sprintf qq{Bearer %s}, $self->token->access_token;
84 my $options = {
85 default_headers => {
86 'Accept' => q{*/*},
87 'Authorization' => $auth_token,
88 'X-EBAY-C-MARKETPLACE-ID' => 'EBAY_US',
89 'X-EBAY-C-ENDUSERCTX' => sprintf('affiliateCampaignId=%s', $self->config->eBay->affiliateCampaignId),
90 },
91 # define API scopes enabled by this token
92 content => undef,
93 };
94 my $ua = $self->_new_ua(%$options);
95 return $ua;
96 }
97  
98 # https://api.ebay.com/developer/analytics/v1_beta/rate_limit?api_name=browse
99 sub rate_limit {
100 my ($self, %params) = @_;
101 my $ua = $self->get_ua;
102 my $uri = URI->new('', 'http');
103 $uri->query_form(_defined_params(%params));
104 my $URL = sprintf qq{%s/%s?%s}, $EBAY_ENDPOINT_BASE, q{developer/analytics/v1_beta/rate_limit}, $uri->query;
105  
106 my $resp = h2o $ua->get($URL);
107  
108 my $raw = $resp->content;
109 my $json = d2o from_json $raw;
110  
111 if (not is_success($resp->status)) {
112 $self->warn_if_exists($resp->headers, "x-ebay-api-call-limit");
113 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-limit");
114 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-remaining");
115 my $status = $resp->status;
116 my $msg = _error_message_from_json($json);
117 die "$msg (HTTP Status: $status)\n";
118 }
119  
120 return $json;
121 }
122  
123 # getItem (part of the 'Browse' API); only gets one item at a time
124 sub getItem {
125 my ($self, %params) = @_;
126 my $params = h2o \%params, qw/itemid/;
127 my $URL = sprintf qq{%s/%s/v1|%s|0}, $EBAY_ENDPOINT_BASE, q{buy/browse/v1/item}, $params->itemid;
128  
129 my $ua = $self->get_ua;
130 my $resp = HTTPTiny2h2o $ua->get($URL);
131  
132 # error handling
133 if (not is_success($resp->status)) {
134 $self->warn_if_exists($resp->headers, "x-ebay-api-call-limit");
135 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-limit");
136 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-remaining");
137 my $status = $resp->status;
138 my $msg = eval { $resp->content->errors->get(0)->longMessage } // "Unknown error";
139 die "$msg (HTTP Status: $status)\n";
140 }
141  
142 return $resp->content;;
143 }
144  
145 # Perl-style alias; retain getItem() for compatibility with existing callers.
146 sub get_item {
147 my ($self, @args) = @_;
148 return $self->getItem(@args);
149 }
150  
151 # https://developer.ebay.com/api-docs/buy/browse/resources/item_summary/methods/search
152 sub browse {
153 my ($self, %params) = @_;
154 my $ua = $self->get_ua;
155 my $uri = URI->new('', 'http');
156 $uri->query_form(_defined_params(%params));
157 my $URL = sprintf qq{%s/%s?%s}, $EBAY_ENDPOINT_BASE, q{buy/browse/v1/item_summary/search}, $uri->query;
158 my $resp = h2o $ua->get($URL);
159  
160 my $raw = $resp->content;
161 my $json = from_json $raw;
162 $json->{next} = $json->{next} // undef; # d2o should probably allow some top level
163 $json->{total} = $json->{total} // undef; # default accessors to be defined
164 $json->{warnings} = $json->{warnings} // []; # default accessors to be defined
165 $json->{errors} = $json->{errors} // [ { longMessage => undef } ];
166 $json = d2o $json;
167  
168 if (not is_success($resp->status)) {
169 $self->warn_if_exists($resp->headers, "x-ebay-api-call-limit");
170 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-limit");
171 $self->warn_if_exists($resp->headers, "x-ebay-api-throttle-remaining");
172 my $status = $resp->status;
173 my $msg = _error_message_from_json($json);
174 die "$msg (HTTP Status: $status)\n";
175 }
176  
177 # capture the next URL as member, "next"
178 $self->next($json->next);
179 $self->total($json->total);
180  
181 return $json;
182 }
183  
184 1;
185  
186 __END__
↑ Summary