Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	sp_api/__version__.py
#	sp_api/auth/access_token_client.py
  • Loading branch information
saleweaver committed Dec 14, 2024
2 parents 2fa058f + c884ea4 commit 5b8bbd2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 41 deletions.
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
## v1.8.21 - 2024-12-14
## v1.8.21

### Changes Relevant to End-Users
- **Configuration Enhancement**: Made the TTL (Time-To-Live) for authentication cache configurable via the `SP_API_AUTH_CACHE_TTL` environment variable. This allows users to customize the cache duration according to their needs, providing greater flexibility in managing token lifetimes.

### Internal Changes
- No internal changes were made in this version.

### Mermaid Diagram Representation

```mermaid
graph TD;
A[Authentication Cache] -->|TTL Configurable| B[Environment Variable];
```

These changes allow users to better manage their authentication cache settings, enhancing the adaptability of the library to different operational requirements.

## v1.8.20 - 2024-12-14
## v1.8.20

### Internal Changes
- Enhanced the changelog generation script to include mermaid diagrams for visual representation of changes.
- Increased the `max_tokens` parameter for OpenAI API calls from 500 to 750 to allow for more detailed changelog entries.

### Mermaid Diagram Representation

```mermaid
graph TD;
A[Changelog Generation Script] -->|Updated| B[Include Mermaid Diagrams];
A -->|Increased| C[Max Tokens for API Calls];
```

These changes improve the clarity and detail of the changelog entries, providing better insights into updates and modifications.

## v1.8.19 - 2024-12-14
## v1.8.19

Expand Down
2 changes: 1 addition & 1 deletion sp_api/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.8.19"
__version__ = '1.8.21'
77 changes: 37 additions & 40 deletions sp_api/auth/access_token_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@
from .access_token_response import AccessTokenResponse
from .exceptions import AuthorizationError

cache = TTLCache(maxsize=int(os.environ.get("SP_API_AUTH_CACHE_SIZE", 10)), ttl=3200)
grantless_cache = TTLCache(
maxsize=int(os.environ.get("SP_API_AUTH_CACHE_SIZE", 10)), ttl=3200
)
cache = TTLCache(maxsize=int(os.environ.get('SP_API_AUTH_CACHE_SIZE', 10)), ttl=int(os.environ.get('SP_API_AUTH_CACHE_TTL', 3200)))
grantless_cache = TTLCache(maxsize=int(os.environ.get('SP_API_AUTH_CACHE_SIZE', 10)), ttl=int(os.environ.get('SP_API_AUTH_CACHE_TTL', 3200)))

logger = logging.getLogger(__name__)


class AccessTokenClient(BaseClient):
host = "api.amazon.com"
grant_type = "refresh_token"
path = "/auth/o2/token"
host = 'api.amazon.com'
grant_type = 'refresh_token'
path = '/auth/o2/token'

def __init__(self, refresh_token=None, credentials=None, proxies=None, verify=True):
self.cred = Credentials(refresh_token, credentials)
self.proxies = proxies
self.verify = verify

def _request(self, url, data, headers):
response = requests.post(
url, data=data, headers=headers, proxies=self.proxies, verify=self.verify
)
response = requests.post(url, data=data, headers=headers, proxies=self.proxies, verify=self.verify)
response_data = response.json()
if response.status_code != 200:
error_message = response_data.get("error_description")
error_code = response_data.get("error")
error_message = response_data.get('error_description')
error_code = response_data.get('error')
raise AuthorizationError(error_code, error_message, response.status_code)
return response_data

Expand All @@ -54,7 +50,7 @@ def get_auth(self) -> AccessTokenResponse:
cache[cache_key] = access_token
return AccessTokenResponse(**access_token)

def get_grantless_auth(self, scope="sellingpartnerapi::notifications"):
def get_grantless_auth(self, scope='sellingpartnerapi::notifications'):
"""
:param scope: One of allowed scope for grantless operations:
sellingpartnerapi::notifications or sellingpartnerapi::migration
Expand All @@ -73,13 +69,15 @@ def get_grantless_auth(self, scope="sellingpartnerapi::notifications"):
cache_key = self._get_cache_key(scope)
try:
access_token = grantless_cache[cache_key]
logger.debug("from_cache. scope: %s", scope)
logger.debug('from_cache. scope: %s', scope)
except KeyError:
request_url = self.scheme + self.host + self.path
access_token = self._request(
request_url, data=self.grantless_data(scope), headers=self.headers
request_url,
data=self.grantless_data(scope),
headers=self.headers
)
logger.debug("token_refreshed")
logger.debug('token_refreshed')
grantless_cache.clear()
grantless_cache[cache_key] = access_token

Expand All @@ -90,45 +88,44 @@ def authorize_auth_code(self, auth_code):
res = self._request(
request_url,
data=self._auth_code_request_body(auth_code),
headers=self.headers,
headers=self.headers
)
return res

def _auth_code_request_body(self, auth_code):
return {
"grant_type": "authorization_code",
"code": auth_code,
"client_id": self.cred.client_id,
"client_secret": self.cred.client_secret,
'grant_type': 'authorization_code',
'code': auth_code,
'client_id': self.cred.client_id,
'client_secret': self.cred.client_secret
}

def grantless_data(self, scope_value: str):
return {
"grant_type": "client_credentials",
"client_id": self.cred.client_id,
"scope": scope_value,
"client_secret": self.cred.client_secret,
'grant_type': 'client_credentials',
'client_id': self.cred.client_id,
'scope': scope_value,
'client_secret': self.cred.client_secret
}

@property
def data(self):
return {
"grant_type": self.grant_type,
"client_id": self.cred.client_id,
"refresh_token": self.cred.refresh_token,
"client_secret": self.cred.client_secret,
'grant_type': self.grant_type,
'client_id': self.cred.client_id,
'refresh_token': self.cred.refresh_token,
'client_secret': self.cred.client_secret
}

@property
def headers(self):
return {"User-Agent": self.user_agent, "content-type": self.content_type}

def _get_cache_key(self, token_flavor=""):
return (
"access_token_"
+ hashlib.md5(
(token_flavor + (self.cred.refresh_token or "__grantless__")).encode(
"utf-8"
)
).hexdigest()
)
return {
'User-Agent': self.user_agent,
'content-type': self.content_type
}

def _get_cache_key(self, token_flavor=''):
return 'access_token_' + hashlib.md5(
(token_flavor + (self.cred.refresh_token or '__grantless__')).encode('utf-8')
).hexdigest()

0 comments on commit 5b8bbd2

Please sign in to comment.