o
    jC<                     @  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                   @  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< dEddZedFddZedGd dZd!d"dHd%dZedId'd(Z	edJd*d+Z
edKd-d.ZedLd0d1ZedMd3d4ZedNd6d7ZedMd8d9ZedMd:d;ZedOd<d=ZedPd?d@Zd!d"dHdAdBZedMdCdDZd!S )Q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rawcast_toclientstream
stream_clsoptionsreturnNonec                C  s.   || _ || _i | _|| _|| _|| _|| _d S N)r'   r)   r*   r,   r.   r/   r1   )selfr2   r3   r4   r5   r6   r7    r<   ]/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/openai/_legacy_response.py__init__8   s   

zLegacyAPIResponse.__init__totype[_T]r#   c                C     d S r:   r<   )r;   r?   r<   r<   r=   parseJ      zLegacyAPIResponse.parser"   c                 C  rA   r:   r<   r;   r<   r<   r=   rB   N   rC   Nr?   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`
        NrE   )r'   r*   get_parser   r/   post_parser)r;   r?   	cache_keycachedparsedr<   r<   r=   rB   R   s    
httpx.Headersc                 C     | j jS r:   )r1   headersrD   r<   r<   r=   rP   ~      zLegacyAPIResponse.headershttpx.Requestc                 C  rO   r:   )r1   requestrD   r<   r<   r=   http_request   rQ   zLegacyAPIResponse.http_requestintc                 C  rO   r:   )r1   status_coderD   r<   r<   r=   rV      rQ   zLegacyAPIResponse.status_code	httpx.URLc                 C  rO   r:   )r1   urlrD   r<   r<   r=   rX      rQ   zLegacyAPIResponse.urlstrc                 C  rO   r:   )rT   methodrD   r<   r<   r=   rZ      rQ   zLegacyAPIResponse.methodbytesc                 C  rO   )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  rO   )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  rO   r:   )r1   http_versionrD   r<   r<   r=   r^      rQ   zLegacyAPIResponse.http_versionc                 C  rO   r:   )r1   	is_closedrD   r<   r<   r=   r_      rQ   zLegacyAPIResponse.is_closeddatetime.timedeltac                 C  rO   )zCThe time taken for the complete request/response cycle to complete.)r1   elapsedrD   r<   r<   r=   ra      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)r3   responser4   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)datar3   rc   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rc   messagebody)3r   r   r,   r   	TypeErrorr   r   r   r#   r   r1   r   r)   r.   r"   _default_stream_clsMissingStreamClassErrorr'   r   rY   r]   rU   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr%   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rP   rH   splitr   json	Exceptionr$   debugtype_process_response_data_strict_response_validationr   )
r;   r?   r6   r3   rc   origincontent_type_rg   excr<   r<   r=   rI      s   


	
	



 



6



	zLegacyAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)rV   r1   reason_phraser'   rD   r<   r<   r=   __repr__4  s    zLegacyAPIResponse.__repr__)r2   r0   r3   r&   r4   r(   r5   r+   r6   r-   r7   r   r8   r9   )r?   r@   r8   r#   )r8   r"   )r?   rF   r8   rG   )r8   rN   )r8   rR   )r8   rU   )r8   rW   r8   rY   r8   r[   )r8   r+   )r8   r`   )__name__
__module____qualname____doc____annotations__r>   r   rB   propertyrP   rT   rV   rX   rZ   r\   r]   r^   r_   ra   rI   r   r   r<   r<   r<   r=   r%   "   sN   
 
, r%   c                      s   e Zd Zd fddZ  ZS )rl   r8   r9   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>   :  s   z MissingStreamClassError.__init__r8   r9   )r   r   r   r>   __classcell__r<   r<   r   r=   rl   9  s    rl   funcCallable[P, R]r8   !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.kwargsr8   LegacyAPIResponse[R]c                    s@   i t t|dp
i }d|t< ||d< t tt  | i |S Nextra_headerstruer   r   rH   r   r%   r"   r   r   r   r   r<   r=   wrappedE  s   z(to_raw_response_wrapper.<locals>.wrappedNr   r   r   r   r8   r   	functoolswrapsr   r   r<   r   r=   to_raw_response_wrapper@     r   Callable[P, Awaitable[R]],Callable[P, Awaitable[LegacyAPIResponse[R]]]c                   r   )r   r   r   r   r   r8   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   V  s
   z.async_to_raw_response_wrapper.<locals>.wrappedNr   r   r   r<   r   r=   async_to_raw_response_wrapperQ  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 )Nrq   r0   rc   r8   r9   c                 C  s
   || _ d S r:   )rc   )r;   rc   r<   r<   r=   r>   e     
z#HttpxBinaryResponseContent.__init__r[   c                 C  rO   r:   )rc   r\   rD   r<   r<   r=   r\   h  rQ   z"HttpxBinaryResponseContent.contentrY   c                 C  rO   r:   )rc   r]   rD   r<   r<   r=   r]   l  rQ   zHttpxBinaryResponseContent.text
str | Nonec                 C  rO   r:   )rc   encodingrD   r<   r<   r=   r   p  rQ   z#HttpxBinaryResponseContent.encodingc                 C  rO   r:   )rc   charset_encodingrD   r<   r<   r=   r   t  rQ   z+HttpxBinaryResponseContent.charset_encodingr   r   c                 K  s   | j jdi |S )Nr<   )rc   r{   )r;   r   r<   r<   r=   r{   x  s   zHttpxBinaryResponseContent.jsonc                 C  
   | j  S r:   )rc   readrD   r<   r<   r=   r   {  r   zHttpxBinaryResponseContent.readN
chunk_size
int | NoneIterator[bytes]c                 C     | j |S r:   )rc   
iter_bytesr;   r   r<   r<   r=   r   ~     z%HttpxBinaryResponseContent.iter_bytesIterator[str]c                 C  r   r:   )rc   	iter_textr   r<   r<   r=   r     r   z$HttpxBinaryResponseContent.iter_textc                 C  r   r:   )rc   
iter_linesrD   r<   r<   r=   r     r   z%HttpxBinaryResponseContent.iter_linesc                 C  r   r:   )rc   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openrc   r   write)r;   r   frg   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   rg   r<   r<   r=   stream_to_file  s
   	"z)HttpxBinaryResponseContent.stream_to_filec                 C  r   r:   )rc   closerD   r<   r<   r=   r     r   z HttpxBinaryResponseContent.closec                      | j  I d H S r:   )rc   areadrD   r<   r<   r=   r        z HttpxBinaryResponseContent.areadAsyncIterator[bytes]c                      | j |S r:   )rc   aiter_bytesr   r<   r<   r=   r        z&HttpxBinaryResponseContent.aiter_bytesAsyncIterator[str]c                   r   r:   )rc   
aiter_textr   r<   r<   r=   r     r   z%HttpxBinaryResponseContent.aiter_textc                   s   | j  S r:   )rc   aiter_linesrD   r<   r<   r=   r     s   
z&HttpxBinaryResponseContent.aiter_linesc                   r   r:   )rc   	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   rc   r   r   )r;   r   r   pathr   rg   r<   r<   r=   astream_to_file  s   
	.z*HttpxBinaryResponseContent.astream_to_filec                   r   r:   )rc   acloserD   r<   r<   r=   r     r   z!HttpxBinaryResponseContent.aclose)rc   r0   r8   r9   r   r   )r8   r   )r   r   r8   r   r:   )r   r   r8   r   )r   r   r8   r   )r8   r   )r   r   r8   r9   )r   r   r   r   r8   r9   r   )r   r   r8   r   )r   r   r8   r   )r8   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=   rq   b  sF   
 








rq   )r   r   r8   r   )r   r   r8   r   )=
__future__r   osrn   loggingdatetimer   typingr   r   r   r   r   r   r	   r
   r   r   typing_extensionsr   r   r   r   r   r   rs   rv   _typesr   _utilsr   r   r   _modelsr   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr   r   _base_clientr    r!   r"   r#   	getLoggerr   r$   r   r%   rj   rl   r   r   rq   r<   r<   r<   r=   <module>   s>    0  

