B
    fL<                 @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZmZmZmZmZ d dlmZmZmZmZ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"m#Z# dd	l$m%Z% dd
l&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, erddl!m-Z- ddl.m/Z/ edZ0edZ1edZ2e3e4Z5de6d< G dd dee1 Z7G dd de8Z9dddddZ:dddddZ;G d d! d!Z<dS )"    )annotationsN)
TYPE_CHECKINGAnyUnionGenericTypeVarCallableIteratorAsyncIteratorcastoverload)	Awaitable	ParamSpecoverride
deprecated
get_origin   )NoneType)is_givenextract_type_argis_annotated_type)	BaseModelis_basemodel)RAW_RESPONSE_HEADER)StreamAsyncStreamis_stream_class_typeextract_stream_chunk_type)APIResponseValidationError)FinalRequestOptions)
BaseClientPR_Tzlogging.Loggerlogc               @  s|  e Zd ZU dZded< ded< ded< ded	< d
ed< ded< ded< ddddd
dddddZedddddZeddddZdddddd dZed!dd"d#Z	ed$dd%d&Z
ed'dd(d)Zed*dd+d,Zed-dd.d/Zed0dd1d2Zed-dd3d4Zed-dd5d6Zeddd7d8Zed9dd:d;Zdddddd<d=Zed-dd>d?ZdS )@LegacyAPIResponsea  This is a legacy class as it will be replaced by `APIResponse`
    and `AsyncAPIResponse` in the `_response.py` file in the next major
    release.

    For the sync client this will mostly be the same with the exception
    of `content` & `text` will be methods instead of properties. In the
    async client, all methods will be async.

    A migration script will be provided & the migration in general should
    be smooth.
    ztype[R]_cast_tozBaseClient[Any, Any]_clientzdict[type[Any], Any]_parsed_by_typebool_streamz1type[Stream[Any]] | type[AsyncStream[Any]] | None_stream_clsr   _optionszhttpx.Responsehttp_responseNone)rawcast_toclientstream
stream_clsoptionsreturnc            C  s.   || _ || _i | _|| _|| _|| _|| _d S )N)r&   r'   r(   r*   r+   r,   r-   )selfr/   r0   r1   r2   r3   r4    r7   ]/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/anthropic/_legacy_response.py__init__8   s    
zLegacyAPIResponse.__init__ztype[_T]r#   )tor5   c            C  s   d S )Nr7   )r6   r:   r7   r7   r8   parseJ   s    zLegacyAPIResponse.parser"   )r5   c             C  s   d S )Nr7   )r6   r7   r7   r8   r;   N   s    N)r:   ztype[_T] | NonezR | _Tc            C  s\   |dk	r|n| j }| j|}|dk	r*|S | j|d}t| jjrN| j|}|| j|< |S )a  Returns the rich python representation of this response's data.

        NOTE: For the async client: this will become a coroutine in the next major version.

        For lower-level control, see `.read()`, `.json()`, `.iter_bytes()`.

        You can customise the type that the response is parsed into through
        the `to` argument, e.g.

        ```py
        from anthropic import BaseModel


        class MyModel(BaseModel):
            foo: str


        obj = response.parse(to=MyModel)
        print(obj.foo)
        ```

        We support parsing:
          - `BaseModel`
          - `dict`
          - `list`
          - `Union`
          - `str`
          - `int`
          - `float`
          - `httpx.Response`
        N)r:   )r&   r(   get_parser   r,   Zpost_parser)r6   r:   	cache_keycachedparsedr7   r7   r8   r;   R   s     
zhttpx.Headersc             C  s   | j jS )N)r-   headers)r6   r7   r7   r8   rA   ~   s    zLegacyAPIResponse.headerszhttpx.Requestc             C  s   | j jS )N)r-   request)r6   r7   r7   r8   http_request   s    zLegacyAPIResponse.http_requestintc             C  s   | j jS )N)r-   status_code)r6   r7   r7   r8   rE      s    zLegacyAPIResponse.status_codez	httpx.URLc             C  s   | j jS )N)r-   url)r6   r7   r7   r8   rF      s    zLegacyAPIResponse.urlstrc             C  s   | j jS )N)rC   method)r6   r7   r7   r8   rH      s    zLegacyAPIResponse.methodbytesc             C  s   | j jS )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r-   content)r6   r7   r7   r8   rJ      s    zLegacyAPIResponse.contentc             C  s   | j jS )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r-   text)r6   r7   r7   r8   rK      s    zLegacyAPIResponse.textc             C  s   | j jS )N)r-   http_version)r6   r7   r7   r8   rL      s    zLegacyAPIResponse.http_versionc             C  s   | j jS )N)r-   	is_closed)r6   r7   r7   r8   rM      s    zLegacyAPIResponse.is_closedzdatetime.timedeltac             C  s   | j jS )zCThe time taken for the complete request/response cycle to complete.)r-   elapsed)r6   r7   r7   r8   rN      s    zLegacyAPIResponse.elapsedc      
      C  s2  |rt |rt|d}| jr|rbt|s<tdt dt tt|t	|dd| j
tt| jdS | jrtt| jt	| j| j
tt| jdS td| jj}|d krt tt|| j| j
tt| jdS |d k	r|n| j}t |rt|d}|tkrttd S | j
}|tkrtt|jS |tkr8ttt|jS |tkrRttt|jS t|p^|}t|rt|trtt||S |tkrtdt|rt|tjr|tjkrt d	tt|S t|rt|t!st|t"j!rtd
|t#k	rn|t$k	rn|t%k	rn|t&k	rnt|t!sntd| dt! dt% dt$ dt& dt dt dtj d|j'(dd)d^}}|dkrt*|ry|+ }W n6 t,k
r }	 zt-.dt/|	|	 W d d }	~	X Y nX | jj0|||dS | jj1rt2|d| d|jd|jS |+ }| jj0|||dS )Nr   z/Expected custom parse type to be a subclass of z or zUExpected custom stream type to be passed with a type argument, e.g. Stream[ChunkType])Zfailure_message)r0   responser1   z1type[Stream[Any]] | type[AsyncStream[Any]] | Nonez+Unexpected state - cast_to is `APIResponse`z:Subclasses of httpx.Response cannot be passed to `cast_to`zYPydantic models must subclass our base model type, e.g. `from anthropic import BaseModel`zUnsupported type, expected z to be a subclass of z, .zcontent-type*;zapplication/jsonz5Could not read JSON from response data due to %s - %s)datar0   rO   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rO   messagebody)3r   r   r*   r   	TypeErrorr   r   r   r#   r   r-   r   r'   r+   r"   Z_default_stream_clsMissingStreamClassErrorr&   r   rG   rK   rD   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr%   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rA   r<   splitr   json	Exceptionr$   debugtypeZ_process_response_dataZ_strict_response_validationr   )
r6   r:   r3   r0   rO   origincontent_type_rS   excr7   r7   r8   r=      s    








&



:

$


zLegacyAPIResponse._parsec             C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)rE   r-   Zreason_phraser&   )r6   r7   r7   r8   __repr__4  s    zLegacyAPIResponse.__repr__)__name__
__module____qualname____doc____annotations__r9   r   r;   propertyrA   rC   rE   rF   rH   rJ   rK   rL   rM   rN   r=   r   rp   r7   r7   r7   r8   r%   "   s2   
,	 r%   c                  s"   e Zd Zdd fddZ  ZS )rW   r.   )r5   c               s   t  d d S )Nz}The `stream` argument was set to `True` but the `stream_cls` argument was not given. See `anthropic._streaming` for reference)superr9   )r6   )	__class__r7   r8   r9   :  s    z MissingStreamClassError.__init__)rq   rr   rs   r9   __classcell__r7   r7   )rx   r8   rW   9  s   rW   zCallable[P, R]z!Callable[P, LegacyAPIResponse[R]])funcr5   c               s$   t  dddd fdd}|S )zHigher order function that takes one of our bound API methods and wraps it
    to support returning the raw `APIResponse` object directly.
    zP.argszP.kwargszLegacyAPIResponse[R])argskwargsr5   c                s:   t t|dpi }d|t< ||d< t tt  | |S )Nextra_headerstrue)r   r   r<   r   r%   r"   )r{   r|   r}   )rz   r7   r8   wrappedE  s    z(to_raw_response_wrapper.<locals>.wrapped)	functoolswraps)rz   r   r7   )rz   r8   to_raw_response_wrapper@  s     	r   zCallable[P, Awaitable[R]]z,Callable[P, Awaitable[LegacyAPIResponse[R]]]c               s$   t  dddd fdd}|S )zHigher order function that takes one of our bound API methods and wraps it
    to support returning the raw `APIResponse` object directly.
    zP.argszP.kwargszLegacyAPIResponse[R])r{   r|   r5   c                s@   t t|dpi }d|t< ||d< t tt  | |I d H S )Nr}   r~   )r   r   r<   r   r%   r"   )r{   r|   r}   )rz   r7   r8   r   V  s    z.async_to_raw_response_wrapper.<locals>.wrapped)r   r   )rz   r   r7   )rz   r8   async_to_raw_response_wrapperQ  s     	r   c               @  s  e Zd ZU ded< dddddZeddd	d
ZeddddZeddddZeddddZ	dddddZ
ddddZdAdddddZdBdddd d!Zddd"d#ZdCdddd$d%Zd&dd'd(d)Zed*dd+d&ddd,d-d.Zddd/d0Zddd1d2ZdDdd3dd4d5ZdEdd6dd7d8Zd6dd9d:ZdFdd3dd;d<Zed*dd+d&ddd,d=d>Zddd?d@ZdS )Gr\   zhttpx.ResponserO   r.   )rO   r5   c             C  s
   || _ d S )N)rO   )r6   rO   r7   r7   r8   r9   e  s    z#HttpxBinaryResponseContent.__init__rI   )r5   c             C  s   | j jS )N)rO   rJ   )r6   r7   r7   r8   rJ   h  s    z"HttpxBinaryResponseContent.contentrG   c             C  s   | j jS )N)rO   rK   )r6   r7   r7   r8   rK   l  s    zHttpxBinaryResponseContent.textz
str | Nonec             C  s   | j jS )N)rO   encoding)r6   r7   r7   r8   r   p  s    z#HttpxBinaryResponseContent.encodingc             C  s   | j jS )N)rO   charset_encoding)r6   r7   r7   r8   r   t  s    z+HttpxBinaryResponseContent.charset_encodingr   )r|   r5   c             K  s   | j jf |S )N)rO   rf   )r6   r|   r7   r7   r8   rf   x  s    zHttpxBinaryResponseContent.jsonc             C  s
   | j  S )N)rO   read)r6   r7   r7   r8   r   {  s    zHttpxBinaryResponseContent.readNz
int | NonezIterator[bytes])
chunk_sizer5   c             C  s   | j |S )N)rO   
iter_bytes)r6   r   r7   r7   r8   r   ~  s    z%HttpxBinaryResponseContent.iter_byteszIterator[str]c             C  s   | j |S )N)rO   	iter_text)r6   r   r7   r7   r8   r     s    z$HttpxBinaryResponseContent.iter_textc             C  s
   | j  S )N)rO   
iter_lines)r6   r7   r7   r8   r     s    z%HttpxBinaryResponseContent.iter_linesc             C  s   | j |S )N)rO   iter_raw)r6   r   r7   r7   r8   r     s    z#HttpxBinaryResponseContent.iter_rawzstr | os.PathLike[str])filer5   c          	   C  s:   t |dd$}x| j D ]}|| qW W dQ R X dS )av  Write the output to the given file.

        Accepts a filename or any path-like object, e.g. pathlib.Path

        Note: if you want to stream the data to the file instead of writing
        all at once then you should use `.with_streaming_response` when making
        the API request, e.g. `client.with_streaming_response.foo().stream_to_file('my_filename.txt')`
        wb)modeN)openrO   r   write)r6   r   frS   r7   r7   r8   write_to_file  s    z(HttpxBinaryResponseContent.write_to_filezDue to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead)r   )r   r   r5   c         	   C  s<   t |dd&}x| j|D ]}|| qW W d Q R X d S )Nr   )r   )r   rO   r   r   )r6   r   r   r   rS   r7   r7   r8   stream_to_file  s    	z)HttpxBinaryResponseContent.stream_to_filec             C  s
   | j  S )N)rO   close)r6   r7   r7   r8   r     s    z HttpxBinaryResponseContent.closec               s   | j  I d H S )N)rO   aread)r6   r7   r7   r8   r     s    z HttpxBinaryResponseContent.areadzAsyncIterator[bytes]c               s   | j |S )N)rO   aiter_bytes)r6   r   r7   r7   r8   r     s    z&HttpxBinaryResponseContent.aiter_byteszAsyncIterator[str]c               s   | j |S )N)rO   
aiter_text)r6   r   r7   r7   r8   r     s    z%HttpxBinaryResponseContent.aiter_textc               s
   | j  S )N)rO   aiter_lines)r6   r7   r7   r8   r     s    z&HttpxBinaryResponseContent.aiter_linesc               s   | j |S )N)rO   	aiter_raw)r6   r   r7   r7   r8   r     s    z$HttpxBinaryResponseContent.aiter_rawc         
     s~   t |}|jddI d H 4 I d H J}xB| j|2 y3 d H }W n
 tk
s^X ||I d H  q4   Y  W W d Q I d H R X d S )Nr   )r   )anyioPathr   rO   r   StopAsyncIterationr   )r6   r   r   pathr   rS   r7   r7   r8   astream_to_file  s    	
&z*HttpxBinaryResponseContent.astream_to_filec               s   | j  I d H S )N)rO   aclose)r6   r7   r7   r8   r     s    z!HttpxBinaryResponseContent.aclose)N)N)N)N)N)N)rq   rr   rs   ru   r9   rv   rJ   rK   r   r   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   r7   r7   r8   r\   b  s4   
r\   )=
__future__r   osrY   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   r^   ra   _typesr   _utilsr   r   r   Z_modelsr   r   
_constantsr   Z
_streamingr   r   r   r   _exceptionsr   r   Z_base_clientr    r!   r"   r#   	getLoggerrq   r$   ru   r%   rV   rW   r   r   r\   r7   r7   r7   r8   <module>   s>    0  