o
    {$f=                     @  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rd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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)	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                   @  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   )r3   rawcast_toclientstream
stream_clsoptionsreturnNonec                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r'   r)   r*   r,   r.   r/   r1   r3   )selfr4   r5   r6   r7   r8   r9   r3    r>   M/var/www/html/dev/env/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)r1   headersgetr=   r>   r>   r?   
request_idZ   s   zLegacyAPIResponse.request_idtotype[_T]r#   c                C     d S r<   r>   )r=   rF   r>   r>   r?   parse^      zLegacyAPIResponse.parser"   c                 C  rH   r<   r>   rD   r>   r>   r?   rI   a   rJ   NrF   type[_T] | NoneR | _Tc                C  s\   |dur|n| j }| j|}|dur|S | j|d}t| jjr'| 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 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`
        NrK   )r'   r*   rC   _parser   r/   post_parser)r=   rF   	cache_keycachedparsedr>   r>   r?   rI   d   s    
httpx.Headersc                 C     | j jS r<   )r1   rB   rD   r>   r>   r?   rB         zLegacyAPIResponse.headershttpx.Requestc                 C  rT   r<   )r1   requestrD   r>   r>   r?   http_request   rU   zLegacyAPIResponse.http_requestc                 C  rT   r<   )r1   status_coderD   r>   r>   r?   rY      rU   zLegacyAPIResponse.status_code	httpx.URLc                 C  rT   r<   )r1   urlrD   r>   r>   r?   r[      rU   zLegacyAPIResponse.urlstrc                 C  rT   r<   )rX   methodrD   r>   r>   r?   r]      rU   zLegacyAPIResponse.methodbytesc                 C  rT   )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r1   contentrD   r>   r>   r?   r_      s   zLegacyAPIResponse.contentc                 C  rT   )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r1   textrD   r>   r>   r?   r`      s   zLegacyAPIResponse.textc                 C  rT   r<   )r1   http_versionrD   r>   r>   r?   ra      rU   zLegacyAPIResponse.http_versionc                 C  rT   r<   )r1   	is_closedrD   r>   r>   r?   rb      rU   zLegacyAPIResponse.is_closeddatetime.timedeltac                 C  rT   )zCThe time taken for the complete request/response cycle to complete.)r1   elapsedrD   r>   r>   r?   rd      s   zLegacyAPIResponse.elapsedc          
      C  s  |rt |rt|d}| jre|r1t|stdt dt tt|t	|dd| j
tt| jdS | jrGtt| jt	| j| j
tt| jdS td| jj}|d u rUt tt|| j| j
tt| jdS |d urk|n| j}t |rwt|d}|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|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#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krt*|rkz|+ }W n t,ya }	 zt-.dt/|	|	 W Y d }	~	nd }	~	ww | 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])failure_message)r5   responser6   r-   z+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*;zapplication/jsonz5Could not read JSON from response data due to %s - %s)datar5   rf   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rf   messagebody)3r   r   r,   r   	TypeErrorr   r   r   r#   r   r1   r   r)   r.   r"   _default_stream_clsMissingStreamClassErrorr'   r   r\   r`   r2   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr%   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rB   rC   splitr   json	Exceptionr$   debugtype_process_response_data_strict_response_validationr   )
r=   rF   r8   r5   rf   origincontent_type_rj   excr>   r>   r?   rN      s   


	
	



 



6



	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)rY   r1   reason_phraser'   rD   r>   r>   r?   __repr__F  s    zLegacyAPIResponse.__repr__)r4   r0   r5   r&   r6   r(   r7   r+   r8   r-   r9   r   r3   r2   r:   r;   r:   rA   )rF   rG   r:   r#   )r:   r"   )rF   rL   r:   rM   )r:   rS   )r:   rV   )r:   r2   )r:   rZ   r:   r\   r:   r^   )r:   r+   )r:   rc   )__name__
__module____qualname____doc____annotations__r@   propertyrE   r   rI   rB   rX   rY   r[   r]   r_   r`   ra   rb   rd   rN   r   r   r>   r>   r>   r?   r%   -   sX   
 , r%   c                      s   e Zd Zd fddZ  ZS )ro   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)superr@   rD   	__class__r>   r?   r@   L  s   z MissingStreamClassError.__init__r:   r;   )r   r   r   r@   __classcell__r>   r>   r   r?   ro   K  s    ro   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_headerstruer   r   rC   r   r%   r"   r   r   r   r   r>   r?   wrappedW  s   z(to_raw_response_wrapper.<locals>.wrappedNr   r   r   r   r:   r   	functoolswrapsr   r   r>   r   r?   to_raw_response_wrapperR     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>   r?   r   h  s
   z.async_to_raw_response_wrapper.<locals>.wrappedNr   r   r   r>   r   r?   async_to_raw_response_wrapperc  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 )Nrt   r0   rf   r:   r;   c                 C  s
   || _ d S r<   )rf   )r=   rf   r>   r>   r?   r@   w     
z#HttpxBinaryResponseContent.__init__r^   c                 C  rT   r<   )rf   r_   rD   r>   r>   r?   r_   z  rU   z"HttpxBinaryResponseContent.contentr\   c                 C  rT   r<   )rf   r`   rD   r>   r>   r?   r`   ~  rU   zHttpxBinaryResponseContent.textrA   c                 C  rT   r<   )rf   encodingrD   r>   r>   r?   r     rU   z#HttpxBinaryResponseContent.encodingc                 C  rT   r<   )rf   charset_encodingrD   r>   r>   r?   r     rU   z+HttpxBinaryResponseContent.charset_encodingr   r   c                 K  s   | j jdi |S )Nr>   )rf   r~   )r=   r   r>   r>   r?   r~     s   zHttpxBinaryResponseContent.jsonc                 C  
   | j  S r<   )rf   readrD   r>   r>   r?   r     r   zHttpxBinaryResponseContent.readN
chunk_size
int | NoneIterator[bytes]c                 C     | j |S r<   )rf   
iter_bytesr=   r   r>   r>   r?   r        z%HttpxBinaryResponseContent.iter_bytesIterator[str]c                 C  r   r<   )rf   	iter_textr   r>   r>   r?   r     r   z$HttpxBinaryResponseContent.iter_textc                 C  r   r<   )rf   
iter_linesrD   r>   r>   r?   r     r   z%HttpxBinaryResponseContent.iter_linesc                 C  r   r<   )rf   iter_rawr   r>   r>   r?   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openrf   r   write)r=   r   frj   r>   r>   r?   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   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   rj   r>   r>   r?   stream_to_file  s
   	"z)HttpxBinaryResponseContent.stream_to_filec                 C  r   r<   )rf   closerD   r>   r>   r?   r     r   z HttpxBinaryResponseContent.closec                      | j  I d H S r<   )rf   areadrD   r>   r>   r?   r        z HttpxBinaryResponseContent.areadAsyncIterator[bytes]c                      | j |S r<   )rf   aiter_bytesr   r>   r>   r?   r        z&HttpxBinaryResponseContent.aiter_bytesAsyncIterator[str]c                   r   r<   )rf   
aiter_textr   r>   r>   r?   r     r   z%HttpxBinaryResponseContent.aiter_textc                   s   | j  S r<   )rf   aiter_linesrD   r>   r>   r?   r     s   
z&HttpxBinaryResponseContent.aiter_linesc                   r   r<   )rf   	aiter_rawr   r>   r>   r?   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   rf   r   r   )r=   r   r   pathr   rj   r>   r>   r?   astream_to_file  s   
	.z*HttpxBinaryResponseContent.astream_to_filec                   r   r<   )rf   acloserD   r>   r>   r?   r     r   z!HttpxBinaryResponseContent.aclose)rf   r0   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   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?   rt   t  sF   
 








rt   )r   r   r:   r   )r   r   r:   r   )=
__future__r   osrq   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   rv   ry   _typesr   _utilsr   r   r   _modelsr   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr   r   _base_clientr    r!   r"   r#   	getLoggerr   r$   r   r%   rm   ro   r   r   rt   r>   r>   r>   r?   <module>   s>    0   

