| 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 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Net::MyCommerce::API::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 4 | 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 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=23.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 2 | my ($pkg, %args) = @_; | |||||||||||||||||||||||||||
| 47 | 2 | ▶ | my $prefix = $args{prefix} || '/api'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'prefix'} || '/api'
MC/DC0%0/1
$args{'prefix'} || '/api'
$args{'prefix'}
Truth table50%1/2
A
A= $args{'prefix'}
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my $access = $args{access} || 'vendor'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'access'} || 'vendor'
MC/DC0%0/1
$args{'access'} || 'vendor'
$args{'access'}
Truth table50%1/2
A
A= $args{'access'}
| |||||||||||||||||||||||||||||
| 49 | 2 | my $scope = $args{scope}; | |||||||||||||||||||||||||||
| 50 | 2 | my $params = {}; | |||||||||||||||||||||||||||
| 51 | 2 | ▶ | if ($access eq 'affiliate') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 0 | $params->{vendor_id} = $args{vendor}; | |||||||||||||||||||||||||||
| 53 | } elsif ($access ne 'vendor') { | ||||||||||||||||||||||||||||
| 54 | 0 | $params->{grant_type} = 'client_credentials'; | |||||||||||||||||||||||||||
| 55 | 0 | $params->{scope} = $scope; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 2 | my $path = join("/",$prefix, $access, 'token'); | |||||||||||||||||||||||||||
| 58 | 2 | $path =~ s#/v\d+##; | |||||||||||||||||||||||||||
| 59 | $args{token} = { | ||||||||||||||||||||||||||||
| 60 | id => 0, | ||||||||||||||||||||||||||||
| 61 | expires => 0, | ||||||||||||||||||||||||||||
| 62 | access => $access, | ||||||||||||||||||||||||||||
| 63 | scope => $scope, | ||||||||||||||||||||||||||||
| 64 | 2 | ▶ | vendor => $access eq 'affiliate' ? $args{vendor} : '', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | }; | ||||||||||||||||||||||||||||
| 66 | $args{client} = Net::MyCommerce::API::Client->new( | ||||||||||||||||||||||||||||
| 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 | 2 | ▶ | getJSON => 1, | ||||||||||||||||||||||||||
|
Condition50%2/4
$args{'host'} || 'https://api.mycommerce.com'
$args{'credentials'} || {}
MC/DC0%0/1
$args{'host'} || 'https://api.mycommerce.com'
$args{'host'}
MC/DC0%0/1
$args{'credentials'} || {}
$args{'credentials'}
Truth table50%1/2
A
A= $args{'host'}
Truth table50%1/2
A
A= $args{'credentials'}
| |||||||||||||||||||||||||||||
| 74 | sendJSON => 0, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | 2 | 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 | 2 | ▶ | sub lookup { | ||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=5 SCAR=22.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ($self->{token}{id} && $self->{token}{expires} > time() + 60) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"token"}{"id"} and $self->{"token"}{"expires"} > time + 60
MC/DC0%0/2
$self->{"token"}{"id"} and $self->{"token"}{"expires"} > time + 60
$self->{"token"}{"id"}
$self->{"token"}{"expires"} > time + 60
Truth table33%1/3
A and B
A= $self->{"token"}{"id"}B= $self->{"token"}{"expires"} > time + 60
| |||||||||||||||||||||||||||||
| 88 | 0 | return $self->_token_cache(); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 2 | my ($error, $result) = $self->{client}->request(); | |||||||||||||||||||||||||||
| 91 | 2 | ▶ | if ($error) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | 2 | return $self->_token_error($error); | |||||||||||||||||||||||||||
| 93 | } elsif ($result->{content}{access_token}) { | ||||||||||||||||||||||||||||
| 94 | 0 | $self->{token}{id} = $result->{content}{access_token}; | |||||||||||||||||||||||||||
| 95 | 0 | $self->{token}{expires} = $result->{content}{expires_in} + time(); | |||||||||||||||||||||||||||
| 96 | 0 | return $self->_token_new(); | |||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 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 | 1 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 1 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 110 | 1 | $self->{token}{id} = 0; | |||||||||||||||||||||||||||
| 111 | 1 | $self->{token}{expires} = 0; | |||||||||||||||||||||||||||
| 112 | 1 | return (''); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Private Methods | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _token_cache { | ||||||||||||||||||||||||||
|
Subroutine
_token_cache: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | return ('',$self->{token}); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _token_new { | ||||||||||||||||||||||||||
|
Subroutine
_token_new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | return ('',$self->{token}); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | sub _token_error { | ||||||||||||||||||||||||||
|
Subroutine
_token_error: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 128 | 2 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | $error ||= 'unknown error'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$error ||= "unknown error"
MC/DC0%0/1
$error ||= "unknown error"
$error
Truth table50%1/2
A
A= $error
| |||||||||||||||||||||||||||||
| 130 | 2 | return ($error); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | 1; | |||||||||||||||||||||||||||