
    f jC<                       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          Z0 ed          Z1 ed          Z2 ej3        e4          Z5de6d<    G d dee1                   Z7 G d de8          Z9d!dZ:d"dZ; G 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                     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<   d7dZed8d            Zed9d            Zdd d:d#Zed;d%            Z	ed<d'            Z
ed=d)            Zed>d+            Zed?d-            Zed@d/            Zed?d0            Zed?d1            ZedAd2            ZedBd4            Zdd d:d5Zed?d6            ZdS )C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               h    || _         || _        i | _        || _        || _        || _        || _        d S N)r)   r+   r,   r.   r0   r1   r3   )selfr4   r5   r6   r7   r8   r9   s          kC:\Users\Terasoftware\OneDrive\Desktop\faahhh\fyndo\fyndo\venv\Lib\site-packages\openai/_legacy_response.py__init__zLegacyAPIResponse.__init__8   s>      !%     totype[_T]r$   c                   d S r=    )r>   rB   s     r?   parsezLegacyAPIResponse.parseJ       rA   r#   c                    d S r=   rE   r>   s    r?   rF   zLegacyAPIResponse.parseN   rG   rA   NrB   type[_T] | NoneR | _Tc                   ||n| j         }| j                            |          }||S |                     |          }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`
        NrJ   )r)   r,   get_parser   r1   post_parser)r>   rB   	cache_keycachedparseds        r?   rF   zLegacyAPIResponse.parseR   s    @ ;BBdm	%)))44 	M##DM-.. 	7]..v66F*0Y'rA   httpx.Headersc                    | j         j        S r=   )r3   headersrI   s    r?   rV   zLegacyAPIResponse.headers~       !))rA   httpx.Requestc                    | j         j        S r=   )r3   requestrI   s    r?   http_requestzLegacyAPIResponse.http_request   rW   rA   intc                    | j         j        S r=   )r3   status_coderI   s    r?   r^   zLegacyAPIResponse.status_code   s    !--rA   	httpx.URLc                    | j         j        S r=   )r3   urlrI   s    r?   ra   zLegacyAPIResponse.url   s    !%%rA   strc                    | j         j        S r=   )r[   methodrI   s    r?   rd   zLegacyAPIResponse.method   s     ''rA   bytesc                    | j         j        S )zReturn the binary response content.

        NOTE: this will be removed in favour of `.read()` in the
        next major version.
        )r3   contentrI   s    r?   rg   zLegacyAPIResponse.content   s     !))rA   c                    | j         j        S )zyReturn the decoded response content.

        NOTE: this will be turned into a method in the next major version.
        )r3   textrI   s    r?   ri   zLegacyAPIResponse.text   s     !&&rA   c                    | j         j        S r=   )r3   http_versionrI   s    r?   rk   zLegacyAPIResponse.http_version   s    !..rA   c                    | j         j        S r=   )r3   	is_closedrI   s    r?   rm   zLegacyAPIResponse.is_closed   s    !++rA   datetime.timedeltac                    | j         j        S )zCThe time taken for the complete request/response cycle to complete.)r3   elapsedrI   s    r?   rp   zLegacyAPIResponse.elapsed   s     !))rA   c               $
   |rt          |          rt          |d          }| j        rK|r{t          |          st	          dt
           dt                     t          t           |t          |d          | j
        t          t          | j                                      S | j        rZt          t          |                     t          | j                  | j
        t          t          | j                                      S t          d| j        j                  }|t!                      t          t           || j        | j
        t          t          | j                                      S ||n| j        }t          |          rt          |d          }|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    r't          t          t-          |j                            S t/          |          p|}t1          j        |          r3t5          |t6                    rt          t           ||                    S |t8          k    rt;          d          t1          j        |          rNt5          |t<          j                  r4|t<          j        k    rtA          d	          t          t          |          S t1          j        |          r>t5          |tB                    s)t5          |tD          j!                  rt	          d
          |tF          ur|tH          urw|tJ          urn|tL          uret5          |tB                    sPt;          d| dtB           dtJ           dtH           dtL           dt$           dt&           dt<          j         d          |j'        (                    dd          )                    d          ^}}|dk    rtU          |          rr	 |+                                }| j        ,                    |||          S # tZ          $ r3}	t\          /                    dta          |	          |	           Y d }	~	nd }	~	ww xY w| j        j1        rte          |d| d|j                  |j        S |+                                }| j        ,                    |||          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/json)datar5   rs   z5Could not read JSON from response data due to %s - %szMExpected Content-Type response header to be `application/json` but received `z
` instead.)rs   messagebody)3r   r   r.   r   	TypeErrorr   r   r   r$   r   r3   r   r+   r0   r#   _default_stream_clsMissingStreamClassErrorr)   r   rb   ri   r\   floatr   inspectisclass
issubclassHttpxBinaryResponseContentr'   RuntimeErrorhttpxResponse
ValueErrorr   pydanticobjectlistdictr   rV   rN   splitr   json_process_response_data	Exceptionr%   debugtype_strict_response_validationr   )
r>   rB   r8   r5   rs   origincontent_type_rw   excs
             r?   rO   zLegacyAPIResponse._parse   s    	)#B'' 	)!"a((B< &	 +B// q#$oV\$o$obm$o$opppB 9 -D! ! ! "&!3#C66  
 
 
  $$ 9$:J K K!%!3#C66 %     QSWS_SsttJ 0-///
 M!/T\22     9""DM W%% 	3&w22Gh 	!4== %c> 	*8=)))c> 	/3x}--...e 	15//000G$$/?6"" 	.z&:T'U'U 	.778,,---&& 	NLMMM?6"" 	%z&%.'I'I 	% %.( ` !^___8$$$?6"" 	v:fi+H+H 	vZX^`h`rMsMs 	vtuuu 6!		dN		 dN		 eO			
 vy11		  Yg  Y  YI  Y  YY]  Y  Yae  Y  Yin  Y  Yrz  Y  Y  B  Y  Y  HM  HV  Y  Y  Y   $+//DDJJ3OOq-- 	!G$$ 
	#==??D  <>>! '!) ?    ! g g gIIUW[\_W`W`beffffffffg |7 0% Elx  E  E  E!    = }}|22 3 
 
 	
s   Q3 3
R0=)R++R0c                B    d| j          d| j        j         d| j         dS )Nz<APIResponse [ z] type=>)r^   r3   reason_phraser)   rI   s    r?   __repr__zLegacyAPIResponse.__repr__4  s0    l 0ll43E3Sll\`\illllrA   )r4   r2   r5   r(   r6   r*   r7   r-   r8   r/   r9   r    r:   r;   )rB   rC   r:   r$   )r:   r#   )rB   rK   r:   rL   )r:   rT   )r:   rX   )r:   r\   )r:   r_   r:   rb   r:   re   )r:   r-   )r:   rn   )__name__
__module____qualname____doc____annotations__r@   r   rF   propertyrV   r[   r^   ra   rd   rg   ri   rk   rm   rp   rO   r   r   rE   rA   r?   r'   r'   "   s{        
 
 !!!!))))MMMBBBB!!!!!!!!! ! ! !$    X    X .2 * * * * * *X * * * X* * * * X* . . . X. & & & X& ( ( ( X( * * * X* ' ' ' X' / / / X/ , , , X, * * * X* /3 B
 B
 B
 B
 B
 B
H m m m Xm m mrA   r'   c                        e Zd Zd fdZ xZS )r|   r:   r;   c                J    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@   )r>   	__class__s    r?   r@   z MissingStreamClassError.__init__:  s/     I	
 	
 	
 	
 	
rA   r:   r;   )r   r   r   r@   __classcell__)r   s   @r?   r|   r|   9  s=        
 
 
 
 
 
 
 
 
 
rA   r|   funcCallable[P, R]r:   !Callable[P, LegacyAPIResponse[R]]c                H     t          j                   d f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                     i t          t          |                    d                    pi }d|t          <   ||d<   t          t          t
                    | i |          S Nextra_headerstruer   r   rN   r   r'   r#   r   r   r   r   s      r?   wrappedz(to_raw_response_wrapper.<locals>.wrappedE  sc    (ZDfjj6Q6Q,R,R,XVX(Z-3)*"/%a($$*?*?*?@@@rA   r   r   r   r   r:   r   	functoolswrapsr   r   s   ` r?   to_raw_response_wrapperr   @  sE    
 _TA A A A A A NrA   Callable[P, Awaitable[R]],Callable[P, Awaitable[LegacyAPIResponse[R]]]c                H     t          j                   d fd            }|S )	r   r   r   r   r   r:   r   c                    K   i t          t          |                    d                    pi }d|t          <   ||d<   t          t          t
                    | i | d {V           S r   r   r   s      r?   r   z.async_to_raw_response_wrapper.<locals>.wrappedV  sy      (ZDfjj6Q6Q,R,R,XVX(Z-3)*"/%a(d0Ef0E0E*E*E*E*E*E*EFFFrA   r   r   r   s   ` r?   async_to_raw_response_wrapperr   Q  sE    
 _TG G G G G G NrA   c                  ^   e Zd ZU ded<   d*dZed+d            Zed,d	            Zed-d            Zed-d            Z	d.dZ
d+dZd/d0dZd/d1dZd2dZd/d0dZd3dZ ed          ddd4d            Zd5d Zd+d!Zd/d6d#Zd/d7d%Zd8d&Zd/d6d'Z ed          ddd4d(            Zd5d)ZdS )9r   r2   rs   r:   r;   c                    || _         d S r=   )rs   )r>   rs   s     r?   r@   z#HttpxBinaryResponseContent.__init__e  s     rA   re   c                    | j         j        S r=   )rs   rg   rI   s    r?   rg   z"HttpxBinaryResponseContent.contenth  s    }$$rA   rb   c                    | j         j        S r=   )rs   ri   rI   s    r?   ri   zHttpxBinaryResponseContent.textl  s    }!!rA   
str | Nonec                    | j         j        S r=   )rs   encodingrI   s    r?   r   z#HttpxBinaryResponseContent.encodingp  s    }%%rA   c                    | j         j        S r=   )rs   charset_encodingrI   s    r?   r   z+HttpxBinaryResponseContent.charset_encodingt  s    }--rA   r   r   c                &     | j         j        di |S )NrE   )rs   r   )r>   r   s     r?   r   zHttpxBinaryResponseContent.jsonx  s    !t}!++F+++rA   c                4    | j                                         S r=   )rs   readrI   s    r?   r   zHttpxBinaryResponseContent.read{  s    }!!###rA   N
chunk_size
int | NoneIterator[bytes]c                6    | j                             |          S r=   )rs   
iter_bytesr>   r   s     r?   r   z%HttpxBinaryResponseContent.iter_bytes~  s    }''
333rA   Iterator[str]c                6    | j                             |          S r=   )rs   	iter_textr   s     r?   r   z$HttpxBinaryResponseContent.iter_text  s    }&&z222rA   c                4    | j                                         S r=   )rs   
iter_linesrI   s    r?   r   z%HttpxBinaryResponseContent.iter_lines  s    }'')))rA   c                6    | j                             |          S r=   )rs   iter_rawr   s     r?   r   z#HttpxBinaryResponseContent.iter_raw  s    }%%j111rA   filestr | os.PathLike[str]c                    t          |d          5 }| j                                        D ]}|                    |           	 ddd           dS # 1 swxY 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openrs   r   write)r>   r   frw   s       r?   write_to_filez(HttpxBinaryResponseContent.write_to_file  s     $T""" 	a0022  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   2AAAzDue to a bug, this method doesn't actually stream the response content, `.with_streaming_response.method()` should be used instead)r   c                   t          |d          5 }| j                            |          D ]}|                    |           	 d d d            d S # 1 swxY w Y   d S Nr   r   r   )r>   r   r   r   rw   s        r?   stream_to_filez)HttpxBinaryResponseContent.stream_to_file  s     $T""" 	a00<<  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   3AAAc                4    | j                                         S r=   )rs   closerI   s    r?   r   z HttpxBinaryResponseContent.close  s    }""$$$rA   c                D   K   | j                                          d {V S r=   )rs   areadrI   s    r?   r   z HttpxBinaryResponseContent.aread  s,      ]((*********rA   AsyncIterator[bytes]c                :   K   | j                             |          S r=   )rs   aiter_bytesr   s     r?   r   z&HttpxBinaryResponseContent.aiter_bytes  s      }((444rA   AsyncIterator[str]c                :   K   | j                             |          S r=   )rs   
aiter_textr   s     r?   r   z%HttpxBinaryResponseContent.aiter_text  s      }''
333rA   c                8   K   | j                                         S r=   )rs   aiter_linesrI   s    r?   r   z&HttpxBinaryResponseContent.aiter_lines  s      }((***rA   c                :   K   | j                             |          S r=   )rs   	aiter_rawr   s     r?   r   z$HttpxBinaryResponseContent.aiter_raw  s      }&&z222rA   c               >  K   t          j        |          }|                    d           d {V 4 d {V }| j                            |          2 3 d {V }|                    |           d {V  #6 	 d d d           d {V  d S # 1 d {V swxY w Y   d S r   )anyioPathr   rs   r   r   )r>   r   r   pathr   rw   s         r?   astream_to_filez*HttpxBinaryResponseContent.astream_to_file  s^      z$-------- 	$ 	$ 	$ 	$ 	$ 	$ 	$"m77
CC $ $ $ $ $ $ $dggdmm######## DC	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   BA7B
BBc                D   K   | j                                          d {V S r=   )rs   acloserI   s    r?   r   z!HttpxBinaryResponseContent.aclose  s,      ]))+++++++++rA   )rs   r2   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   rg   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   rA   r?   r   r   b  st        ! ! ! ! % % % X% " " " X" & & & X& . . . X., , , ,$ $ $ $4 4 4 4 43 3 3 3 3* * * *2 2 2 2 2     Z 	M  "&	     % % % %+ + + +5 5 5 5 54 4 4 4 4+ + + +3 3 3 3 3 Z 	M  "&		$ 	$ 	$ 	$ 	$ 	$, , , , , ,rA   r   )r   r   r:   r   )r   r   r:   r   )=
__future__r   osr~   loggingdatetimer   typingr   r   r   r   r   r	   r
   r   r   r   typing_extensionsr   r   r   r   r   r   r   r   _typesr   _utilsr   r   r   _modelsr   r   
_constantsr   
_streamingr   r   r   r   _exceptionsr   r    _base_clientr!   r"   r#   r$   	getLoggerr   r%   r   r'   rz   r|   r   r   r   rE   rA   r?   <module>r     s   " " " " " " " 				        q q q q q q q q q q q q q q q q q q q q q q q q T T T T T T T T T T T T T T          A A A A A A A A A A , , , , , , , , + + + + + + \ \ \ \ \ \ \ \ \ \ \ \ 3 3 3 3 3 3 ),,,,,,(((((( IcNNGCLLWT]]'g'11 1 1 1 1Tm Tm Tm Tm Tm
 Tm Tm Tmn
 
 
 
 
i 
 
 
   "   "f, f, f, f, f, f, f, f, f, f,rA   