o
    Q?h
                     @   sr   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efdd	Zdefd
dZdefddZdS )    N)OAuth2Session)SERVER_TIMESTAMP)getAppCredentials)	API_Error)saveShopparamsc                 C   s   |  d}| d| d|  d}}}td| }|r@| d}d| d}t| t||d	}|d
\}	}
t|
|	dS tddd)NcurrentUseruidenterpriseIdhostname8apiKeyzhttps://z/shops/redirects/bigcartel)redirect_uriz(https://my.bigcartel.com/oauth/authorize)stateurlNo app credentials found  messagestatus_code)getr   printr   authorization_urldictr   )r   r   r	   r
   r   appclientIdredirectUrioauthr   r    r   M/var/www/html/riverr-enterprise-integrations-main/functions/Bigcartel/Auth.pyauthUrl
   s   
"
r    c                 C   s6  |  d}| d| d|  d}}}td|d}|r| d}| d}d	}tj|t|||  d
dd}	|	jdvrFt|	j|	jd|	 }
|
 d}tj d| dt	|
 dd}|jdv r|  di  d}t
||dd| d| d||
 d|tt| dd}|S t|j|jdtddd)Nr   r	   r
   r   r   )
platformIdr
   r   	apiSecretz%https://api.bigcartel.com/oauth/tokencode)	client_idclient_secretr#   )data)      r   
account_idz&https://api.bigcartel.com/v1/accounts/z.jsonaccess_token)headersr&   
attributes	Bigcartelid
store_namer   )r	   r
   r!   platformNameappIdnameplatformShopIdaccessToken	accountId	createdAt	updatedAtr   )r   r   r   )r   r   requestspostr   r   r   textjsongetAuthr   r   )r   r   r	   r
   r   r   r   clientSecretr   tokenr&   r5   
accountResaccountshopr   r   r   	authToken   s@   
"



 
rB   r*   c                 C   s   d|  dddS )NzBearer zapplication/vnd.api+json)AuthorizationAcceptzContent-Typer   )r*   r   r   r   r<   9   s   r<   )r8   requests_oauthlibr   configs.firebaser   functions.Applicationsr   functions.Responser   functions.Shopsr   r   r    rB   strr<   r   r   r   r   <module>   s    "