o
    i                     @  s   d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZ eddG dd dZG dd	 d	e
ZG d
d de
ZG dd deZdS )    )annotations)	dataclass)AnyAsyncIterable	AwaitableMappingOptionalProtocolTypeUnionT)frozenc                   @  s$   e Zd ZU dZded< 	 ded< dS )Contentz
    A container for a map of headers and a byte array of data.

    It is used by the SDK's Serializer interface implementations.
    Mapping[str, str]headersbytesdataN)__name__
__module____qualname____doc____annotations__ r   r   Z/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/nexusrpc/_serializer.pyr      s   
 r   c                   @  s(   e Zd ZdZdddZ	ddddZdS )
SerializerzX
    Serializer is used by the framework to serialize/deserialize input and output.
    valuer   return"Union[Content, Awaitable[Content]]c                 C     dS )z)Serialize encodes a value into a Content.Nr   )selfr   r   r   r   	serialize(      zSerializer.serializeNcontentr   as_typeOptional[Type[Any]]Union[Any, Awaitable[Any]]c                 C  r   )a  Deserialize decodes a Content into a value.

        Args:
            content: The content to deserialize.
            as_type: The type to convert the result of deserialization into.
                     Do not attempt type conversion if this is None.
        Nr   )r   r!   r"   r   r   r   deserialize,   s   
zSerializer.deserialize)r   r   r   r   N)r!   r   r"   r#   r   r$   )r   r   r   r   r   r%   r   r   r   r   r   #   s
    
r   c                   @  s   e Zd Z	dd	ddZdS )

LazyValueTNr"   r#   r   r$   c                 C  s   d S r&   r   r   r"   r   r   r   consume:   r    zLazyValueT.consumer&   )r"   r#   r   r$   )r   r   r   r)   r   r   r   r   r'   9   s    r'   c                   @  s*   e Zd ZdZ	ddddZddddZdS )	LazyValuea  
    A container for a value encoded in an underlying stream.

    It is used to stream inputs and outputs in the various client and server APIs.

    Example:
        .. code-block:: python

            # Creating a LazyValue from raw data
            lazy_input = LazyValue(
                serializer=my_serializer,
                headers={"content-type": "application/json"},
                stream=async_data_stream
            )

            # Using LazyValue with Handler.start_operation
            handler = nexusrpc.handler.Handler([my_service])
            result = await handler.start_operation(ctx, lazy_input)

            # Consuming a LazyValue directly
            deserialized_data = await lazy_input.consume(as_type=MyInputType)
    N
serializerr   r   r   streamOptional[AsyncIterable[bytes]]r   Nonec                 C  s   || _ || _|| _dS )a  
        Args:
            serializer: The serializer to use for consuming the value.
            headers: Headers that include information on how to process the stream's content.
                     Headers constructed by the framework always have lower case keys.
                     User provided keys are treated case-insensitively.
            stream:  Iterable that contains request or response data. None means empty data.
        N)r+   r   r,   )r   r+   r   r,   r   r   r   __init__W   s   
zLazyValue.__init__r"   r#   r   c                   sx   | j du r| jjt| jdd|dI dH S t| j ts td| jjt| jddd | j 2 I dH d|dI dH S )zb
        Consume the underlying reader stream, deserializing via the embedded serializer.
        N    )r   r   )r"   z3When using consume, stream must be an AsyncIterablec                   s   g | z3 d H W }|q6 S r&   r   ).0cr   r   r   
<listcomp>w   s    z%LazyValue.consume.<locals>.<listcomp>)	r,   r+   r%   r   r   
isinstancer   
ValueErrorjoinr(   r   r   r   r)   i   s   
zLazyValue.consumer&   )r+   r   r   r   r,   r-   r   r.   )r"   r#   r   r   )r   r   r   r   r/   r)   r   r   r   r   r*   ?   s
    r*   N)
__future__r   dataclassesr   typingr   r   r   r   r   r	   r
   r   r   r   r'   r*   r   r   r   r   <module>   s    (