o
    î6WjÜC  ã                   @  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 m!Z! ddl"m#Z#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. ddl/m0Z0m1Z1 er—ddl"m2Z2 ddl3m4Z4 edƒZ5edƒZ6edƒZ7eddd�Z8e 9e:¡Z;de<d< G dd„ dee6 ƒZ=G dd„ de>ƒZ?d'dd „Z@d(d#d$„ZAG d%d&„ d&ƒZBd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Úis_type_alias_type)Ú	BaseModelÚis_basemodelÚadd_request_id)ÚRAW_RESPONSE_HEADER)ÚStreamÚAsyncStreamÚis_stream_class_typeÚextract_stream_chunk_type)ÚAPIResponseValidationError)ÚJSONLDecoderÚAsyncJSONLDecoder)ÚFinalRequestOptions)Ú
BaseClientÚPÚRÚ_TÚ_T_coT)Ú	covariantzlogging.LoggerÚlogc                   @  sH  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ed< 	 ddœdKdd„ZedLdd „ƒZedMd$d%„ƒZ	edNd'd%„ƒZ	d(d)œdOd,d%„Z	edPd.d/„ƒZ
edQd1d2„ƒZedRd3d4„ƒZedSd6d7„ƒZedTd9d:„ƒZedUd<d=„ƒZedTd>d?„ƒZedTd@dA„ƒZedVdBdC„ƒZedWdEdF„ƒZd(d)œdOdGdH„ZedTdIdJ„ƒZd(S )XÚ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.
    útype[R]Ú_cast_toúBaseClient[Any, Any]Ú_clientzdict[type[Any], Any]Ú_parsed_by_typeÚboolÚ_streamú1type[Stream[Any]] | type[AsyncStream[Any]] | NoneÚ_stream_clsr#   Ú_optionsúhttpx.ResponseÚhttp_responseÚintÚretries_takenr   )r9   ÚrawÚcast_toÚclientÚstreamÚ
stream_clsÚoptionsÚreturnÚNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S ©N)r-   r/   r0   r2   r4   r5   r7   r9   )Úselfr:   r;   r<   r=   r>   r?   r9   © rD   úi/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/anthropic/_legacy_response.pyÚ__init__H   s   
zLegacyAPIResponse.__init__ú
str | Nonec                 C  s   | j j d¡S )Nz
request-id)r7   ÚheadersÚget©rC   rD   rD   rE   Ú
request_id\   s   zLegacyAPIResponse.request_idÚtoútype[_T]r'   c                C  ó   d S rB   rD   )rC   rL   rD   rD   rE   Úparse`   ó   zLegacyAPIResponse.parser&   c                 C  rN   rB   rD   rJ   rD   rD   rE   rO   c   rP   N©rL   útype[_T] | NoneúR | _Tc                C  sx   |dur|n| j }| j |¡}|dur|S | j|d�}t| jjƒr'| j |¡}t|tƒr2t	|| j
ƒ || j|< tt|ƒ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`
        NrQ   )r-   r0   rI   Ú_parser   r5   Úpost_parserÚ
isinstancer   r   rK   r   r&   )rC   rL   Ú	cache_keyÚcachedÚparsedrD   rD   rE   rO   f   s    


úhttpx.Headersc                 C  ó   | j jS rB   )r7   rH   rJ   rD   rD   rE   rH   •   ó   zLegacyAPIResponse.headersúhttpx.Requestc                 C  r[   rB   )r7   ÚrequestrJ   rD   rD   rE   Úhttp_request™   r\   zLegacyAPIResponse.http_requestc                 C  r[   rB   )r7   Ústatus_coderJ   rD   rD   rE   r`   �   r\   zLegacyAPIResponse.status_codeú	httpx.URLc                 C  r[   rB   )r7   ÚurlrJ   rD   rD   rE   rb   ¡   r\   zLegacyAPIResponse.urlÚstrc                 C  r[   rB   )r_   ÚmethodrJ   rD   rD   rE   rd   ¥   r\   zLegacyAPIResponse.methodÚbytesc                 C  r[   )zŠReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r7   ÚcontentrJ   rD   rD   rE   rf   ©   s   zLegacyAPIResponse.contentc                 C  r[   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r7   ÚtextrJ   rD   rD   rE   rg   ²   s   zLegacyAPIResponse.textc                 C  r[   rB   )r7   Úhttp_versionrJ   rD   rD   rE   rh   º   r\   zLegacyAPIResponse.http_versionc                 C  r[   rB   )r7   Ú	is_closedrJ   rD   rD   rE   ri   ¾   r\   zLegacyAPIResponse.is_closedúdatetime.timedeltac                 C  r[   )zCThe time taken for the complete request/response cycle to complete.)r7   ÚelapsedrJ   rD   rD   rE   rk   Â   s   zLegacyAPIResponse.elapsedc          
      C  sÐ  |d ur|n| j }t|ƒr|j}|rt|ƒrt|dƒ}t|ƒp |}t |¡rbtt	t
|ƒtƒrDt	tt	d|ƒ| jjdd�t|dƒ| jd�ƒS tt	t
|ƒtƒrbt	tt	d|ƒ| jjdd�t|dƒ| jd�ƒS | jrÁ|rŠt|ƒsutdt› dt› �ƒ‚t	t|t|d	d
�| jt	t
| jƒ| jd�ƒS | jr¢t	t| jt| jƒ| jt	t
| jƒ| jd�ƒS t	d| jjƒ}|d u r°tƒ ‚t	t||| jt	t
| jƒ| jd�ƒS |tu rÊt	td ƒS | j}|tkr×t	t|jƒS |t krãt	tt |jƒƒS |t!krït	tt!|jƒƒS |t"krýt	t|j #¡ dkƒS t |¡�rt|t$ƒ�rt	t||ƒƒS |t%k�rt&dƒ‚t |¡�r5t|t'j(ƒ�r5|t'j(k�r0t)dƒ‚t	t|ƒS t |¡�rLt|t*ƒ�sLt|t+j*ƒ�rLtdƒ‚|t,u�r„|t-u�r„|t.u�r„|t/u�r„t|t*ƒ�s„t&d|› dt*› dt.› dt-› dt/› dt› dt› dt'j(› d�ƒ‚|j0 1dd¡ 2d¡^}}| 3d¡�sÛt4|ƒ�rÇz| 5¡ }W n t6�y½ }	 zt7 8dt9|	ƒ|	¡ W Y d }	~	nd }	~	ww | jj:|||d�S | jj;�rØt<|d|› d�|jd�‚|jS | 5¡ }| jj:|||d�S )Nr   ztype[JSONLDecoder[Any]]é@   ©Ú
chunk_size)Úraw_iteratorÚ	line_typer7   ztype[AsyncJSONLDecoder[Any]]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])Úfailure_message)r;   Úresponser<   r?   r3   Útruez+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Ú*ú;Újsonz5Could not read JSON from response data due to %s - %s)Údatar;   rr   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rr   ÚmessageÚbody)=r-   r   Ú	__value__r   r   r   ÚinspectÚisclassÚ
issubclassr   r   r!   r&   r7   Ú
iter_bytesr"   Úaiter_bytesr2   r   Ú	TypeErrorr   r   r'   r   r/   r5   r4   Ú_default_stream_clsÚMissingStreamClassErrorr   rc   rg   r8   Úfloatr1   ÚlowerÚHttpxBinaryResponseContentr+   ÚRuntimeErrorÚhttpxÚResponseÚ
ValueErrorr   ÚpydanticÚobjectÚlistÚdictr   rH   rI   ÚsplitÚendswithr   rw   Ú	Exceptionr*   ÚdebugÚtypeÚ_process_response_dataÚ_strict_response_validationr    )
rC   rL   r;   Úoriginr>   rr   Úcontent_typeÚ_rx   ÚexcrD   rD   rE   rT   Ç   s  

ýþ	ýþ	þ
ùþ
üþ

üþ


ÿ
þ
ÿÿü
û



ÿ6ÿ
€ÿý

ý	ýzLegacyAPIResponse._parsec                 C  s    d| j › d| jj› d| j› d�S )Nz<APIResponse [ú z] type=ú>)r`   r7   Úreason_phraser-   rJ   rD   rD   rE   Ú__repr__n  s    zLegacyAPIResponse.__repr__)r:   r6   r;   r,   r<   r.   r=   r1   r>   r3   r?   r#   r9   r8   r@   rA   ©r@   rG   )rL   rM   r@   r'   )r@   r&   )rL   rR   r@   rS   )r@   rZ   )r@   r]   )r@   r8   )r@   ra   ©r@   rc   ©r@   re   )r@   r1   )r@   rj   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rF   ÚpropertyrK   r   rO   rH   r_   r`   rb   rd   rf   rg   rh   ri   rk   rT   r   r�   rD   rD   rD   rE   r+   /   sX   
 ÷/ (r+   c                      s   e Zd Zd‡ fdd„Z‡  ZS )rƒ   r@   rA   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)ÚsuperrF   rJ   ©Ú	__class__rD   rE   rF   t  s   ÿz MissingStreamClassError.__init__©r@   rA   )r¡   r¢   r£   rF   Ú__classcell__rD   rD   r¨   rE   rƒ   s  s    rƒ   ÚfuncúCallable[P, R]r@   ú!Callable[P, LegacyAPIResponse[R]]c                   ó   t  ˆ ¡d
‡ fdd„ƒ}|S )ú�Higher order function that takes one of our bound API methods and wraps it
    to support returning the raw `APIResponse` object directly.
    ÚargsúP.argsÚkwargsúP.kwargsr@   úLegacyAPIResponse[R]c                    s@   i t t| d¡ƒp
i ¥}d|t< ||d< t tt ˆ | i |¤ŽƒS ©NÚextra_headersrs   ©r   r   rI   r   r+   r&   ©r±   r³   r·   ©r¬   rD   rE   Úwrapped  s   z(to_raw_response_wrapper.<locals>.wrappedN©r±   r²   r³   r´   r@   rµ   ©Ú	functoolsÚwraps©r¬   r»   rD   rº   rE   Úto_raw_response_wrapperz  ó   rÁ   úCallable[P, Awaitable[R]]ú,Callable[P, Awaitable[LegacyAPIResponse[R]]]c                   r¯   )r°   r±   r²   r³   r´   r@   rµ   c                  Ÿ  sH   �i t t| d¡ƒpi ¥}d|t< ||d< t tt ˆ | i |¤ŽI d H ƒS r¶   r¸   r¹   rº   rD   rE   r»   �  s
   €z.async_to_raw_response_wrapper.<locals>.wrappedNr¼   r½   rÀ   rD   rº   rE   Úasync_to_raw_response_wrapper‹  rÂ   rÅ   c                   @  s   e Zd ZU ded< d?dd„Zed@dd	„ƒZedAdd„ƒZedBdd„ƒZedBdd„ƒZ	dCdd„Z
d@dd„ZdDdEdd„ZdDdFdd „ZdGd!d"„ZdDdEd#d$„ZdHd'd(„Zed)ƒdd*œdId+d,„ƒZdJd-d.„Zd@d/d0„ZdDdKd2d3„ZdDdLd5d6„ZdMd7d8„ZdDdKd9d:„Zed)ƒdd*œdId;d<„ƒZdJd=d>„ZdS )Nr†   r6   rr   r@   rA   c                 C  s
   || _ d S rB   )rr   )rC   rr   rD   rD   rE   rF   Ÿ  ó   
z#HttpxBinaryResponseContent.__init__re   c                 C  r[   rB   )rr   rf   rJ   rD   rD   rE   rf   ¢  r\   z"HttpxBinaryResponseContent.contentrc   c                 C  r[   rB   )rr   rg   rJ   rD   rD   rE   rg   ¦  r\   zHttpxBinaryResponseContent.textrG   c                 C  r[   rB   )rr   ÚencodingrJ   rD   rD   rE   rÇ   ª  r\   z#HttpxBinaryResponseContent.encodingc                 C  r[   rB   )rr   Úcharset_encodingrJ   rD   rD   rE   rÈ   ®  r\   z+HttpxBinaryResponseContent.charset_encodingr³   r   c                 K  s   | j jdi |¤ŽS )NrD   )rr   rw   )rC   r³   rD   rD   rE   rw   ²  s   zHttpxBinaryResponseContent.jsonc                 C  ó
   | j  ¡ S rB   )rr   ÚreadrJ   rD   rD   rE   rÊ   µ  rÆ   zHttpxBinaryResponseContent.readNrn   ú
int | NoneúIterator[bytes]c                 C  ó   | j  |¡S rB   )rr   r   ©rC   rn   rD   rD   rE   r   ¸  ó   z%HttpxBinaryResponseContent.iter_bytesúIterator[str]c                 C  rÍ   rB   )rr   Ú	iter_textrÎ   rD   rD   rE   rÑ   »  rÏ   z$HttpxBinaryResponseContent.iter_textc                 C  rÉ   rB   )rr   Ú
iter_linesrJ   rD   rD   rE   rÒ   ¾  rÆ   z%HttpxBinaryResponseContent.iter_linesc                 C  rÍ   rB   )rr   Úiter_rawrÎ   rD   rD   rE   rÓ   Á  rÏ   z#HttpxBinaryResponseContent.iter_rawÚfileústr | os.PathLike[str]c                 C  sL   t |dd��}| j ¡ D ]}| |¡ qW d  ƒ dS 1 sw   Y  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©Úopenrr   r   Úwrite)rC   rÔ   Úfrx   rD   rD   rE   Úwrite_to_fileÄ  s
   ÿ"ÿz(HttpxBinaryResponseContent.write_to_filez‚Due to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used insteadrm   c                C  sN   t |dd��}| j |¡D ]}| |¡ qW d   ƒ d S 1 s w   Y  d S ©NrÖ   r×   rÙ   )rC   rÔ   rn   rÜ   rx   rD   rD   rE   Ústream_to_fileÔ  s
   	ÿ"ÿz)HttpxBinaryResponseContent.stream_to_filec                 C  rÉ   rB   )rr   ÚcloserJ   rD   rD   rE   rà   á  rÆ   z HttpxBinaryResponseContent.closec                 Ã  ó   �| j  ¡ I d H S rB   )rr   ÚareadrJ   rD   rD   rE   râ   ä  ó   €z HttpxBinaryResponseContent.areadúAsyncIterator[bytes]c                 Ã  ó   �| j  |¡S rB   )rr   r€   rÎ   rD   rD   rE   r€   ç  ó   €z&HttpxBinaryResponseContent.aiter_bytesúAsyncIterator[str]c                 Ã  rå   rB   )rr   Ú
aiter_textrÎ   rD   rD   rE   rè   ê  ræ   z%HttpxBinaryResponseContent.aiter_textc                 Ã  s   �| j  ¡ S rB   )rr   Úaiter_linesrJ   rD   rD   rE   ré   í  s   €
z&HttpxBinaryResponseContent.aiter_linesc                 Ã  rå   rB   )rr   Ú	aiter_rawrÎ   rD   rD   rE   rê   ð  ræ   z$HttpxBinaryResponseContent.aiter_rawc             	   Ã  s„   �t  |¡}|jdd�I d H 4 I d H š"}| j |¡2 z3 d H W }| |¡I d H  q6 W d   ƒI d H  d S 1 I d H s;w   Y  d S rÞ   )ÚanyioÚPathrÚ   rr   r€   rÛ   )rC   rÔ   rn   ÚpathrÜ   rx   rD   rD   rE   Úastream_to_fileó  s   €
	ÿ.ÿz*HttpxBinaryResponseContent.astream_to_filec                 Ã  rá   rB   )rr   ÚacloserJ   rD   rD   rE   rï     rã   z!HttpxBinaryResponseContent.aclose)rr   r6   r@   rA   r    rŸ   rž   )r³   r   r@   r   rB   )rn   rË   r@   rÌ   )rn   rË   r@   rÐ   )r@   rÐ   )rÔ   rÕ   r@   rA   )rÔ   rÕ   rn   rË   r@   rA   rª   )rn   rË   r@   rä   )rn   rË   r@   rç   )r@   rç   )r¡   r¢   r£   r¥   rF   r¦   rf   rg   rÇ   rÈ   rw   rÊ   r   rÑ   rÒ   rÓ   rÝ   r   rß   rà   râ   r€   rè   ré   rê   rî   rï   rD   rD   rD   rE   r†   œ  sF   
 




ÿü



ÿür†   )r¬   r­   r@   r®   )r¬   rÃ   r@   rÄ   )CÚ
__future__r   Úosr|   ÚloggingÚdatetimer¾   Útypingr   r   r   r   r   r   r	   r
   r   r   Útyping_extensionsr   r   r   r   r   rë   rˆ   r‹   Ú_typesr   Ú_utilsr   r   r   r   Ú_modelsr   r   r   Ú
_constantsr   Ú
_streamingr   r   r   r   Ú_exceptionsr    Ú_decoders.jsonlr!   r"   r#   Ú_base_clientr$   r%   r&   r'   r(   Ú	getLoggerr¡   r*   r¥   r+   r�   rƒ   rÁ   rÅ   r†   rD   rD   rD   rE   Ú<module>   sB    0  F

