o
    ØRWjö?  ã                   @  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. er�ddl"m/Z/ ddl0m1Z1 edƒZ2edƒZ3edƒZ4e 5e6¡Z7de8d< G dd„ dee3 ƒZ9G 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)Ú	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úhttpx.ResponseÚhttp_responseÚintÚretries_takenr   )r5   ÚrawÚcast_toÚclientÚstreamÚ
stream_clsÚoptionsÚreturnÚNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S ©N)r)   r+   r,   r.   r0   r1   r3   r5   )Úselfr6   r7   r8   r9   r:   r;   r5   © r@   úf/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/openai/_legacy_response.pyÚ__init__F   s   
zLegacyAPIResponse.__init__ú
str | Nonec                 C  s   | j j d¡S )Nzx-request-id)r3   ÚheadersÚget©r?   r@   r@   rA   Ú
request_idZ   s   zLegacyAPIResponse.request_idÚtoútype[_T]r%   c                C  ó   d S r>   r@   )r?   rH   r@   r@   rA   Úparse^   ó   zLegacyAPIResponse.parser$   c                 C  rJ   r>   r@   rF   r@   r@   rA   rK   a   rL   N©rH   ú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`
          - `httpx.Response`
        NrM   )r)   r,   rE   Ú_parser   r1   Úpost_parserÚ
isinstancer   r   rG   r   r$   )r?   rH   Ú	cache_keyÚcachedÚparsedr@   r@   rA   rK   d   s    


úhttpx.Headersc                 C  ó   | j jS r>   )r3   rD   rF   r@   r@   rA   rD   “   ó   zLegacyAPIResponse.headersúhttpx.Requestc                 C  rW   r>   )r3   ÚrequestrF   r@   r@   rA   Úhttp_request—   rX   zLegacyAPIResponse.http_requestc                 C  rW   r>   )r3   Ústatus_coderF   r@   r@   rA   r\   ›   rX   zLegacyAPIResponse.status_codeú	httpx.URLc                 C  rW   r>   )r3   ÚurlrF   r@   r@   rA   r^   Ÿ   rX   zLegacyAPIResponse.urlÚstrc                 C  rW   r>   )r[   ÚmethodrF   r@   r@   rA   r`   £   rX   zLegacyAPIResponse.methodÚbytesc                 C  rW   )zŠReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r3   ÚcontentrF   r@   r@   rA   rb   §   s   zLegacyAPIResponse.contentc                 C  rW   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r3   ÚtextrF   r@   r@   rA   rc   °   s   zLegacyAPIResponse.textc                 C  rW   r>   )r3   Úhttp_versionrF   r@   r@   rA   rd   ¸   rX   zLegacyAPIResponse.http_versionc                 C  rW   r>   )r3   Ú	is_closedrF   r@   r@   rA   re   ¼   rX   zLegacyAPIResponse.is_closedúdatetime.timedeltac                 C  rW   )zCThe time taken for the complete request/response cycle to complete.)r3   ÚelapsedrF   r@   r@   rA   rg   À   s   zLegacyAPIResponse.elapsedc          
      C  sB  |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 |¡rît|t#j$ƒrî|t#j$k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¡^}}| /d¡�s”t0|ƒ�r€z| 1¡ }W n t2�yv }	 zt3 4dt5|	ƒ|	¡ W Y d }	~	nd }	~	ww | jj6|||d�S | jj7�r‘t8|d|› d�|jd�‚|jS | 1¡ }| jj6|||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)r7   Úresponser8   r;   r/   Útruez+Unexpected state - cast_to is `APIResponse`z:Subclasses of httpx.Response 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Ú*ú;Újsonz5Could not read JSON from response data due to %s - %s)Údatar7   ri   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)ri   ÚmessageÚbody)9r)   r   Ú	__value__r   r   r   r.   r   Ú	TypeErrorr   r   r   r%   r   r3   r   r+   r1   r0   r$   Ú_default_stream_clsÚMissingStreamClassErrorr   r_   rc   r4   Úfloatr-   ÚlowerÚinspectÚisclassÚ
issubclassÚHttpxBinaryResponseContentr'   ÚRuntimeErrorÚhttpxÚResponseÚ
ValueErrorr   ÚpydanticÚobjectÚlistÚdictr   rD   rE   ÚsplitÚendswithr   rn   Ú	Exceptionr&   ÚdebugÚtypeÚ_process_response_dataÚ_strict_response_validationr    )
r?   rH   r7   Úoriginr:   ri   Úcontent_typeÚ_ro   Úexcr@   r@   rA   rP   Å   sà   
þ
ùþ
üþ

üþ

ÿ
þ

ÿÿü
û



ÿ6ÿ
€ÿý

ý	ýzLegacyAPIResponse._parsec                 C  s    d| j › d| jj› d| j› d�S )Nz<APIResponse [ú z] type=ú>)r\   r3   Úreason_phraser)   rF   r@   r@   rA   Ú__repr__W  s    zLegacyAPIResponse.__repr__)r6   r2   r7   r(   r8   r*   r9   r-   r:   r/   r;   r!   r5   r4   r<   r=   ©r<   rC   )rH   rI   r<   r%   )r<   r$   )rH   rN   r<   rO   )r<   rV   )r<   rY   )r<   r4   )r<   r]   ©r<   r_   ©r<   ra   )r<   r-   )r<   rf   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rB   ÚpropertyrG   r   rK   rD   r[   r\   r^   r`   rb   rc   rd   re   rg   rP   r   r’   r@   r@   r@   rA   r'   -   sX   
 ÷/ r'   c                      s   e Zd Zd‡ fdd„Z‡  ZS )ru   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)ÚsuperrB   rF   ©Ú	__class__r@   rA   rB   ]  s   ÿz MissingStreamClassError.__init__©r<   r=   )r–   r—   r˜   rB   Ú__classcell__r@   r@   r�   rA   ru   \  s    ru   Ú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_headersrj   ©r   r   rE   r   r'   r$   ©r¦   r¨   r¬   ©r¡   r@   rA   Úwrappedh  s   z(to_raw_response_wrapper.<locals>.wrappedN©r¦   r§   r¨   r©   r<   rª   ©Ú	functoolsÚwraps©r¡   r°   r@   r¯   rA   Úto_raw_response_wrapperc  ó   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¯   r@   rA   r°   y  s
   €z.async_to_raw_response_wrapper.<locals>.wrappedNr±   r²   rµ   r@   r¯   rA   Úasync_to_raw_response_wrappert  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{   r2   ri   r<   r=   c                 C  s
   || _ d S r>   )ri   )r?   ri   r@   r@   rA   rB   ˆ  ó   
z#HttpxBinaryResponseContent.__init__ra   c                 C  rW   r>   )ri   rb   rF   r@   r@   rA   rb   ‹  rX   z"HttpxBinaryResponseContent.contentr_   c                 C  rW   r>   )ri   rc   rF   r@   r@   rA   rc   �  rX   zHttpxBinaryResponseContent.textrC   c                 C  rW   r>   )ri   ÚencodingrF   r@   r@   rA   r¼   “  rX   z#HttpxBinaryResponseContent.encodingc                 C  rW   r>   )ri   Úcharset_encodingrF   r@   r@   rA   r½   —  rX   z+HttpxBinaryResponseContent.charset_encodingr¨   r   c                 K  s   | j jdi |¤ŽS )Nr@   )ri   rn   )r?   r¨   r@   r@   rA   rn   ›  s   zHttpxBinaryResponseContent.jsonc                 C  ó
   | j  ¡ S r>   )ri   ÚreadrF   r@   r@   rA   r¿   ž  r»   zHttpxBinaryResponseContent.readNÚ
chunk_sizeú
int | NoneúIterator[bytes]c                 C  ó   | j  |¡S r>   )ri   Ú
iter_bytes©r?   rÀ   r@   r@   rA   rÄ   ¡  ó   z%HttpxBinaryResponseContent.iter_bytesúIterator[str]c                 C  rÃ   r>   )ri   Ú	iter_textrÅ   r@   r@   rA   rÈ   ¤  rÆ   z$HttpxBinaryResponseContent.iter_textc                 C  r¾   r>   )ri   Ú
iter_linesrF   r@   r@   rA   rÉ   §  r»   z%HttpxBinaryResponseContent.iter_linesc                 C  rÃ   r>   )ri   Úiter_rawrÅ   r@   r@   rA   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©Úopenri   rÄ   Úwrite)r?   rË   Úfro   r@   r@   rA   Ú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Ó   ro   r@   r@   rA   Ústream_to_file½  s
   	ÿ"ÿz)HttpxBinaryResponseContent.stream_to_filec                 C  r¾   r>   )ri   ÚcloserF   r@   r@   rA   r×   Ê  r»   z HttpxBinaryResponseContent.closec                 Ã  ó   �| j  ¡ I d H S r>   )ri   ÚareadrF   r@   r@   rA   rÙ   Í  ó   €z HttpxBinaryResponseContent.areadúAsyncIterator[bytes]c                 Ã  ó   �| j  |¡S r>   )ri   Úaiter_bytesrÅ   r@   r@   rA   rÝ   Ð  ó   €z&HttpxBinaryResponseContent.aiter_bytesúAsyncIterator[str]c                 Ã  rÜ   r>   )ri   Ú
aiter_textrÅ   r@   r@   rA   rà   Ó  rÞ   z%HttpxBinaryResponseContent.aiter_textc                 Ã  s   �| j  ¡ S r>   )ri   Úaiter_linesrF   r@   r@   rA   rá   Ö  s   €
z&HttpxBinaryResponseContent.aiter_linesc                 Ã  rÜ   r>   )ri   Ú	aiter_rawrÅ   r@   r@   rA   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Ñ   ri   rÝ   rÒ   )r?   rË   rÀ   ÚpathrÓ   ro   r@   r@   rA   Úastream_to_fileÜ  s   €
	ÿ.ÿz*HttpxBinaryResponseContent.astream_to_filec                 Ã  rØ   r>   )ri   ÚacloserF   r@   r@   rA   rç   ê  rÚ   z!HttpxBinaryResponseContent.aclose)ri   r2   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š   rB   r›   rb   rc   r¼   r½   rn   r¿   rÄ   rÈ   rÉ   rÊ   rÔ   r   rÖ   r×   rÙ   rÝ   rà   rá   râ   ræ   rç   r@   r@   r@   rA   r{   …  sF   
 




ÿü



ÿür{   )r¡   r¢   r<   r£   )r¡   r¸   r<   r¹   )?Ú
__future__r   Úosrx   Ú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    r!   Ú_base_clientr"   r#   r$   r%   Ú	getLoggerr–   r&   rš   r'   rs   ru   r¶   rº   r{   r@   r@   r@   rA   Ú<module>   s>    0  1

