a
    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rd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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)	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                
   @  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   )r1   None)rawcast_toclientstream
stream_clsoptionsr1   returnc                C  s4   || _ || _i | _|| _|| _|| _|| _|| _d S N)r&   r'   r(   r*   r,   r-   r/   r1   )selfr3   r4   r5   r6   r7   r8   r1    r<   P/var/www/ai-form-bot/venv/lib/python3.9/site-packages/openai/_legacy_response.py__init__F   s    zLegacyAPIResponse.__init__
str | Noner9   c                 C  s   | j jdS )Nzx-request-id)r/   headersgetr;   r<   r<   r=   
request_idZ   s    zLegacyAPIResponse.request_idztype[_T]r#   )tor9   c                C  s   d S r:   r<   )r;   rE   r<   r<   r=   parse^   s    zLegacyAPIResponse.parser"   c                 C  s   d S r:   r<   rC   r<   r<   r=   rF   a   s    NrE   ztype[_T] | NonezR | _Tc                C  s\   |dur|n| j }| j|}|dur*|S | j|d}t| jjrN| 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`
        NrG   )r&   r(   rB   _parser   r-   Zpost_parser)r;   rE   	cache_keycachedparsedr<   r<   r=   rF   d   s     
zhttpx.Headersc                 C  s   | j jS r:   )r/   rA   rC   r<   r<   r=   rA      s    zLegacyAPIResponse.headerszhttpx.Requestc                 C  s   | j jS r:   )r/   requestrC   r<   r<   r=   http_request   s    zLegacyAPIResponse.http_requestc                 C  s   | j jS r:   )r/   status_coderC   r<   r<   r=   rN      s    zLegacyAPIResponse.status_codez	httpx.URLc                 C  s   | j jS r:   )r/   urlrC   r<   r<   r=   rO      s    zLegacyAPIResponse.urlstrc                 C  s   | j jS r:   )rM   methodrC   r<   r<   r=   rQ      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.
        )r/   contentrC   r<   r<   r=   rS      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.
        )r/   textrC   r<   r<   r=   rT      s    zLegacyAPIResponse.textc                 C  s   | j jS r:   )r/   http_versionrC   r<   r<   r=   rU      s    zLegacyAPIResponse.http_versionc                 C  s   | j jS r:   )r/   	is_closedrC   r<   r<   r=   rV      s    zLegacyAPIResponse.is_closedzdatetime.timedeltac                 C  s   | j jS )zCThe time taken for the complete request/response cycle to complete.)r/   elapsedrC   r<   r<   r=   rW      s    zLegacyAPIResponse.elapsedc          
      C  s4  |rt |rt|d}| jr|rbt|s<t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| j
tt| jdS |d ur|n| j}t |rt|d}|tu rttd S | j
}|tkrtt|jS |tkr8ttt|jS |tkrRt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rn|t$urn|t%urn|t&urnt|t!snt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*|rz|+ }W n8 t,y }	 zt-.dt/|	|	 W Y d }	~	nd }	~	0 0 | 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])Zfailure_message)r4   responser5   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)datar4   rX   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rX   messagebody)3r   r   r*   r   	TypeErrorr   r   r   r#   r   r/   r   r'   r,   r"   Z_default_stream_clsMissingStreamClassErrorr&   r   rP   rT   r0   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr%   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rA   rB   splitr   json	Exceptionr$   debugtypeZ_process_response_dataZ_strict_response_validationr   )
r;   rE   r7   r4   rX   origincontent_type_r\   excr<   r<   r=   rH      s    


	
	







&6

(

	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)rN   r/   Zreason_phraser&   rC   r<   r<   r=   __repr__F  s    zLegacyAPIResponse.__repr__)__name__
__module____qualname____doc____annotations__r>   propertyrD   r   rF   rA   rM   rN   rO   rQ   rS   rT   rU   rV   rW   rH   r   ry   r<   r<   r<   r=   r%   -   sT   
 , r%   c                      s"   e Zd Zdd fddZ  ZS )r`   r2   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>   rC   	__class__r<   r=   r>   L  s    z MissingStreamClassError.__init__)rz   r{   r|   r>   __classcell__r<   r<   r   r=   r`   K  s   r`   zCallable[P, R]z!Callable[P, LegacyAPIResponse[R]])funcr9   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kwargsr9   c                    s@   i t t|dpi }d|t< ||d< t tt  | i |S Nextra_headerstruer   r   rB   r   r%   r"   r   r   r   r   r<   r=   wrappedW  s    z(to_raw_response_wrapper.<locals>.wrapped	functoolswrapsr   r   r<   r   r=   to_raw_response_wrapperR  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   h  s    z.async_to_raw_response_wrapper.<locals>.wrappedr   r   r<   r   r=   async_to_raw_response_wrapperc  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 )Gre   r.   rX   r2   )rX   r9   c                 C  s
   || _ d S r:   )rX   )r;   rX   r<   r<   r=   r>   w  s    z#HttpxBinaryResponseContent.__init__rR   r@   c                 C  s   | j jS r:   )rX   rS   rC   r<   r<   r=   rS   z  s    z"HttpxBinaryResponseContent.contentrP   c                 C  s   | j jS r:   )rX   rT   rC   r<   r<   r=   rT   ~  s    zHttpxBinaryResponseContent.textr?   c                 C  s   | j jS r:   )rX   encodingrC   r<   r<   r=   r     s    z#HttpxBinaryResponseContent.encodingc                 C  s   | j jS r:   )rX   charset_encodingrC   r<   r<   r=   r     s    z+HttpxBinaryResponseContent.charset_encodingr   )r   r9   c                 K  s   | j jf i |S r:   )rX   ro   )r;   r   r<   r<   r=   ro     s    zHttpxBinaryResponseContent.jsonc                 C  s
   | j  S r:   )rX   readrC   r<   r<   r=   r     s    zHttpxBinaryResponseContent.readNz
int | NonezIterator[bytes])
chunk_sizer9   c                 C  s   | j |S r:   )rX   
iter_bytesr;   r   r<   r<   r=   r     s    z%HttpxBinaryResponseContent.iter_byteszIterator[str]c                 C  s   | j |S r:   )rX   	iter_textr   r<   r<   r=   r     s    z$HttpxBinaryResponseContent.iter_textc                 C  s
   | j  S r:   )rX   
iter_linesrC   r<   r<   r=   r     s    z%HttpxBinaryResponseContent.iter_linesc                 C  s   | j |S r:   )rX   iter_rawr   r<   r<   r=   r     s    z#HttpxBinaryResponseContent.iter_rawzstr | os.PathLike[str])filer9   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openrX   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   r9   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:   )rX   closerC   r<   r<   r=   r     s    z HttpxBinaryResponseContent.closec                   s   | j  I d H S r:   )rX   areadrC   r<   r<   r=   r     s    z HttpxBinaryResponseContent.areadzAsyncIterator[bytes]c                   s   | j |S r:   )rX   aiter_bytesr   r<   r<   r=   r     s    z&HttpxBinaryResponseContent.aiter_byteszAsyncIterator[str]c                   s   | j |S r:   )rX   
aiter_textr   r<   r<   r=   r     s    z%HttpxBinaryResponseContent.aiter_textc                   s
   | j  S r:   )rX   aiter_linesrC   r<   r<   r=   r     s    z&HttpxBinaryResponseContent.aiter_linesc                   s   | j |S r:   )rX   	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   rX   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:   )rX   acloserC   r<   r<   r=   r     s    z!HttpxBinaryResponseContent.aclose)N)N)N)N)N)N)rz   r{   r|   r~   r>   r   rS   rT   r   r   ro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r<   r<   r=   re   t  sD   

re   )=
__future__r   osrb   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   rg   rj   _typesr   Z_utilsr   r   r   Z_modelsr   r   
_constantsr   Z
_streamingr   r   r   r   _exceptionsr   r   Z_base_clientr    r!   r"   r#   	getLoggerrz   r$   r~   r%   r_   r`   r   r   re   r<   r<   r<   r=   <module>   s<   0   