o
    i'                     @  sF  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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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 ddlmZ ddlmZ d#ddZG dd deZeddejiZ eddej!iZ"ed d!ej#iZ$ed"dej%iZ&dS )$    )annotations)Any)cast)List)OptionalN)	TypedDict   )Client)InArgs)InStatementLibsqlError)Transaction)_Config)proto)_batch_results_from_proto)_batch_to_proto)_result_set_from_proto)_stmt_to_proto)	ResultSetconfigr   return
HttpClientc                 C  sn   | j dv sJ | j dkr| jrtdd| j dkr!| js!tddtj| j | j| jdddf}t|| j	dS )	N)httphttpsr   z1A 'http:' URL cannot opt into TLS by using ?tls=1URL_INVALIDr   z4A 'https:' URL cannot opt out of TLS by using ?tls=0 
auth_token)
schemetlsr   urllibparse
urlunparse	authoritypathr   r   )r   url r'   F/home/ubuntu/.local/lib/python3.10/site-packages/libsql_client/http.py_create_http_client   s&   
r)   c                   @  sz   e Zd ZU ded< ded< ddd*d
dZd+d,ddZd-ddZd.ddZd/ddZd0dd Z	e
d1d"d#Zd2d(d)ZdS )3r   zaiohttp.ClientSession_sessionstr_urlNr   r&   r   Optional[str]c                C  s&   dd| i}t j|d| _|| _d S )NauthorizationzBearer )headers)aiohttpClientSessionr*   r,   )selfr&   r   r/   r'   r'   r(   __init__6   s   
zHttpClient.__init__stmtr   argsr
   r   r   c                   s:   dt ||i}| dd|I d H }tt|d }t|S )Nr4   POSTz
v1/executeresult)r   _sendr   _ExecuteRespr   )r2   r4   r5   requestresponse	proto_resr'   r'   r(   execute;   s   
zHttpClient.executeNonec                   s   t dd)Nz*The HTTP client does not support sequence.SEQUENCE_NOT_SUPPORTEDr   )r2   r4   r'   r'   r(   sequenceC   s
   zHttpClient.sequencestmtsList[InStatement]List[ResultSet]c                   s>   dt |i}| dd|I d H }tt|d }t|t|S )Nbatchr6   zv1/batchr7   )r   r8   r   
_BatchRespr   len)r2   rA   r:   r;   r<   r'   r'   r(   rD   I   s   zHttpClient.batchr   c                 C  s
   t dd)NzThe HTTP client does not support transactions. Please use a libsql:, ws: or wss: URL, so that the client connects using a WebSocket.TRANSACTIONS_NOT_SUPPORTEDr   r2   r'   r'   r(   transactionQ   s   zHttpClient.transactionc                   s   | j  I d H  d S N)r*   closerH   r'   r'   r(   rK   Y   s   zHttpClient.closeboolc                 C  s   | j jS rJ   )r*   closedrH   r'   r'   r(   rM   \   s   zHttpClient.closedmethodr%   request_bodyr   c           
   	     s   t j| j|}| jj|||d4 I d H W}|js\|jdkr;| I d H }d|v r:|d }|	dp4d}t
||n|jdkrS| I d H }	t
d|j d|	d	t
d|j d	| I d H W  d   I d H  S 1 I d H ssw   Y  d S )
N)jsonzapplication/jsonmessagecodeUNKNOWNz
text/plainzServer returned HTTP status z and error: SERVER_ERROR)r!   r"   urljoinr,   r*   r:   okcontent_typerP   getr   textstatus)
r2   rN   r%   rO   r&   resp	resp_jsonrQ   rR   	resp_textr'   r'   r(   r8   `   s4   


0zHttpClient._send)r&   r+   r   r-   rJ   )r4   r   r5   r
   r   r   )r4   r+   r   r>   )rA   rB   r   rC   )r   r   )r   r>   )r   rL   )rN   r+   r%   r+   rO   r   r   r   )__name__
__module____qualname____annotations__r3   r=   r@   rD   rI   rK   propertyrM   r8   r'   r'   r'   r(   r   2   s   
 



_ExecuteReqr4   r9   r7   	_BatchReqrD   rE   )r   r   r   r   )'
__future__r   typingr   r   r   r   urllib.parser!   r0   typing_extensionsr   clientr	   r
   r   r   r   r   r   hranar   hrana.convertr   r   r   r   r7   r   r)   r   Stmtrc   
StmtResultr9   Batchrd   BatchResultrE   r'   r'   r'   r(   <module>   sT    
F