o
    ùT·ja@  ã                   @  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# 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/m0Z0 er•ddl$m1Z1 ddl2m3Z3 edƒZ4edƒZ5edƒZ6e 7e8¡Z9de:d< G dd„ dee5 ƒZ;G dd„ de<ƒZ=d$dd„Z>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Úis_type_alias_type)Úhttp_response_types)Ú	BaseModelÚis_basemodelÚadd_request_id)ÚRAW_RESPONSE_HEADER)ÚStreamÚAsyncStreamÚis_stream_class_typeÚextract_stream_chunk_type)ÚAPIResponseValidationError)ÚFinalRequestOptions)Ú
BaseClientÚPÚRÚ_Tz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úhttpx2.ResponseÚhttp_responseÚintÚretries_takenr   )r6   ÚrawÚcast_toÚclientÚstreamÚ
stream_clsÚoptionsÚreturnÚNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S ©N)r*   r,   r-   r/   r1   r2   r4   r6   )Úselfr7   r8   r9   r:   r;   r<   r6   © rA   úZ/home/dinkstrade/pdmp-scanner/venv/lib/python3.10/site-packages/openai/_legacy_response.pyÚ__init__G   s   
zLegacyAPIResponse.__init__ú
str | Nonec                 C  s   | j j d¡S )Nzx-request-id)r4   ÚheadersÚget©r@   rA   rA   rB   Ú
request_id[   s   zLegacyAPIResponse.request_idÚtoútype[_T]r&   c                C  ó   d S r?   rA   )r@   rI   rA   rA   rB   Úparse_   ó   zLegacyAPIResponse.parser%   c                 C  rK   r?   rA   rG   rA   rA   rB   rL   b   rM   N©rI   ú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 openai 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`
          - `httpx2.Response`
        NrN   )r*   r-   rF   Ú_parser   r2   Úpost_parserÚ
isinstancer   r   rH   r   r%   )r@   rI   Ú	cache_keyÚcachedÚparsedrA   rA   rB   rL   e   s    


úhttpx2.Headersc                 C  ó   | j jS r?   )r4   rE   rG   rA   rA   rB   rE   ”   ó   zLegacyAPIResponse.headersúhttpx2.Requestc                 C  rX   r?   )r4   ÚrequestrG   rA   rA   rB   Úhttp_request˜   rY   zLegacyAPIResponse.http_requestc                 C  rX   r?   )r4   Ústatus_coderG   rA   rA   rB   r]   œ   rY   zLegacyAPIResponse.status_codeú
httpx2.URLc                 C  rX   r?   )r4   ÚurlrG   rA   rA   rB   r_       rY   zLegacyAPIResponse.urlÚstrc                 C  rX   r?   )r\   ÚmethodrG   rA   rA   rB   ra   ¤   rY   zLegacyAPIResponse.methodÚbytesc                 C  rX   )zŠReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r4   ÚcontentrG   rA   rA   rB   rc   ¨   s   zLegacyAPIResponse.contentc                 C  rX   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r4   ÚtextrG   rA   rA   rB   rd   ±   s   zLegacyAPIResponse.textc                 C  rX   r?   )r4   Úhttp_versionrG   rA   rA   rB   re   ¹   rY   zLegacyAPIResponse.http_versionc                 C  rX   r?   )r4   Ú	is_closedrG   rA   rA   rB   rf   ½   rY   zLegacyAPIResponse.is_closedúdatetime.timedeltac                 C  rX   )zCThe time taken for the complete request/response cycle to complete.)r4   ÚelapsedrG   rA   rA   rB   rh   Á   s   zLegacyAPIResponse.elapsedc                C  sD  |d ur|n| j }t|ƒr|j}|rt|ƒrt|dƒ}t|ƒp |}| jr€|rIt|ƒs4tdt	› dt
› �ƒ‚tt|t|dd�| jtt| jƒ| jd�ƒS | jratt| jt| jƒ| jtt| jƒ| jd�ƒS td| jjƒ}|d u rotƒ ‚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#ƒ }t |¡rït||ƒrï||vrêt$d
ƒ‚tt|ƒS t |¡�rt|t%ƒ�st|t&j%ƒ�rt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- .dd¡ /d¡^}}| 0d¡�s•t1|ƒ�r�z| 2¡ }	W n t3�yw }
 zt4 5dt6|
ƒj7¡ W Y d }
~
nd }
~
ww | jj8|	||d�S | jj9�r’t:|d|› d�|jd�‚|jS | 2¡ }	| jj8|	||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])Úfailure_message)r8   Úresponser9   r<   r0   Útruez+Unexpected state - cast_to is `APIResponse`zASubclasses of HTTP response classes cannot be passed to `cast_to`zVPydantic models must subclass our base model type, e.g. `from openai import BaseModel`zUnsupported type, expected z to be a subclass of z, Ú.zcontent-typeÚ*ú;Újsonz0Could not read JSON from response data due to %s)Údatar8   rj   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rj   ÚmessageÚbody);r*   r   Ú	__value__r   r   r   r/   r   Ú	TypeErrorr   r   r   r&   r    r4   r   r,   r2   r1   r%   Ú_default_stream_clsÚMissingStreamClassErrorr   r`   rd   r5   Úfloatr.   ÚlowerÚinspectÚisclassÚ
issubclassÚHttpxBinaryResponseContentr(   ÚRuntimeErrorr   Ú
ValueErrorr   ÚpydanticÚobjectÚlistÚdictr   Úhttpx2ÚResponserE   rF   ÚsplitÚendswithr   ro   Ú	Exceptionr'   ÚdebugÚtypeÚ__name__Ú_process_response_dataÚ_strict_response_validationr!   )r@   rI   r8   Úoriginr;   rj   Úresponse_typesÚcontent_typeÚ_rp   ÚexcrA   rA   rB   rQ   Æ   sâ   
þ
ùþ
üþ

üþ

ÿþ
ÿÿü
û



ÿ6ÿ
€ÿý

ý	ýzLegacyAPIResponse._parsec                 C  s    d| j › d| jj› d| j› d�S )Nz<APIResponse [ú z] type=ú>)r]   r4   Úreason_phraser*   rG   rA   rA   rB   Ú__repr__Y  s    zLegacyAPIResponse.__repr__)r7   r3   r8   r)   r9   r+   r:   r.   r;   r0   r<   r"   r6   r5   r=   r>   ©r=   rD   )rI   rJ   r=   r&   )r=   r%   )rI   rO   r=   rP   )r=   rW   )r=   rZ   )r=   r5   )r=   r^   ©r=   r`   ©r=   rb   )r=   r.   )r=   rg   )rŠ   Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rC   ÚpropertyrH   r   rL   rE   r\   r]   r_   ra   rc   rd   re   rf   rh   rQ   r   r•   rA   rA   rA   rB   r(   .   sX   
 ÷/ r(   c                      s   e Zd Zd‡ fdd„Z‡  ZS )rv   r=   r>   c                   s   t ƒ  d¡ d S )NzzThe `stream` argument was set to `True` but the `stream_cls` argument was not given. See `openai._streaming` for reference)ÚsuperrC   rG   ©Ú	__class__rA   rB   rC   _  s   ÿz MissingStreamClassError.__init__©r=   r>   )rŠ   r™   rš   rC   Ú__classcell__rA   rA   rŸ   rB   rv   ^  s    rv   Ú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_headersrk   ©r   r   rF   r   r(   r%   ©r¨   rª   r®   ©r£   rA   rB   Úwrappedj  s   z(to_raw_response_wrapper.<locals>.wrappedN©r¨   r©   rª   r«   r=   r¬   ©Ú	functoolsÚwraps©r£   r²   rA   r±   rB   Úto_raw_response_wrappere  ó   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±   rA   rB   r²   {  s
   €z.async_to_raw_response_wrapper.<locals>.wrappedNr³   r´   r·   rA   r±   rB   Úasync_to_raw_response_wrapperv  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|   r3   rj   r=   r>   c                 C  s
   || _ d S r?   )rj   )r@   rj   rA   rA   rB   rC   Š  ó   
z#HttpxBinaryResponseContent.__init__rb   c                 C  rX   r?   )rj   rc   rG   rA   rA   rB   rc   �  rY   z"HttpxBinaryResponseContent.contentr`   c                 C  rX   r?   )rj   rd   rG   rA   rA   rB   rd   ‘  rY   zHttpxBinaryResponseContent.textrD   c                 C  rX   r?   )rj   ÚencodingrG   rA   rA   rB   r¾   •  rY   z#HttpxBinaryResponseContent.encodingc                 C  rX   r?   )rj   Úcharset_encodingrG   rA   rA   rB   r¿   ™  rY   z+HttpxBinaryResponseContent.charset_encodingrª   r   c                 K  s   | j jdi |¤ŽS )NrA   )rj   ro   )r@   rª   rA   rA   rB   ro   �  s   zHttpxBinaryResponseContent.jsonc                 C  ó
   | j  ¡ S r?   )rj   ÚreadrG   rA   rA   rB   rÁ      r½   zHttpxBinaryResponseContent.readNÚ
chunk_sizeú
int | NoneúIterator[bytes]c                 C  ó   | j  |¡S r?   )rj   Ú
iter_bytes©r@   rÂ   rA   rA   rB   rÆ   £  ó   z%HttpxBinaryResponseContent.iter_bytesúIterator[str]c                 C  rÅ   r?   )rj   Ú	iter_textrÇ   rA   rA   rB   rÊ   ¦  rÈ   z$HttpxBinaryResponseContent.iter_textc                 C  rÀ   r?   )rj   Ú
iter_linesrG   rA   rA   rB   rË   ©  r½   z%HttpxBinaryResponseContent.iter_linesc                 C  rÅ   r?   )rj   Úiter_rawrÇ   rA   rA   rB   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©Úopenrj   rÆ   Úwrite)r@   rÍ   Úfrp   rA   rA   rB   Ú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 instead)rÂ   c                C  sN   t |dd��}| j |¡D ]}| |¡ qW d   ƒ d S 1 s w   Y  d S ©NrÏ   rÐ   rÒ   )r@   rÍ   rÂ   rÕ   rp   rA   rA   rB   Ústream_to_file¿  s
   	ÿ"ÿz)HttpxBinaryResponseContent.stream_to_filec                 C  rÀ   r?   )rj   ÚcloserG   rA   rA   rB   rÙ   Ì  r½   z HttpxBinaryResponseContent.closec                 Ã  ó   �| j  ¡ I d H S r?   )rj   ÚareadrG   rA   rA   rB   rÛ   Ï  ó   €z HttpxBinaryResponseContent.areadúAsyncIterator[bytes]c                 Ã  ó   �| j  |¡S r?   )rj   Úaiter_bytesrÇ   rA   rA   rB   rß   Ò  ó   €z&HttpxBinaryResponseContent.aiter_bytesúAsyncIterator[str]c                 Ã  rÞ   r?   )rj   Ú
aiter_textrÇ   rA   rA   rB   râ   Õ  rà   z%HttpxBinaryResponseContent.aiter_textc                 Ã  s   �| j  ¡ S r?   )rj   Úaiter_linesrG   rA   rA   rB   rã   Ø  s   €
z&HttpxBinaryResponseContent.aiter_linesc                 Ã  rÞ   r?   )rj   Ú	aiter_rawrÇ   rA   rA   rB   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Ó   rj   rß   rÔ   )r@   rÍ   rÂ   ÚpathrÕ   rp   rA   rA   rB   Úastream_to_fileÞ  s   €
	ÿ.ÿz*HttpxBinaryResponseContent.astream_to_filec                 Ã  rÚ   r?   )rj   ÚacloserG   rA   rA   rB   ré   ì  rÜ   z!HttpxBinaryResponseContent.aclose)rj   r3   r=   r>   r˜   r—   r–   )rª   r   r=   r   r?   )rÂ   rÃ   r=   rÄ   )rÂ   rÃ   r=   rÉ   )r=   rÉ   )rÍ   rÎ   r=   r>   )rÍ   rÎ   rÂ   rÃ   r=   r>   r¡   )rÂ   rÃ   r=   rÝ   )rÂ   rÃ   r=   rá   )r=   rá   )rŠ   r™   rš   rœ   rC   r�   rc   rd   r¾   r¿   ro   rÁ   rÆ   rÊ   rË   rÌ   rÖ   r   rØ   rÙ   rÛ   rß   râ   rã   rä   rè   ré   rA   rA   rA   rB   r|   ‡  sF   
 




ÿü



ÿür|   )r£   r¤   r=   r¥   )r£   rº   r=   r»   )AÚ
__future__r   Úosry   Ú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   Ú_httpx2r   Ú_modelsr   r   r   Ú
_constantsr   Ú
_streamingr   r   r   r    Ú_exceptionsr!   r"   Ú_base_clientr#   r$   r%   r&   Ú	getLoggerrŠ   r'   rœ   r(   rt   rv   r¸   r¼   r|   rA   rA   rA   rB   Ú<module>   s@    0  2

