o
    ȳg-                     @  s  d Z 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 ddl	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mZ eeZG dd	 d	eeZerddd
lmZmZmZmZmZm Z m!Z!m"Z" zddlm#Z# W n e$yw   e%Z#Y nw G dd de#Z&dS )z.Utility functions for parsing an OpenAPI spec.    )annotationsN)Enum)Path)TYPE_CHECKINGDictListOptionalUnion)ValidationErrorc                   @  s>   e Zd ZdZdZdZdZdZdZdZ	dZ
d	ZedddZdS )HTTPVerbzEnumerator of the HTTP verbs.getputpostdeleteoptionsheadpatchtraceverbstrreturnc                 C  s*   z| |W S  t y   t d| j w )zParse an HTTP verb.z$Invalid HTTP verb. Valid values are )
ValueError__members__)clsr    r   a/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/langchain_community/utilities/openapi.pyfrom_str    s
   
zHTTPVerb.from_strN)r   r   r   r   )__name__
__module____qualname____doc__GETPUTPOSTDELETEOPTIONSHEADPATCHTRACEclassmethodr   r   r   r   r   r      s    r   )
Components	Operation	ParameterPathItemPaths	ReferenceRequestBodySchema)OpenAPIc                      sd  e Zd ZU dZdZded< ed\ddZd]ddZed^ddZ	ed_ddZ
ed`ddZedaddZdbddZdcdd Zddd"d#Z	$	%dedfd,d-Zddd.d/Zdgd1d2Zdhd4d5Zedid9d:Zedj fd;d<Zedkd>d?ZedldAdBZedmdDdEZedndGdHZedodIdJZdpdLdMZdqdOdPZdrdSdTZdsdVdWZ dtdXdYZ!edudZd[Z"  Z#S )vOpenAPISpecz;OpenAPI Model that removes mis-formatted parts of the spec.3.1.0r   openapir   r.   c                 C  s   | j std| j S )NzNo paths found in spec)pathsr   selfr   r   r   _paths_strict@   s   zOpenAPISpec._paths_strictpathr-   c                 C  s"   | j |}|std| |S )NzNo path found for )r9   r   r   )r8   r:   	path_itemr   r   r   _get_path_strictF   s   zOpenAPISpec._get_path_strictr*   c                 C  s   | j du r	td| j S )zGet components or err.NzNo components found in spec. )
componentsr   r7   r   r   r   _components_strictL   s   
zOpenAPISpec._components_strict&Dict[str, Union[Parameter, Reference]]c                 C     | j j}|du rtd|S )zGet parameters or err.NzNo parameters found in spec. )r>   
parametersr   )r8   rA   r   r   r   _parameters_strictS      zOpenAPISpec._parameters_strictDict[str, Schema]c                 C  r@   )z%Get the dictionary of schemas or err.NzNo schemas found in spec. )r>   schemasr   )r8   rE   r   r   r   _schemas_strict[   rC   zOpenAPISpec._schemas_strict(Dict[str, Union[RequestBody, Reference]]c                 C  r@   )zGet the request body or err.NzNo request body found in spec. )r>   requestBodiesr   )r8   request_bodiesr   r   r   _request_bodies_strictc   rC   z"OpenAPISpec._request_bodies_strictrefr/   Union[Parameter, Reference]c                 C  4   |j dd }| j}||vrtd| || S )z-Get a parameter (or nested reference) or err./zNo parameter found for )rK   splitrB   r   )r8   rK   ref_namerA   r   r   r   _get_referenced_parameterk   
   z%OpenAPISpec._get_referenced_parameterr,   c                 C  8   ddl m} | |}t||r| |}t||s|S zGet the root reference or err.r   r/   )openapi_pydanticr/   rR   
isinstance)r8   rK   r/   	parameterr   r   r   _get_root_referenced_parameters      



z*OpenAPISpec._get_root_referenced_parameterr1   c                 C  rM   )z*Get a schema (or nested reference) or err.rN   rO   zNo schema found for )rK   rP   rF   r   )r8   rK   rQ   rE   r   r   r   get_referenced_schema|   rS   z!OpenAPISpec.get_referenced_schemar   NschemaUnion[Reference, Schema]depthint	max_depthOptional[int]c                 C  s   |d ur||krt d| dddlm} t||r | |}|jd ur;|j D ]\}}| ||d ||j|< q*|jd urK| |j|d ||_|S )NzMax depth of z- has been exceeded when resolving references.r   rV      )RecursionErrorrW   r/   rX   r\   
propertiesitems
get_schema)r8   r]   r_   ra   r/   p_namepr   r   r   rg      s   




zOpenAPISpec.get_schemac                 C  rT   rU   )rW   r/   r\   rX   )r8   rK   r/   r]   r   r   r   _get_root_referenced_schema   r[   z'OpenAPISpec._get_root_referenced_schema'Optional[Union[Reference, RequestBody]]c                 C  rM   )z0Get a request body (or nested reference) or err.rN   rO   zNo request body found for )rK   rP   rJ   r   )r8   rK   rQ   rI   r   r   r   _get_referenced_request_body   s
   z(OpenAPISpec._get_referenced_request_bodyOptional[RequestBody]c                 C  rT   )z!Get the root request Body or err.r   rV   )rW   r/   rl   rX   )r8   rK   r/   request_bodyr   r   r   !_get_root_referenced_request_body   s   



z-OpenAPISpec._get_root_referenced_request_bodyobjdictNonec                 C  s   d}|  d}|  d}t|tr%|dkr"td| d|  d
S 	 d
S t|tr7td| d|  d
S td|  d	| )z#Alert if the spec is not supported.ze This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.swaggerr5   r4   zAttempting to load an OpenAPI z spec. zAttempting to load a Swagger z)Attempting to load an unsupported spec:


N)r   rX   r   loggerwarningr   )rp   warning_messageswagger_versionopenapi_versionr   r   r   _alert_unsupported_spec   s2   



z#OpenAPISpec._alert_unsupported_specc              
     s   z|  | t |W S  tyJ } z2t|}| D ]}|d }|}|d d D ]}|| }q+||d d  q| |W  Y d }~S d }~ww )NlocrO   )rz   super	parse_objr
   copydeepcopyerrorspop)r   rp   enew_objerrorkeysitemkey	__class__r   r   r}      s   


zOpenAPISpec.parse_obj	spec_dictc                 C  s
   |  |S )z Get an OpenAPI spec from a dict.)r}   )r   r   r   r   r   from_spec_dict   s   
zOpenAPISpec.from_spec_dicttextc                 C  s8   zt |}W n t jy   t|}Y nw | |S )z Get an OpenAPI spec from a text.)jsonloadsJSONDecodeErroryaml	safe_loadr   )r   r   r   r   r   r   	from_text   s   
zOpenAPISpec.from_textUnion[str, Path]c                 C  sh   t |tr|nt|}| st| d|d}| | W  d   S 1 s-w   Y  dS )z%Get an OpenAPI spec from a file path.z does not existrN)rX   r   existsFileNotFoundErroropenr   read)r   r:   path_fr   r   r   	from_file   s   $zOpenAPISpec.from_fileurlc                 C  s   t |}| |jS )zGet an OpenAPI spec from a URL.)requestsr   r   r   )r   r   responser   r   r   from_url  s   
zOpenAPISpec.from_urlc                 C  s   | j d jS )zGet the base url.r   )serversr   r7   r   r   r   base_url  s   zOpenAPISpec.base_url	List[str]c                 C  sL   ddl m} | |}g }tD ]}t||jd}t||r#||j q|S )z6Return a list of valid methods for the specified path.r   r+   N)rW   r+   r<   r   getattrvaluerX   append)r8   r:   r+   r;   resultsmethod	operationr   r   r   get_methods_for_path  s   

z OpenAPISpec.get_methods_for_pathList[Parameter]c                 C  sR   ddl m} | |}g }|jsg S |jD ]}t||r!| |}|| q|S )Nr   rV   )rW   r/   r<   rA   rX   rZ   r   )r8   r:   r/   r;   rA   rY   r   r   r   get_parameters_for_path  s   



z#OpenAPISpec.get_parameters_for_pathr   r+   c                 C  sD   ddl m} | |}t||d}t||s td| d| |S )z:Get the operation object for a given path and HTTP method.r   r   NzNo z method found for )rW   r+   r<   r   rX   r   )r8   r:   r   r+   r;   operation_objr   r   r   get_operation&  s   

zOpenAPISpec.get_operationr   c                 C  sD   ddl m} g }|jr |jD ]}t||r| |}|| q|S )z)Get the components for a given operation.r   rV   )rW   r/   rA   rX   rZ   r   )r8   r   r/   rA   rY   r   r   r   get_parameters_for_operation0  s   


z(OpenAPISpec.get_parameters_for_operationc                 C  s*   ddl m} |j}t||r| |}|S )z+Get the request body for a given operation.r   rV   )rW   r/   requestBodyrX   ro   )r8   r   r/   rn   r   r   r   get_request_body_for_operation<  s
   

z*OpenAPISpec.get_request_body_for_operationc                 C  sL   | j }|du rtdd|d}| d| }|ddddddS )z0Get a cleaned operation id from an operation id.Nz[^a-zA-Z0-9]_rN   -.)operationIdresublstripreplace)r   r:   r   operation_idr   r   r   get_cleaned_operation_idG  s
   z$OpenAPISpec.get_cleaned_operation_id)r   r.   )r:   r   r   r-   )r   r*   )r   r?   )r   rD   )r   rG   )rK   r/   r   rL   )rK   r/   r   r,   )rK   r/   r   r1   )r   N)r]   r^   r_   r`   ra   rb   r   r1   )rK   r/   r   rk   )rK   r/   r   rm   )rp   rq   r   rr   )rp   rq   r   r3   )r   rq   r   r3   )r   r   r   r3   )r:   r   r   r3   )r   r   r   r3   )r   r   )r:   r   r   r   )r:   r   r   r   )r:   r   r   r   r   r+   )r   r+   r   r   )r   r+   r   rm   )r   r+   r:   r   r   r   r   r   )$r   r   r   r    r5   __annotations__propertyr9   r<   r>   rB   rF   rJ   rR   rZ   r\   rg   rj   rl   ro   staticmethodrz   r)   r}   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r3   ;   sX   
 



	

	







r3   )'r    
__future__r   r~   r   loggingr   enumr   pathlibr   typingr   r   r   r   r	   r   r   pydanticr
   	getLoggerr   ru   r   r   rW   r*   r+   r,   r-   r.   r/   r0   r1   r2   ImportErrorobjectr3   r   r   r   r   <module>   s,    
(