
    ZThf                        U d Z ddlmZ ddl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mZ e	rddlmZ  G d d	e      Zg Zd
ed<    ed      ZddZy)zNUtility for generating synchronous API wrappers from asynchronous API classes.    )annotationsN)wraps)TYPE_CHECKINGAnyTypeVar)	TypedDict)ResourceBasec                  "    e Zd ZU ded<   ded<   y)SyncAPIRegistrationz	type[Any]	async_clssync_clsN)__name__
__module____qualname____annotations__     g/home/www/backend.miabetepe.com/venv/lib/python3.12/site-packages/sift_client/_internal/sync_wrapper.pyr   r      s    r   r   zlist[SyncAPIRegistration]_registeredSc                     j                   } j                  } j                  }t        |       fd       }d }|d| d j                  xs d |||d}d fd}d fd}	 j
                  j                         D ]  \  }}
|j                  d	      rt        |
t              ru|
j                  ri|
j                  }t        j                  |      }|}|r"t        t        |      |fd
              }|||<   n!t        t        |      |fd              }|||<   t        |
t              rt        d      t        |
t              rt        d      t        j                  |
      r ||      ||<    |	|      ||<    t!        |t"        f|      }|t$        j&                  v r"t$        j&                  |   j
                  }|||<   t(        j+                  t-         |             |S )aV  Generate a synchronous wrapper class for the given async API class.

    It creates a new class whose name is derived from the async class by
    stripping a trailing 'Async' (e.g. PingAPIAsync -> PingAPI). For each
    public coroutine method on the async class, it defines a sync method that
    invokes the async one on the default loop using run_coroutine_threadsafe.

    Usage:
        from sift_client._internal.sync_wrapper import generate_sync_api
        PingAPI = generate_sync_api(PingAPIAsync)

    Returns:
        A new class that wraps the async class with synchronous methods.
    c                      |i || _         y N)_async_impl)selfargskwargsclss      r   __init__z#generate_sync_api.<locals>.__init__2   s    //r   c                    | j                   j                  j                         }t        j                  ||      j                         S r   )r   clientget_asyncio_loopasynciorun_coroutine_threadsaferesult)r   coroloops      r   _runzgenerate_sync_api.<locals>._run6   s8    &&779//d;BBDDr   zSync counterpart to `z`.

 )r   __doc__r   r(   r   c                H     t               }t        |       fd       }|S )Nc                Z    | j                   t        | j                        |i |      S r   )r(   getattrr   r   akw	func_names      r   	sync_funcz8generate_sync_api.<locals>._make_sync.<locals>.sync_funcF   s,    99AWT%5%5yA1KKLLr   r-   r   )r1   
async_funcr2   r   s   `  r   
_make_syncz%generate_sync_api.<locals>._make_syncC   s0    S),
	z		M 
	M r   c                H     t               }t        |       fd       }|S )Nc                <     t        | j                        |i |S r   r-   r   r.   s      r   wrapped_funcz;generate_sync_api.<locals>._wrap_sync.<locals>.wrapped_funcO   s"    774++Y7AbAAr   r3   )r1   funcr9   r   s   `  r   
_wrap_syncz%generate_sync_api.<locals>._wrap_syncL   s/    sI&	t	B 
	B r   _c                P    t        | j                  |      }| j                  |      S r   )r-   r   r(   )r   
_prop_namer&   s      r   sync_prop_wrapperz,generate_sync_api.<locals>.sync_prop_wrapperc   s$     #4#3#3Z@D99T?*r   c                .    t        | j                  |      S r   r8   )r   r>   s     r   	sync_propz$generate_sync_api.<locals>.sync_propn   s     #4#3#3Z@@r   z*staticmethod is not supported sync_wrapperz-classmethod is not supported for sync_wrapper)r   r   )r1   str)r   r   r   r   r*   __dict__items
startswith
isinstancepropertyfgetinspectiscoroutinefunctionstaticmethodNotImplementedErrorclassmethodtypeobjectsysmodulesr   appendr   )r   	sync_namenamemodule	orig_initr   r(   	namespacer5   r;   attrr:   is_async_prop	prop_namer?   rA   
sync_classmodule_globalss   `                 r   generate_sync_apir]      s     <<D^^FI 90 0E
 *4&s{{7Hb6IJ!I ll((* 2+
d??3 dH%$))99D#77=M It7@ +  +
 #4	$ t/8 A  A #,	$ dL)%&RSS dK(%&UVV &&t,(.IdO$T*	$e2+j i&I6J V,55$.y!*S:NOr   )r   ztype[ResourceBase]rS   rB   returnrN   )r*   
__future__r   r#   rI   rP   	functoolsr   typingr   r   r   typing_extensionsr   sift_client.resources._baser	   r   r   r   r   r]   r   r   r   <module>rd      sN    T "   
  . . '8) 
 *,& +CLxr   