o
    tBh@                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	Z	d dl
mZ ddlmZ ddlmZ ddlmZ ed	d
daed	d
daeeZG dd deZdS )    N)ClientError)TTLCache)
BaseClient   )Credentials)AccessTokenResponse)AuthorizationError
     maxsizettlc                   @   s   e Zd ZdZdZdZdddZdd Zd	efd
dZ	dddZ
dd Zdd ZdefddZedd Zedd ZdddZdS ) AccessTokenClientzapi.amazon.comrefresh_tokenz/auth/o2/tokenNc                 C   s   t ||| _d S )N)r   cred)selfr   credentials r   v/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sp_api/auth/access_token_client.py__init__   s   zAccessTokenClient.__init__c                 C   sH   t j|||d}| }|jdkr"|d}|d}t|||j|S )Ndataheaders   error_descriptionerror)requestspostjsonstatus_codegetr   )r   urlr   r   responseresponse_dataerror_message
error_coder   r   r   _request   s   


zAccessTokenClient._requestreturnc                 C   s   |   }zt| }W n3 ty=   d}d}|s*| j| j | j }| || j| j}n|	d}t
d|d da|t|< Y nw tdi |S )zL
        Get's the access token
        :return:AccessTokenResponse
        r
   N
expires_inr	      r   r   )_get_cache_keycacheKeyErrorschemehostpathr&   r   r   r    r   r   )r   	cache_keyaccess_token	cache_ttlrequest_urlr   r   r   get_auth'   s   

zAccessTokenClient.get_auth sellingpartnerapi::notificationsc                 C   s   |  |}zt| }td| W n+ ty<   | j| j | j }| j|| 	|| j
d}td t  |t|< Y nw tdi |S )a  
        :param scope: One of allowed scope for grantless operations:
            sellingpartnerapi::notifications or sellingpartnerapi::migration
            See: https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/developer-guide/SellingPartnerApiDeveloperGuide.md#step-3-configure-your-lwa-credentials

        POST /auth/o2/token HTTP/l.l
        Host: api.amazon.com
        Content-Type: application/x-www-form-urlencoded;charset=UTF-8
        grant_type=client_credentials
        &scope=sellingpartnerapi::notifications
        &client_id=foodev
        &client_secret=Y76SDl2F
        :return: AccessTokenResponse
        zfrom_cache. scope: %sr   token_refreshedNr   )r*   grantless_cacheloggerdebugr,   r-   r.   r/   r&   grantless_datar   clearr   )r   scoper0   r1   r3   r   r   r   get_grantless_auth=   s    

z$AccessTokenClient.get_grantless_authc                 C   s.   | j | j | j }| j|| || jd}|S )Nr   )r-   r.   r/   r&   _auth_code_request_bodyr   )r   	auth_coder3   resr   r   r   authorize_auth_code^   s   z%AccessTokenClient.authorize_auth_codec                 C   s   d|| j j| j jdS )Nauthorization_code)
grant_typecode	client_idclient_secretr   rE   rF   )r   r?   r   r   r   r>   g   s
   z)AccessTokenClient._auth_code_request_bodyscope_valuec                 C   s   d| j j|| j jdS )Nclient_credentials)rC   rE   r<   rF   rG   )r   rH   r   r   r   r:   o   s
   z AccessTokenClient.grantless_datac                 C   s   | j | jj| jj| jjdS )N)rC   rE   r   rF   )rC   r   rE   r   rF   r   r   r   r   r   w   s
   zAccessTokenClient.datac                 C   s   | j | jdS )N)z
User-Agentzcontent-type)
user_agentcontent_typerJ   r   r   r   r      s   zAccessTokenClient.headers c                 C   s    dt || jj d  S )Naccess_token_zutf-8)hashlibmd5r   r   encode	hexdigest)r   token_flavorr   r   r   r*      s
   z AccessTokenClient._get_cache_key)NN)r5   )rM   )__name__
__module____qualname__r.   rC   r/   r   r&   r   r4   r=   rA   r>   strr:   propertyr   r   r*   r   r   r   r   r      s     
	
!	

r   )r   osr   botocore.exceptionsr   rO   logging
cachetoolsr   boto3sp_api.baser   r   r   access_token_responser   
exceptionsr   r+   r7   	getLoggerrT   r8   r   r   r   r   r   <module>   s     
