o
    +iSC                     @  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rd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_stream1type[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   W/var/www/html/psymed-ai/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[   )zReturn 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|rt|sut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 u rt t	t||| jt	t
| jdS |tu rt	td S | j}|tkrt	t|jS |tkrt	tt|jS |t krt	tt |jS |t!krt	t|j" dkS t|r
t|t#r
t	t||S |t$krt%dt|r/t|t&j'r/|t&j'kr*t(dt	t|S t|rFt|t)sFt|t*j)rFt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|j/0dd1d^}}|2dst3|rz|4 }W n t5y }	 zt67dt8|	|	 W Y d }	~	nd }	~	ww | jj9|||dS | jj:rt;|d| d|jd|jS |4 }| jj9|||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<   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/   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__k  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   q  s   z MissingStreamClassError.__init__r@   rA   )r   r   r   rF   __classcell__rD   rD   r   rE   r   p  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_wrapperw     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_filezDue 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  C

