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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mZmZmZ ddlmZ ddlmZmZ dd	lmZ dd
l m!Z! erddlm"Z" ddl#m$Z$m%Z%m&Z& edZ'edZ(e)e*Z+de,d< G dd de
e( Z-G dd de.Z/dddddZ0dddddZ1dddd d!Z2dS )"    )annotationsN)TYPE_CHECKINGAnyUnionGenericTypeVarCallablecast)	Awaitable	ParamSpecget_argsoverride
get_origin   )NoneTypeUnknownResponseBinaryResponseContent)is_given)	BaseModelis_basemodel)RAW_RESPONSE_HEADER)APIResponseValidationError)FinalRequestOptions)Stream
BaseClientAsyncStreamPRzlogging.Loggerlogc                   @  s0  e Zd ZU 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Z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
e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d0d1Zed#dd2d3Zd4S )5APIResponseztype[R]_cast_tozBaseClient[Any, Any]_clientzR | None_parsedbool_stream1type[Stream[Any]] | type[AsyncStream[Any]] | None_stream_clsr   _optionszhttpx.Responsehttp_responseNone)rawcast_toclientstream
stream_clsoptionsreturnc                C  s.   || _ || _d | _|| _|| _|| _|| _d S N)r    r!   r"   r$   r&   r'   r(   )selfr*   r+   r,   r-   r.   r/    r3   Q/var/www/html/python-backend/venv/lib/python3.9/site-packages/openai/_response.py__init__'   s    
zAPIResponse.__init__r   r0   c                 C  s:   | j d ur| j S |  }t| jjr0| j|}|| _ |S r1   )r"   _parser   r'   Zpost_parser)r2   parsedr3   r3   r4   parse9   s    
zAPIResponse.parsezhttpx.Headersc                 C  s   | j jS r1   )r(   headersr2   r3   r3   r4   r:   D   s    zAPIResponse.headerszhttpx.Requestc                 C  s   | j jS r1   )r(   requestr;   r3   r3   r4   http_requestH   s    zAPIResponse.http_requestintc                 C  s   | j jS r1   )r(   status_coder;   r3   r3   r4   r?   L   s    zAPIResponse.status_codez	httpx.URLc                 C  s   | j jS r1   )r(   urlr;   r3   r3   r4   r@   P   s    zAPIResponse.urlstrc                 C  s   | j jS r1   )r=   methodr;   r3   r3   r4   rB   T   s    zAPIResponse.methodbytesc                 C  s   | j jS r1   )r(   contentr;   r3   r3   r4   rD   X   s    zAPIResponse.contentc                 C  s   | j jS r1   )r(   textr;   r3   r3   r4   rE   \   s    zAPIResponse.textc                 C  s   | j jS r1   )r(   http_versionr;   r3   r3   r4   rF   `   s    zAPIResponse.http_versionzdatetime.timedeltac                 C  s   | j jS )zCThe time taken for the complete request/response cycle to complete.)r(   elapsedr;   r3   r3   r4   rG   d   s    zAPIResponse.elapsedc           	      C  s6  | j rn| jr2tt| jt| j| jtt| jdS td| jj}|d u rNt	 tt|| j
| jtt| jdS | j
}|tu rttd S | j}|tkrtt|jS t|p|}t|rt|trtt||S |tkrtdt|rt|tjr|tjkrtdtt|S |turr|turr|turr|turrt|tsrtd| dt dt dt dt d	|jd
d^}}|dkrt |rz|! }W n8 t"y } zt#$dt%|| W Y d }~nd }~0 0 | jj&|||dS | jj'rt(|d| d|jd|jS |! }| jj&|||dS )N)r+   responser,   r%   z+Unexpected state - cast_to is `APIResponse`z:Subclasses of httpx.Response cannot be passed to `cast_to`zInvalid state, expected z to be a subclass type of z, z or .zcontent-type;zapplication/jsonz5Could not read JSON from response data due to %s - %s)datar+   rH   zMExpected Content-Type response header to be `application/json` but received `z
` instead.)rH   messagebody))r$   r&   r	   r   _extract_stream_chunk_typer(   r   r!   Z_default_stream_clsMissingStreamClassErrorr    r   rA   rE   r   inspectisclass
issubclassr   r   RuntimeErrorhttpxResponse
ValueErrorr   listdictr   r   r:   getsplitr   json	Exceptionr   debugtypeZ_process_response_dataZ_strict_response_validationr   )	r2   r.   r+   rH   origincontent_type_rK   excr3   r3   r4   r7   i   s    
	
	

"

(

	zAPIResponse._parsec                 C  s    d| j  d| jj d| j dS )Nz<APIResponse [ z] type=>)r?   r(   Zreason_phraser    r;   r3   r3   r4   __repr__   s    zAPIResponse.__repr__N)__name__
__module____qualname____annotations__r5   r9   propertyr:   r=   r?   r@   rB   rD   rE   rF   rG   r7   r   re   r3   r3   r3   r4   r      s<   
jr   c                      s"   e Zd Zdd fddZ  ZS )rO   r)   r6   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)superr5   r;   	__class__r3   r4   r5      s    z MissingStreamClassError.__init__)rf   rg   rh   r5   __classcell__r3   r3   rl   r4   rO      s   rO   r^   )r.   r0   c                 C  s(   t | }|std|  tt|d S )Nz^Expected stream_cls to have been given a generic type argument, e.g. Stream[Foo] but received r   )r   	TypeErrorr	   r^   )r.   argsr3   r3   r4   rN      s    rN   zCallable[P, R]zCallable[P, APIResponse[R]])funcr0   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APIResponse[R]rp   kwargsr0   c                    s@   i t t|dpi }d|t< ||d< t tt  | i |S Nextra_headerstruer	   r   rY   r   r   r   rp   rw   ry   rq   r3   r4   wrapped   s    z(to_raw_response_wrapper.<locals>.wrapped	functoolswrapsrq   r~   r3   r}   r4   to_raw_response_wrapper   s    r   zCallable[P, Awaitable[R]]z&Callable[P, Awaitable[APIResponse[R]]]c                   s$   t  dddd fdd}|S )rr   rs   rt   ru   rv   c                    sF   i t t|dpi }d|t< ||d< t tt  | i |I d H S rx   r{   r|   r}   r3   r4   r~      s    z.async_to_raw_response_wrapper.<locals>.wrappedr   r   r3   r}   r4   async_to_raw_response_wrapper   s    r   )3
__future__r   rP   loggingdatetimer   typingr   r   r   r   r   r   r	   Ztyping_extensionsr
   r   r   r   r   rT   _typesr   r   r   _utilsr   Z_modelsr   r   
_constantsr   _exceptionsr   r   Z_base_clientr   r   r   r   r   	getLoggerrf   r   ri   r   ro   rO   rN   r   r   r3   r3   r3   r4   <module>   s0   $ <	