
    i#5                        S SK Jr  S SKrS SKrS SKJrJrJrJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJrJrJrJrJrJrJr  S SKJrJr  SS	 jr " S
 S5      r S     SS jjrg)    )annotationsN)AnyAsyncGeneratorAsyncIteratoroverload)asynccontextmanager)
StartEventEvent)Context)HandlerDataHandlersListResponseHealthResponseSendEventResponseStatusWorkflowsListResponseCancelHandlerResponse)EventEnvelopeEventEnvelopeWithMetadatac                    U R                  5         g! [        R                   a  nSUR                  R                  s=::  a  S:  a  O  e UR                  R
                  SS nUR                  R                  nUR                  R                  nUR                  R                  n[        R                  " U SUR                  R                   SU SU SU 3	UR                  UR                  S9Uee SnAff = f)	zk
Raise an HTTPStatusError with the first 200 characters of the response body
for 400 and 500 level errors.
i  iX  N    z for z. Response: )requestresponse)
raise_for_statushttpxHTTPStatusErrorr   status_codetextr   methodurlreason_phrase)r   ebody_previewr   r    r   s         Q/home/james-whalen/.local/lib/python3.13/site-packages/workflows/client/client.py_raise_for_status_with_bodyr%   !   s    
!!#   !**((.3. 	 ::??4C0LYY%%F))--C**00K''-q!9!9 :%xq\ZfYgh		 	
 	s    C9CC44C9c                  V   \ rS rSr\SS j5       r\  SS j5       rSSS.   SS jjr\SS j5       rSS jrSS	 jr	   S         SS
 jjr
   S         SS jjr  S       SS jjr S       SS jjrSS jr  S      S!S jjrSS jr S"     S#S jjrSrg)$WorkflowClient6   c                   g N )selfhttpx_clients     r$   __init__WorkflowClient.__init__7   s    <?    c                   g r*   r+   )r,   base_urls     r$   r.   r/   9   s    
 r0   N)r-   r2   c               `    Uc  Uc  [        S5      eUb  Ub  [        S5      eXl        X l        g )Nz0Either httpx_client or base_url must be providedz5Only one of httpx_client or base_url must be provided)
ValueErrorr-   r2   )r,   r-   r2   s      r$   r.   r/   @   s=     H$4OPP#(<TUU( r0   c                 #    U R                   (       a  U R                   7v   g [        R                  " U R                  =(       d    SS9 IS h  vN nU7v   S S S 5      IS h  vN   g  N N! , IS h  vN  (       d  f       g = f7f)N )r2   )r-   r   AsyncClientr2   )r,   clients     r$   _get_clientWorkflowClient._get_clientM   sP     ###(($--2E2FF& GFFFFFsH   ABA*BA.B$A,%B,B.B4A75BBc                .  #    U R                  5        ISh  vN nUR                  S5      I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $  N] NF N	! , ISh  vN  (       d  f       g= f7f)zu
Check whether the workflow server is helathy or not

Returns:
    HealthResponse: health response from the workflow
Nz/health)r9   getr%   r   model_validatejsonr,   r8   r   s      r$   
is_healthyWorkflowClient.is_healthyU   sa      ##%%#ZZ	22H'1!00A &%%2 &%%%T   BA5BA;A72A;#B/A90B7A;9B;BBBBc                .  #    U R                  5        ISh  vN nUR                  S5      I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $  N] NF N	! , ISh  vN  (       d  f       g= f7f)zj
List workflows

Returns:
    WorkflowsListResponse: List of workflow names available through the server.
Nz
/workflows)r9   r<   r%   r   r=   r>   r?   s      r$   list_workflowsWorkflowClient.list_workflowsa   sa      ##%%#ZZ55H'1(77H &%%5 &%%%rB   c                `  #    Ub   [        USS9n[        U[        5      (       a   UR                  5       nU=(       d    SU=(       d    0 S.nU(       a  X&S'   U R                  5        ISh  vN nUR                  S	U S
3US9I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $ ! [         a  n[        SU 35      eSnAff = f! [         a  n[        SU 35      eSnAff = f N N NI! , ISh  vN  (       d  f       g= f7f)a,  
Run the workflow and wait until completion.

Args:
    start_event (Union[StartEvent, dict[str, Any], None]): start event class or dictionary representation (optional, defaults to None and get passed as an empty dictionary if not provided).
    context: Context or serialized representation of it (optional, defaults to None if not provided)
    handler_id (Optional[str]): Workflow handler identifier to continue from a previous completed run.

Returns:
    HandlerData: Data representing the handler running the workflow (including result and metadata)
NT)bare4Impossible to serialize the start event because of: 0Impossible to serialize the context because of: r6   start_eventcontext
handler_id/workflows/z/runr>   )_serialize_event	Exceptionr4   
isinstancer   to_dictr9   postr%   r   r=   r>   	r,   workflow_namerM   rK   rL   r"   request_bodyr8   r   s	            r$   run_workflowWorkflowClient.run_workflowo   s-    $ ".{F
 gw''Y!//+ ',"}"
 )3&##%%#[[m_D1 )  H (1--hmmo> &%%   J1#N   Y #STUSV!WXXY & &%%%s   D.
C D.C. 6D.-D.D.1D	D
2D<D.D	D.
C+C&&C++D..
D8DDD.DD.D+DD+'D.c                  #    Ub   [        U5      n[        U[        5      (       a   UR                  5       nU=(       d    [        [        5       5      U=(       d    0 S.nU(       a  X&S'   U R                  5        ISh  vN nUR                  SU S3US9I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $ ! [         a  n[        SU 35      eSnAff = f! [         a  n[        SU 35      eSnAff = f N N NI! , ISh  vN  (       d  f       g= f7f)	a  
Run the workflow in the background.

Args:
    start_event (Union[StartEvent, dict[str, Any], None]): start event class or dictionary representation (optional, defaults to None and get passed as an empty dictionary if not provided).
    context: Context or serialized representation of it (optional, defaults to None if not provided)
    handler_id (Optional[str]): Workflow handler identifier to continue from a previous completed run.

Returns:
    HandlerData: data representing the handler running the workflow.
NrH   rI   rJ   rM   rN   z/run-nowaitrO   )rP   rQ   r4   rR   r   rS   r	   r9   rT   r%   r   r=   r>   rU   s	            r$   run_workflow_nowait"WorkflowClient.run_workflow_nowait   s3    $ ".{;
 gw''Y!//+ 'H*::<*H}"(
 )3&##%%#[[m_K8| )  H (1--hmmo> &%%   J1#N   Y #STUSV!WXXY & &%%%s   E C  E D  AE ?D  E D&D"2D&E D$E  
C=*C88C==E  
D
DDE "D&$E &D=,D/-D=9E c           
      #    U(       a  SOSnSU 3nU R                  5        ISh  vN n UR                  SUSUUS.SS0SS	9 ISh  vN nUR                  S
:X  a  [        S5      eUR                  S:X  a"   SSS5      ISh  vN   SSS5      ISh  vN   g[	        U5        UR                  5         Sh  vN nUR                  5       (       d  M    [        R                  " U5      n	U	7v   M>   N N Nr Nd NA! [        R                   a  n
[        SU
 SU 35         Sn
A
Ms  Sn
A
ff = f
 SSS5      ISh  vN    Oe! , ISh  vN  (       d  f       ON= f! [        R                   a    [        SU 35      e[        R                   a  n
[!        SU
 35      eSn
A
ff = fSSS5      ISh  vN    g! , ISh  vN  (       d  f       g= f7f)a  
Stream events as they are produced by the workflow.

Args:
    handler_id (str): ID of the handler running the workflow
    include_internal_events (bool): Include internal workflow events. Defaults to False.
    lock_timeout (float): Timeout (in seconds) for acquiring the lock to iterate over the events.

Returns:
    AsyncGenerator[EventEnvelopeWithMetadata, None]: Generator for the events that are streamed as instances of `EventEnvelopeWithMetadata`.
truefalse/events/NGET)sseinclude_internalacquire_timeout
Connectionz
keep-alive)paramsheaderstimeouti  zHandler not found   zFailed to parse JSON: z, data: z(Timeout waiting for events from handler z#Failed to connect to event stream: )r9   streamr   r4   r%   aiter_linesstripr   model_validate_jsonr>   JSONDecodeErrorprintr   TimeoutExceptionTimeoutErrorRequestErrorConnectionError)r,   rM   include_internal_eventslock_timeout
incl_interr    r8   r   lineeventr"   s              r$   get_workflow_events"WorkflowClient.get_workflow_events   s    "  7VG
%##%%%Q!==&,6+7
 *<8  ) 
 
 ++s2()<==!--4!
 
 &%%( 09&.&:&:&< 	)d::<<)(A(U(U$()" ',9 &
 &,	) $(#7#7 ) %(>qc$&P Q () '=)
 
 
 
 
 
> )) ">zlK  %% Q%(KA3&OPPQK &%%%%%s3  %GC5GF<E
C7E-E;EC9EGC;GE7D2;C=
<D2?EC?3E5G7E9E;G=D2?D/	D*	$E*D/	/E3E>E?EF<E	EE	EF<E5F'F""F''F<*G5F86G<GGGGc                  #     [        U5      nSU0nU(       a  X6S'   U R                  5        ISh  vN nUR	                  SU 3US9I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $ ! [         a  n[        SU 35      eSnAff = f N Nf N)! , ISh  vN  (       d  f       g= f7f)a  
Send an event to the workflow.

Args:
    handler_id (str): ID of the handler of the running workflow to send the event to
    event (Event | dict[str, Any] | str): Event to send, represented as an Event object, a dictionary or a serialized string.
    step (Optional[str]): Step to send the event to (optional, defaults to None)

Returns:
    SendEventResponse: Confirmation of the send operation
z,Error while serializing the provided event: Nrx   stepr`   rO   )	rP   rQ   r4   r9   rT   r%   r   r=   r>   )	r,   rM   rx   r|   serialized_eventr"   rW   r8   r   s	            r$   
send_eventWorkflowClient.send_event  s     "	Q/?/F )01A'B#' ##%%#[[8J<)@|[TTH'1$33HMMOD	 &%%  	QKA3OPP	Q
 &T &%%%sx   CB #CB2CB8B42B8 CB6C
B/B**B//C4B86C8C>C?CCc                @   #    U R                  U5      I Sh  vN $  N7f)z&
Deprecated. Use get_handler instead.
N)get_handler)r,   rM   s     r$   
get_resultWorkflowClient.get_result#  s      %%j1111s   c                2  #    U R                  5        ISh  vN nUR                  SUUS.S9I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $  N_ NF N	! , ISh  vN  (       d  f       g= f7f)a9  
Get all the workflow handlers.
Args:
    status (list[Status] | None): List of statuses (e.g. "running", "completed", etc. ) to filter by. Defaults to None.
    workflow_name (list[str] | None): List of workflow names to filter by. Defaults to None.
Returns:
    HandlersListResponse: List of workflow handlers.
Nz	/handlers)statusrV   rf   )r9   r<   r%   r   r=   r>   )r,   r   rV   r8   r   s        r$   get_handlersWorkflowClient.get_handlers)  sz      ##%%#ZZ$%2 (  H (1'66x}}G &%% &%%%sT   BA7BA=A92A=%B1A;2B9A=;B=BBBBc                4  #    U R                  5        ISh  vN nUR                  SU 35      I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $  N` NF N	! , ISh  vN  (       d  f       g= f7f)z
Get a single workflow handler by identifier.

Args:
    handler_id (str): ID of the handler associated with the workflow run

Returns:
    HandlerData: Handler metadata persisted by the server.
N
/handlers/)r9   r<   r%   r   r=   r>   )r,   rM   r8   r   s       r$   r   WorkflowClient.get_handlerB  sh      ##%%#ZZ*ZL(ABBH'1--hmmo>	 &%%B &%%%sT   BA8BA>A:2A>&B2A<3B:A><B>BBBBc                J  #    U R                  5        ISh  vN nUR                  SU S3SU(       a  SOS0S9I Sh  vN n[        U5        [        R                  " UR                  5       5      sSSS5      ISh  vN   $  Nk NF N	! , ISh  vN  (       d  f       g= f7f)z
Stop and cancel a workflow run.

Args:
    handler_id (str): ID of the handler associated with the workflow run
    purge (bool): Whether or not to delete the run also from the persistent storage. Defaults to false
Nr   z/cancelpurger^   r_   r   )r9   rT   r%   r   r=   r>   )r,   rM   r   r8   r   s        r$   cancel_handlerWorkflowClient.cancel_handlerR  s      ##%%#[[ZL05g> )  H (1(77H &%% &%%%sT   B#BB##B	B2B	1B#=B>B#B	B#	B BB B#)r2   r-   )r-   zhttpx.AsyncClient)r2   str)r-   zhttpx.AsyncClient | Noner2   
str | None)returnz AsyncIterator[httpx.AsyncClient])r   r   )r   r   )NNN)
rV   r   rM   r   rK   z"StartEvent | dict[str, Any] | NonerL   zContext | dict[str, Any] | Noner   r   )F   )rM   r   rt   boolru   floatr   z/AsyncGenerator[EventEnvelopeWithMetadata, None]r*   )rM   r   rx   Event | dict[str, Any]r|   r   r   r   )rM   r   r   r   )NN)r   zlist[Status] | NonerV   zlist[str] | Noner   r   F)rM   r   r   r   r   r   )__name__
__module____qualname____firstlineno__r   r.   r   r9   r@   rD   rX   r[   ry   r~   r   r   r   r   __static_attributes__r+   r0   r$   r'   r'   6   s   ? ?   26#	! /! 	!  
BI" "&:>37+?+? +? 8	+?
 1+? 
+?` "&:>37+?+? +? 8	+?
 1+? 
+?` ).	:Q:Q "&:Q 	:Q
 
9:Q@  	EE &E 	E
 
E<2 '+*.H#H (H 
	H2?" .3II&*I	I Ir0   r'   c                    [        U [        5      (       a  U $ U(       a  U R                  5       $ [        R                  " U S9R                  5       $ )N)rx   )rR   dict
model_dumpr   
from_event)rx   rG   s     r$   rP   rP   f  sL     %  	 %%E2==?r0   )r   zhttpx.Responser   Noner   )rx   r   rG   r   r   zdict[str, Any])
__future__r   r   r>   typingr   r   r   r   
contextlibr   workflows.eventsr	   r
   	workflowsr   workflows.protocolr   r   r   r   r   r   r   &workflows.protocol.serializable_eventsr   r   r%   r'   rP   r+   r0   r$   <module>r      so    #    + .   *mI mIb	 16	!	)-		r0   