U
    >iZ                     @   s`   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 dl
mZmZ G dd deZd	S )
    )
HTTPStatus)HTTPBasicAuth)	parse_qsl)	urlencode)get_adapter)OAuth2ClientOAuth2Errorc                   @   s   e Zd Zdd ZdddZdS )NotionOAuth2Clientc                 C   s@   | j t|}| j|ddd}| jr0| j|d< d|t|f S )Ncodeuser)Z	client_idscopeZresponse_typeownerstatez%s?%s)Zscope_delimiterjoinsetconsumer_keyr   r   )selfZauthorization_urlr   Zextra_paramsparams r   Q/tmp/pip-unpacked-wheel-upujnpc2/allauth/socialaccount/providers/notion/client.pyget_redirect_url   s    
z#NotionOAuth2Client.get_redirect_urlNc                 C   s   t   j| j| jt| j| j|dd| jd}d }|j	t
jt
jfkrvz| }W n" tk
rt   tt|j}Y nX |rd|krtd|j |S )NZauthorization_code)r
   Z
grant_type)authjsonheadersaccess_tokenz!Error retrieving access token: %s)r   Zget_requests_sessionrequestZaccess_token_methodZaccess_token_urlr   r   Zconsumer_secretr   status_coder   OKCREATEDr   
ValueErrordictr   textr   content)r   r
   Zpkce_code_verifierrespr   r   r   r   get_access_token   s"    
z#NotionOAuth2Client.get_access_token)N)__name__
__module____qualname__r   r$   r   r   r   r   r	      s   r	   N)httpr   Zrequests.authr   urllib.parser   Zdjango.utils.httpr   Zallauth.socialaccount.adapterr   Z-allauth.socialaccount.providers.oauth2.clientr   r   r	   r   r   r   r   <module>   s   