
    hi                        d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
Z
d dlZddlmZ  G d dej                        Z ed	      	 	 	 	 	 	 	 	 	 	 dd
       Zy)    )annotationsN)Any)	GeneratorAsyncGeneratoroverride   )	lru_cachec                      e Zd ZU dZdZded<   	 	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Z	edd       Z
dd	Zdd
ZddZy)OAuth2ClientCredentialsz-OAuth2 client credentials authentication flowTboolrequires_response_bodyc                   || _         || _        || _        || _        d | _        d | _        t        j                         | _        t        j                         | _
        y N)
_token_url_header
_client_id_client_secret_token_token_expires_atanyioLock_token_async_lock	threading_token_sync_lock)self	token_url	client_idclient_secretheaders        Q/mnt/e/genesis-system/.venv_telnyx/lib/python3.12/site-packages/telnyx/_oauth2.py__init__z OAuth2ClientCredentials.__init__   sM     $#+-1/3!& ) 0    c              #  b  K   | j                   5  | j                  rG| j                         s7d| j                  d    |j                  | j                  <   | 	 d d d        y | j                         }|j                  dk(  r|j                          |j                         | _        | j                  J t        j                         | j                  j                  dd      z   | _        d| j                  d    |j                  | j                  <   | d d d        y # 1 sw Y   y xY wwNzBearer access_token   
expires_ini  )r   r   token_is_expiredheadersr   _build_token_requeststatus_codereadjsontimegetr   r   requesttoken_responses      r    sync_auth_flowz&OAuth2ClientCredentials.sync_auth_flow'   s    "" 	{{4#8#8#:29$++n:U9V0W-		 	 $(#<#<#>>N))S0##%,113{{...)-t{{|UY7Z)Z&29$++n:U9V0W-M	 	 	s*   D/AD#	D/#B7D#	D/#D,(D/c                $    | j                  |      S r   )r3   )r   r1   s     r    	auth_flowz!OAuth2ClientCredentials.auth_flow:   s    ""7++r"   c                 K   | j                   4 d {    | j                  P| j                         s@d| j                  d    |j                  | j                  <   | 	 d d d       d {    y | j                         }|j                  dk(  r|j                          d {    |j                         | _        | j                  J t        j                         | j                  j                  dd      z   | _        d| j                  d    |j                  | j                  <   | d d d       d {    y 7 37 7 7 # 1 d {  7  sw Y   y xY wwr$   )r   r   r(   r)   r   r*   r+   areadr-   r.   r/   r   r0   s      r    async_auth_flowz'OAuth2ClientCredentials.async_auth_flow>   s7    )) 	 	{{&t/D/D/F29$++n:U9V0W-		 	 	 $(#<#<#>>N))S0$**,,,,113{{...)-t{{|UY7Z)Z&29$++n:U9V0W-M	 	 	 	 -	 	 	 	sv   E$EE$AE#E$.E	/E$45E)E*BE5E$ EE$	E$EE$E!EE!E$c                    dd l }| j                   d| j                   }|j                  |j	                               j                         }t        j                  d| j                  ddd| d      S )	Nr   :POSTs   grant_type=client_credentialsz!application/x-www-form-urlencodedzBasic )zContent-TypeAuthorization)contentr)   )	base64r   r   	b64encodeencodedecodehttpxRequestr   )r   r>   credentialsencoded_credentialss       r    r*   z,OAuth2ClientCredentials._build_token_requestQ   sv    )4+>+>*?@$..{/A/A/CDKKM}}OO4 C#)*=)>!?	
 	
r"   c                |    | j                   | j                  yt        j                         | j                   dz
  kD  S )NT
   )r   r   r.   r   s    r    r(   z(OAuth2ClientCredentials.token_is_expireda   s6    !!)T[[-@yy{T33b888r"   c                     d| _         d| _        y)zBInvalidate the cached token, forcing a refresh on the next requestN)r   r   rH   s    r    invalidate_tokenz(OAuth2ClientCredentials.invalidate_tokeng   s    !%r"   N)
r   	httpx.URLr   strr   rL   r   rL   returnNone)r1   httpx.RequestrM   z.Generator[httpx.Request, httpx.Response, None])r1   rO   rM   z-AsyncGenerator[httpx.Request, httpx.Response])rM   rO   )rM   r   )rM   rN   )__name__
__module____qualname____doc__r   __annotations__r!   r   r3   r5   r8   r*   r(   rJ    r"   r    r   r      s    7#'D'1 1 	1
 1 1 
1$  $ , ,  $
 9&r"   r   )maxsizec                     t        | |||      S )Nr   r   r   r   )r   rX   s       r    make_oauth2rY   m   s     ##	 r"   )
r   rK   r   rL   r   rL   r   rL   rM   r   )
__future__r   r.   r   typingr   typing_extensionsr   r   r   r   rB   _utilsr	   Authr   rY   rU   r"   r    <module>r_      s|    #    A A   Z&ejj Z&z 4  	
  r"   