| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# Copyright 2013 Digital River, Inc. |
|
2
|
|
|
|
|
|
|
# |
|
3
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
4
|
|
|
|
|
|
|
# you may not use this file except in compliance with the License. |
|
5
|
|
|
|
|
|
|
# You may obtain a copy of the License at |
|
6
|
|
|
|
|
|
|
# |
|
7
|
|
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
8
|
|
|
|
|
|
|
# |
|
9
|
|
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
10
|
|
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
11
|
|
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12
|
|
|
|
|
|
|
# See the License for the specific language governing permissions and |
|
13
|
|
|
|
|
|
|
# limitations under the License. |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
package Net::MyCommerce::API::Token; |
|
16
|
|
|
|
|
|
|
|
|
17
|
3
|
|
|
3
|
|
23765
|
use strict; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
105
|
|
|
18
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
74
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
3
|
|
|
3
|
|
507
|
use Net::MyCommerce::API::Client; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
1659
|
|
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Net::MyCommerce::API::Token |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 VERSION |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
version 1.0.1 |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $VERSION = '1.0.1'; |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 METHODS |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 new ( access => $access, vendor => $vendor, credentials => { id => $id, secret => $secret } ); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
access: 'vendor' or 'affiliate' |
|
39
|
|
|
|
|
|
|
vendor: vendor id (only needed for access=affiliate) |
|
40
|
|
|
|
|
|
|
id: MyCommerce vendor or affiliate ID |
|
41
|
|
|
|
|
|
|
secret: API secret appropriate to type of resource object requested |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub new { |
|
46
|
2
|
|
|
2
|
1
|
18
|
my ($pkg, %args) = @_; |
|
47
|
2
|
|
50
|
|
|
19
|
my $prefix = $args{prefix} || '/api'; |
|
48
|
2
|
|
50
|
|
|
16
|
my $access = $args{access} || 'vendor'; |
|
49
|
2
|
|
|
|
|
5
|
my $scope = $args{scope}; |
|
50
|
2
|
|
|
|
|
4
|
my $params = {}; |
|
51
|
2
|
50
|
|
|
|
17
|
if ($access eq 'affiliate') { |
|
|
|
50
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
0
|
$params->{vendor_id} = $args{vendor}; |
|
53
|
|
|
|
|
|
|
} elsif ($access ne 'vendor') { |
|
54
|
0
|
|
|
|
|
0
|
$params->{grant_type} = 'client_credentials'; |
|
55
|
0
|
|
|
|
|
0
|
$params->{scope} = $scope; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
2
|
|
|
|
|
9
|
my $path = join("/",$prefix, $access, 'token'); |
|
58
|
2
|
|
|
|
|
21
|
$path =~ s#/v\d+##; |
|
59
|
2
|
50
|
|
|
|
21
|
$args{token} = { |
|
60
|
|
|
|
|
|
|
id => 0, |
|
61
|
|
|
|
|
|
|
expires => 0, |
|
62
|
|
|
|
|
|
|
access => $access, |
|
63
|
|
|
|
|
|
|
scope => $scope, |
|
64
|
|
|
|
|
|
|
vendor => $access eq 'affiliate' ? $args{vendor} : '', |
|
65
|
|
|
|
|
|
|
}; |
|
66
|
2
|
|
50
|
|
|
49
|
$args{client} = Net::MyCommerce::API::Client->new( |
|
|
|
|
50
|
|
|
|
|
|
67
|
|
|
|
|
|
|
host => $args{host} || 'https://api.mycommerce.com', |
|
68
|
|
|
|
|
|
|
path => $path, |
|
69
|
|
|
|
|
|
|
credentials => $args{credentials} || {}, |
|
70
|
|
|
|
|
|
|
method => 'POST', |
|
71
|
|
|
|
|
|
|
params => $params, |
|
72
|
|
|
|
|
|
|
timeout => $args{timeout}, |
|
73
|
|
|
|
|
|
|
getJSON => 1, |
|
74
|
|
|
|
|
|
|
sendJSON => 0, |
|
75
|
|
|
|
|
|
|
); |
|
76
|
2
|
|
|
|
|
19
|
return bless { %args }, $pkg; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 lookup () |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Use cached token if not yet expired; otherwise request new token from token service |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub lookup { |
|
86
|
2
|
|
|
2
|
1
|
9
|
my ($self) = @_; |
|
87
|
2
|
50
|
33
|
|
|
16
|
if ($self->{token}{id} && $self->{token}{expires} > time() + 60) { |
|
88
|
0
|
|
|
|
|
0
|
return $self->_token_cache(); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
2
|
|
|
|
|
13
|
my ($error, $result) = $self->{client}->request(); |
|
91
|
2
|
50
|
|
|
|
14
|
if ($error) { |
|
|
|
0
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
14
|
return $self->_token_error($error); |
|
93
|
|
|
|
|
|
|
} elsif ($result->{content}{access_token}) { |
|
94
|
0
|
|
|
|
|
0
|
$self->{token}{id} = $result->{content}{access_token}; |
|
95
|
0
|
|
|
|
|
0
|
$self->{token}{expires} = $result->{content}{expires_in} + time(); |
|
96
|
0
|
|
|
|
|
0
|
return $self->_token_new(); |
|
97
|
|
|
|
|
|
|
} else { |
|
98
|
0
|
|
|
|
|
0
|
return _token_error('missing token'); |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 reset () |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
clear cached token; necessary if resource API returns an 'invalid_token' error |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=cut |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub reset { |
|
109
|
1
|
|
|
1
|
1
|
5
|
my ($self, %args) = @_; |
|
110
|
1
|
|
|
|
|
4
|
$self->{token}{id} = 0; |
|
111
|
1
|
|
|
|
|
4
|
$self->{token}{expires} = 0; |
|
112
|
1
|
|
|
|
|
5
|
return (''); |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Private Methods |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _token_cache { |
|
118
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
|
119
|
0
|
|
|
|
|
0
|
return ('',$self->{token}); |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _token_new { |
|
123
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
|
124
|
0
|
|
|
|
|
0
|
return ('',$self->{token}); |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
sub _token_error { |
|
128
|
2
|
|
|
2
|
|
6
|
my ($self, $error) = @_; |
|
129
|
2
|
|
50
|
|
|
10
|
$error ||= 'unknown error'; |
|
130
|
2
|
|
|
|
|
19
|
return ($error); |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |