U
    #i                     @   s^   d dl mZmZmZmZmZ d dlmZmZ ddl	m
Z
mZ ddlmZmZ G dd dZdS )	    )AnyDictLiteralOptionalUnion)	HTTPErrorResponse   )FunctionsHttpErrorFunctionsRelayError)
SyncClient__version__c                   @   s   e Zd ZdeeeedddZded ee	eeef df e
eeef  eddd	Zedd
ddZdee
e e	eef dddZdS )SyncFunctionsClientT)urlheaderstimeoutverifyc                 C   s<   || _ ddt i|| _t| j | jt||ddd| _d S )Nz
User-Agentzsupabase-py/functions-py vT)base_urlr   r   r   Zfollow_redirectsZhttp2)r   r   r   r   bool_client)selfr   r   r   r    r   C/tmp/pip-unpacked-wheel-snf83r8y/supafunc/_sync/functions_client.py__init__
   s     zSyncFunctionsClient.__init__N)GETOPTIONSHEADPOSTPUTPATCHDELETE)methodr   r   jsonreturnc              
   C   sl   | j j||||d}z|  W nF tk
rf } z(t| dpPd|jjd|W 5 d }~X Y nX |S )N)r"   r   errorz9An error occurred while requesting your edge function at .)r   requestraise_for_statusr   r
   r"   getr   )r   r!   r   r   r"   responseexcr   r   r   _request   s    zSyncFunctionsClient._request)tokenr#   c                 C   s   d| | j d< dS )zUpdates the authorization header

        Parameters
        ----------
        token : str
            the new jwt token sent in the authorization header
        zBearer AuthorizationN)r   )r   r,   r   r   r   set_auth+   s    	zSyncFunctionsClient.set_auth)function_nameinvoke_optionsr#   c           
      C   s   | j }d}d}|dk	r||di  |dd}|d}|rdt|trd|dkrd|  |d< |d}t|trd|d	< nt|trd
|d	< | jd| j	 d| ||d}|j d}|r|dkrt
| d|dkr| }	n|j}	|	S )a  Invokes a function

        Parameters
        ----------
        function_name : the name of the function to invoke
        invoke_options : object with the following properties
            `headers`: object representing the headers to send with the request
            `body`: the body of the request
            `responseType`: how the response should be parsed. The default is `json`
        Nz
text/plainr   ZresponseTyperegionanyzx-regionbodyzContent-Typezapplication/jsonr   /)r   r"   zx-relay-headertruer$   r"   )r   updater(   
isinstancestrlowerstripdictr+   r   r   r"   content)
r   r/   r0   r   r3   Zresponse_typer1   r)   Zis_relay_errordatar   r   r   invoke6   s6    




   
zSyncFunctionsClient.invoke)T)NN)N)__name__
__module____qualname__r8   r   intr   r   r   r   r   r   r   r+   r.   bytesr>   r   r   r   r   r   	   s"       
r   N)typingr   r   r   r   r   Zhttpxr   r   errorsr
   r   utilsr   r   r   r   r   r   r   <module>   s   