o
    Q?h                     @   s   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mZ d dlm	Z	 d dl
mZ d dlmZ dZd	efd
dZd	efddZG dd dZdS )    N)datetime)OAuth2Session)Application)Shop)	API_Error)Paramsak  https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.marketing.readonly https://api.ebay.com/oauth/api_scope/sell.marketing https://api.ebay.com/oauth/api_scope/sell.inventory.readonly https://api.ebay.com/oauth/api_scope/sell.inventory https://api.ebay.com/oauth/api_scope/sell.account.readonly https://api.ebay.com/oauth/api_scope/sell.account https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly https://api.ebay.com/oauth/api_scope/sell.fulfillment https://api.ebay.com/oauth/api_scope/sell.analytics.readonly https://api.ebay.com/oauth/api_scope/sell.finances https://api.ebay.com/oauth/api_scope/sell.payment.dispute https://api.ebay.com/oauth/api_scope/commerce.identity.readonly https://api.ebay.com/oauth/api_scope/commerce.notification.subscription https://api.ebay.com/oauth/api_scope/commerce.notification.subscription.readonlyparamsc                 C   sF   | j j}td| }t|jdtd}|jdddd\}}t||dS )	N9 Awear_LLC-AwearLLC-Riverr-pfeyxz)redirect_uriscopez&https://auth.ebay.com/oauth2/authorizeloginzen-US)urlpromptlocale)r   state)	currentUserenterpriseIdr   getr   apiKeyscopesauthorization_urldict)r   r   appoauthr   r    r   K/var/www/html/riverr-enterprise-integrations-main/V2/functions/Ebay/Auth.pyauthUrl   s
   r   c                 C   sv  | j d| j d| j d}}}| jj}td| }t|jdd}|j d|j }t	|
  }tjdt|d	dd
d| ddd}	|	jdv r|	 }
|
d}tjtjd t|
dd }|
d}|
d}|
|_ddlm} ||}|r|d}|d}t|||||||j|j|j|jtjjd d| jjd d}|j |dd |! S t"dd)Nr   code
expires_inr	   r
   )r   :-https://api.ebay.com/identity/v1/oauth2/tokenauthorization_code)r   
grant_typer   zBasic !application/x-www-form-urlencoded)AuthorizationContent-Type)dataheaders)      access_token)tzi   refresh_token
token_typer   )getEbayUseruserIdusernamedt)nameplatformShopIdaccessToken	tokenType	expiresAtrefreshTokenappIdr   
platformIdplatformNametimezoneuidr   registrationMarketplaceId)marketPlaceIdzShop not found.i  )#argsr   r   r   r   r   r   	apiSecretbase64	b64encodeencodedecoderequestspostr   status_codejsonr   nowpytzutc	timestampinttokenV2.functions.Ebay.Shopsr/   r   idr;   r<   tznamer>   saveto_dictr   )r   r   r   r   r   r   r   usrPassb64ValresrP   r6   r8   r9   r7   r/   shopr5   r4   newShopr   r   r   	authToken   s\   (

"




r[   c                   @   sV   e Zd ZdededdfddZdd Zi fd	ed
efddZ	i fd	edefddZ
dS )
EbayClientr   rY   returnNc                 C   sR   || _ || _dd|j i| _| jjr%| jjttj	 k r'| 
  d S d S d S )Nr%   zBearer )r   rY   r6   r(   r8   r   rK   rL   rM   rN   tokenRefresher)selfr   rY   r   r   r   __init__Q   s   $zEbayClient.__init__c                 C   s   t jdddi| jd| jjtdd}|jdv rP|d| j_|d	| j_	|d| j_t
|d
ttj  | j_tjjt d| j_| j  d S t|j|j)Nr!   r&   r$   r-   )r#   r-   r   )r   r(   r'   )r*   r)   r+   r.   r   r2   )rG   rH   r(   rY   r9   r   rI   r   r6   r7   rO   r   rK   rL   rM   rN   r8   rS   r=   updater   text)r_   rX   r   r   r   r^   Z   s(   
"
zEbayClient.tokenRefresherr   r   c                 C      t j||| jdS )N)r   r(   rG   r   r(   )r_   r   r   r   r   r   r   q      zEbayClient.getr'   c                 C   rc   )N)rJ   r(   rd   )r_   r   r'   r   r   r   rH   t   re   zEbayClient.post)__name__
__module____qualname__r   r   r`   r^   strr   r   rH   r   r   r   r   r\   P   s
    	r\   )rC   r   rL   rG   requests_oauthlibr   V2.functions.Applications.mainr   V2.functions.Shops.mainr   V2.middlewares.authr   	V2.Paramsr   r   r   r[   r\   r   r   r   r   <module>   s    :