a
    _gn?                     @  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ddZ<dd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                
   @  s  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ddddd
ddddddZeddddZeddddd Z	ed!dd"d Z	d#d$d%d&dd'd Z	ed(dd)d*Z
ed+dd,d-Zeddd.d/Zed0dd1d2Zed3dd4d5Zed6dd7d8Zed3dd9d:Zed3dd;d<Zeddd=d>Zed?dd@dAZd#d$d%d&ddBdCZed3ddDdEZd#S )F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.
    ztype[R]_cast_toz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   None)rawcast_toclientstream
stream_clsoptionsr3   returnc                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r(   r)   r*   r,   r.   r/   r1   r3   )selfr5   r6   r7   r8   r9   r:   r3    r>   e/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/openai/_legacy_response.py__init__F   s    zLegacyAPIResponse.__init__
str | Noner;   c                 C  s   | j jdS )Nzx-request-id)r1   headersgetr=   r>   r>   r?   
request_idZ   s    zLegacyAPIResponse.request_idztype[_T]r%   )tor;   c                C  s   d S r<   r>   )r=   rG   r>   r>   r?   parse^   s    zLegacyAPIResponse.parser$   c                 C  s   d S r<   r>   rE   r>   r>   r?   rH   a   s    NrG   ztype[_T] | NonezR | _Tc                C  sx   |dur|n| j }| j|}|dur*|S | j|d}t| jjrN| j|}t|trdt	|| 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`
        NrI   )r(   r*   rD   _parser   r/   Zpost_parser
isinstancer   r   rF   r   r$   )r=   rG   	cache_keycachedparsedr>   r>   r?   rH   d   s     

zhttpx.Headersc                 C  s   | j jS r<   )r1   rC   rE   r>   r>   r?   rC      s    zLegacyAPIResponse.headerszhttpx.Requestc                 C  s   | j jS r<   )r1   requestrE   r>   r>   r?   http_request   s    zLegacyAPIResponse.http_requestc                 C  s   | j jS r<   )r1   status_coderE   r>   r>   r?   rQ      s    zLegacyAPIResponse.status_codez	httpx.URLc                 C  s   | j jS r<   )r1   urlrE   r>   r>   r?   rR      s    zLegacyAPIResponse.urlstrc                 C  s   | j jS r<   )rP   methodrE   r>   r>   r?   rT      s    zLegacyAPIResponse.methodbytesc                 C  s   | j jS )zReturn the binary response content.

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

        NOTE: this will be turned into a method in the next major version.
        )r1   textrE   r>   r>   r?   rW      s    zLegacyAPIResponse.textc                 C  s   | j jS r<   )r1   http_versionrE   r>   r>   r?   rX      s    zLegacyAPIResponse.http_versionc                 C  s   | j jS r<   )r1   	is_closedrE   r>   r>   r?   rY      s    zLegacyAPIResponse.is_closedzdatetime.timedeltac                 C  s   | j jS )zCThe time taken for the complete request/response cycle to complete.)r1   elapsedrE   r>   r>   r?   rZ      s    zLegacyAPIResponse.elapsedc          
      C  sJ  |d ur|n| j }t|r |j}|r6t|r6t|d}t|p@|}| jr|rt|sht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 rttd S | j}|tkr$tt|jS |tkr>ttt|jS |tkrXttt|jS |tkrvtt|j dkS t|rt|trtt||S |t krt!d	t|rt|t"j#r|t"j#krt$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rt|t%st!d| dt% dt) dt( dt* dt dt dt"j# d|j+,dd-d^}}|dkr0t.|rz|/ }W n8 t0y }	 zt12dt3|	|	 W Y d }	~	nd }	~	0 0 | jj4|||dS | jj5r*t6|d| d|jd|jS |/ }| jj4|||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])Zfailure_message)r6   responser7   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*;zapplication/jsonz5Could not read JSON from response data due to %s - %s)datar6   r[   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)r[   messagebody)7r(   r   Z	__value__r   r   r   r,   r   	TypeErrorr   r   r   r%   r   r1   r   r)   r.   r$   Z_default_stream_clsMissingStreamClassErrorr   rS   rW   r2   floatr+   lowerinspectisclass
issubclassHttpxBinaryResponseContentr'   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rC   rD   splitr   json	Exceptionr&   debugtypeZ_process_response_dataZ_strict_response_validationr    )
r=   rG   r6   originr9   r[   content_type_r`   excr>   r>   r?   rJ      s    


	
	









6

(

	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)rQ   r1   Zreason_phraser(   rE   r>   r>   r?   __repr__T  s    zLegacyAPIResponse.__repr__)__name__
__module____qualname____doc____annotations__r@   propertyrF   r   rH   rC   rP   rQ   rR   rT   rV   rW   rX   rY   rZ   rJ   r   r~   r>   r>   r>   r?   r'   -   sT   
 / r'   c                      s"   e Zd Zdd fddZ  ZS )rd   r4   rB   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@   rE   	__class__r>   r?   r@   Z  s    z MissingStreamClassError.__init__)r   r   r   r@   __classcell__r>   r>   r   r?   rd   Y  s   rd   zCallable[P, R]z!Callable[P, LegacyAPIResponse[R]])funcr;   c                   s$   t  ddd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.
    P.argsP.kwargsLegacyAPIResponse[R]argskwargsr;   c                    s@   i t t|dpi }d|t< ||d< t tt  | i |S Nextra_headersr\   r   r   rD   r   r'   r$   r   r   r   r   r>   r?   wrappede  s    z(to_raw_response_wrapper.<locals>.wrapped	functoolswrapsr   r   r>   r   r?   to_raw_response_wrapper`  s    r   zCallable[P, Awaitable[R]]z,Callable[P, Awaitable[LegacyAPIResponse[R]]]c                   s$   t  dddd fdd}|S )r   r   r   r   r   c                    sF   i t t|dpi }d|t< ||d< t tt  | i |I d H S r   r   r   r   r>   r?   r   v  s    z.async_to_raw_response_wrapper.<locals>.wrappedr   r   r>   r   r?   async_to_raw_response_wrapperq  s    r   c                   @  s  e Zd ZU ded< dddddZeddd	d
ZeddddZeddddZeddddZ	dddddZ
ddddZdAdddddZdBdddd d!Zddd"d#ZdCdddd$d%Zd&dd'd(d)Zed*dd+d&ddd,d-d.Zddd/d0Zddd1d2ZdDdd3dd4d5ZdEdd6dd7d8Zd6dd9d:ZdFdd3dd;d<Zed*dd+d&ddd,d=d>Zddd?d@ZdS )Grj   r0   r[   r4   )r[   r;   c                 C  s
   || _ d S r<   )r[   )r=   r[   r>   r>   r?   r@     s    z#HttpxBinaryResponseContent.__init__rU   rB   c                 C  s   | j jS r<   )r[   rV   rE   r>   r>   r?   rV     s    z"HttpxBinaryResponseContent.contentrS   c                 C  s   | j jS r<   )r[   rW   rE   r>   r>   r?   rW     s    zHttpxBinaryResponseContent.textrA   c                 C  s   | j jS r<   )r[   encodingrE   r>   r>   r?   r     s    z#HttpxBinaryResponseContent.encodingc                 C  s   | j jS r<   )r[   charset_encodingrE   r>   r>   r?   r     s    z+HttpxBinaryResponseContent.charset_encodingr   )r   r;   c                 K  s   | j jf i |S r<   )r[   rt   )r=   r   r>   r>   r?   rt     s    zHttpxBinaryResponseContent.jsonc                 C  s
   | j  S r<   )r[   readrE   r>   r>   r?   r     s    zHttpxBinaryResponseContent.readNz
int | NonezIterator[bytes])
chunk_sizer;   c                 C  s   | j |S r<   )r[   
iter_bytesr=   r   r>   r>   r?   r     s    z%HttpxBinaryResponseContent.iter_byteszIterator[str]c                 C  s   | j |S r<   )r[   	iter_textr   r>   r>   r?   r     s    z$HttpxBinaryResponseContent.iter_textc                 C  s
   | j  S r<   )r[   
iter_linesrE   r>   r>   r?   r     s    z%HttpxBinaryResponseContent.iter_linesc                 C  s   | j |S r<   )r[   iter_rawr   r>   r>   r?   r     s    z#HttpxBinaryResponseContent.iter_rawzstr | os.PathLike[str])filer;   c                 C  sJ   t |dd*}| j D ]}|| qW d   n1 s<0    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openr[   r   write)r=   r   fr`   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   )r   r   r;   c                C  sL   t |dd,}| j|D ]}|| qW d    n1 s>0    Y  d S Nr   r   r   )r=   r   r   r   r`   r>   r>   r?   stream_to_file  s    	z)HttpxBinaryResponseContent.stream_to_filec                 C  s
   | j  S r<   )r[   closerE   r>   r>   r?   r     s    z HttpxBinaryResponseContent.closec                   s   | j  I d H S r<   )r[   areadrE   r>   r>   r?   r     s    z HttpxBinaryResponseContent.areadzAsyncIterator[bytes]c                   s   | j |S r<   )r[   aiter_bytesr   r>   r>   r?   r     s    z&HttpxBinaryResponseContent.aiter_byteszAsyncIterator[str]c                   s   | j |S r<   )r[   
aiter_textr   r>   r>   r?   r     s    z%HttpxBinaryResponseContent.aiter_textc                   s
   | j  S r<   )r[   aiter_linesrE   r>   r>   r?   r     s    z&HttpxBinaryResponseContent.aiter_linesc                   s   | j |S r<   )r[   	aiter_rawr   r>   r>   r?   r     s    z$HttpxBinaryResponseContent.aiter_rawc             	     s   t |}|jddI d H 4 I d H B}| j|2 z3 d H W }||I d H  q26 W d   I d H  q|1 I d H sr0    Y  d S r   )anyioPathr   r[   r   r   )r=   r   r   pathr   r`   r>   r>   r?   astream_to_file  s    	
z*HttpxBinaryResponseContent.astream_to_filec                   s   | j  I d H S r<   )r[   acloserE   r>   r>   r?   r     s    z!HttpxBinaryResponseContent.aclose)N)N)N)N)N)N)r   r   r   r   r@   r   rV   rW   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r>   r>   r>   r?   rj     sD   

rj   )?
__future__r   osrg   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   Ztyping_extensionsr   r   r   r   r   r   rl   ro   _typesr   _utilsr   r   r   r   Z_modelsr   r   r   
_constantsr   Z
_streamingr   r   r   r   _exceptionsr    r!   Z_base_clientr"   r#   r$   r%   	getLoggerr   r&   r   r'   rc   rd   r   r   rj   r>   r>   r>   r?   <module>   s<   0  .