a
    bg-                     @  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rd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$y   e%Z#Y n0 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                   @  sD   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ddZdS )HTTPVerbzEnumerator of the HTTP verbs.getputpostdeleteoptionsheadpatchtracestr)verbreturnc                 C  s2   z
| |W S  t y,   t d| j Y n0 dS )zParse an HTTP verb.z$Invalid HTTP verb. Valid values are N)
ValueError__members__)clsr    r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/openapi.pyfrom_str    s    
zHTTPVerb.from_strN)__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                      s  e Zd ZU dZdZded< eddddZ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d Zdd!dd"d#Zd[d&d'd(d!d)d*d+Zdd!dd,d-Zdd.dd/d0Zdd1dd2d3Zed4d5d6d7d8Zed4d d6 fd9d:Zed4d d;d<d=Zedd d>d?d@ZedAd d
dBdCZedd dDdEdFZedddGdHZddId
dJdKZddLd
dMdNZdddOdPdQdRZdOdLdSdTdUZ dOd1dSdVdWZ!edOddddXdYdZZ"  Z#S )\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_strictr-   )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_strictz&Dict[str, Union[Parameter, Reference]]c                 C  s   | j j}|du rtd|S )zGet parameters or err.NzNo parameters found in spec. )r>   
parametersr   )r8   r?   r   r   r   _parameters_strictS   s    zOpenAPISpec._parameters_strictzDict[str, Schema]c                 C  s   | j j}|du rtd|S )z%Get the dictionary of schemas or err.NzNo schemas found in spec. )r>   schemasr   )r8   rA   r   r   r   _schemas_strict[   s    zOpenAPISpec._schemas_strictz(Dict[str, Union[RequestBody, Reference]]c                 C  s   | j j}|du rtd|S )zGet the request body or err.NzNo request body found in spec. )r>   ZrequestBodiesr   )r8   request_bodiesr   r   r   _request_bodies_strictc   s    z"OpenAPISpec._request_bodies_strictr/   zUnion[Parameter, Reference])refr   c                 C  s4   |j dd }| j}||vr,td| || S )z-Get a parameter (or nested reference) or err./zNo parameter found for )rE   splitr@   r   )r8   rE   ref_namer?   r   r   r   _get_referenced_parameterk   s
    z%OpenAPISpec._get_referenced_parameterr,   c                 C  s0   ddl m} | |}t||r,| |}q|S zGet the root reference or err.r   r/   )openapi_pydanticr/   rJ   
isinstance)r8   rE   r/   	parameterr   r   r   _get_root_referenced_parameters   s
    

z*OpenAPISpec._get_root_referenced_parameterr1   c                 C  s4   |j dd }| j}||vr,td| || S )z*Get a schema (or nested reference) or err.rF   rG   zNo schema found for )rE   rH   rB   r   )r8   rE   rI   rA   r   r   r   get_referenced_schema|   s
    z!OpenAPISpec.get_referenced_schemar   NzUnion[Reference, Schema]intzOptional[int])schemadepth	max_depthr   c                 C  s   |d ur ||kr t d| dddlm} t||r@| |}|jd urv|j D ] \}}| ||d ||j|< qT|jd ur| |j|d ||_|S )NzMax depth of z- has been exceeded when resolving references.r   rL      )RecursionErrorrM   r/   rN   rQ   
propertiesitems
get_schema)r8   rS   rT   rU   r/   Zp_namepr   r   r   rZ      s    




zOpenAPISpec.get_schemac                 C  s0   ddl m} | |}t||r,| |}q|S rK   )rM   r/   rQ   rN   )r8   rE   r/   rS   r   r   r   _get_root_referenced_schema   s
    

z'OpenAPISpec._get_root_referenced_schemaz'Optional[Union[Reference, RequestBody]]c                 C  s4   |j dd }| j}||vr,td| || S )z0Get a request body (or nested reference) or err.rF   rG   zNo request body found for )rE   rH   rD   r   )r8   rE   rI   rC   r   r   r   _get_referenced_request_body   s
    z(OpenAPISpec._get_referenced_request_bodyzOptional[RequestBody]c                 C  s0   ddl m} | |}t||r,| |}q|S )z!Get the root request Body or err.r   rL   )rM   r/   r]   rN   )r8   rE   r/   request_bodyr   r   r   !_get_root_referenced_request_body   s
    

z-OpenAPISpec._get_root_referenced_request_bodydictNone)objr   c                 C  s~   d}|  d}|  d}t|trD|dkrztd| d|  qzn6t|trftd| d|  ntd|  d	| d
S )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.Z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   rN   r   loggerwarningr   )rb   Zwarning_messageZswagger_versionZopenapi_versionr   r   r   _alert_unsupported_spec   s2    



z#OpenAPISpec._alert_unsupported_specc              
     s   z|  | t |W S  ty } zdt|}| D ]:}|d }|}|d d D ]}|| }qV||d d  q:| |W  Y d }~S d }~0 0 d S )NlocrG   )rf   super	parse_objr
   copydeepcopyerrorspop)r   rb   eZnew_objerrorkeysitemkey	__class__r   r   ri      s    


zOpenAPISpec.parse_obj)	spec_dictr   c                 C  s
   |  |S )z Get an OpenAPI spec from a dict.)ri   )r   ru   r   r   r   from_spec_dict   s    zOpenAPISpec.from_spec_dict)textr   c                 C  s8   zt |}W n t jy,   t|}Y n0 | |S )z Get an OpenAPI spec from a text.)jsonloadsJSONDecodeErroryamlZ	safe_loadrv   )r   rw   ru   r   r   r   	from_text   s
    zOpenAPISpec.from_textzUnion[str, Path]c                 C  sh   t |tr|nt|}| s,t| d|d}| | W  d   S 1 sZ0    Y  dS )z%Get an OpenAPI spec from a file path.z does not existrN)rN   r   existsFileNotFoundErroropenr|   read)r   r:   Zpath_fr   r   r   	from_file   s
    zOpenAPISpec.from_file)urlr   c                 C  s   t |}| |jS )zGet an OpenAPI spec from a URL.)requestsr   r|   rw   )r   r   responser   r   r   from_url  s    
zOpenAPISpec.from_urlc                 C  s   | j d jS )zGet the base url.r   )Zserversr   r7   r   r   r   base_url  s    zOpenAPISpec.base_urlz	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)rM   r+   r<   r   getattrvaluerN   append)r8   r:   r+   r;   resultsmethod	operationr   r   r   get_methods_for_path  s    

z OpenAPISpec.get_methods_for_pathzList[Parameter]c                 C  sR   ddl m} | |}g }|js$g S |jD ]"}t||rB| |}|| q*|S )Nr   rL   )rM   r/   r<   r?   rN   rP   r   )r8   r:   r/   r;   r?   rO   r   r   r   get_parameters_for_path  s    



z#OpenAPISpec.get_parameters_for_pathr+   )r:   r   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 )rM   r+   r<   r   rN   r   )r8   r:   r   r+   r;   Zoperation_objr   r   r   get_operation&  s    

zOpenAPISpec.get_operation)r   r   c                 C  sD   ddl m} g }|jr@|jD ]"}t||r4| |}|| q|S )z)Get the components for a given operation.r   rL   )rM   r/   r?   rN   rP   r   )r8   r   r/   r?   rO   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   rL   )rM   r/   ZrequestBodyrN   r_   )r8   r   r/   r^   r   r   r   get_request_body_for_operation<  s
    

z*OpenAPISpec.get_request_body_for_operation)r   r:   r   r   c                 C  sL   | j }|du r0t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]_rF   -.)ZoperationIdresublstripreplace)r   r:   r   Zoperation_idr   r   r   get_cleaned_operation_idG  s
    z$OpenAPISpec.get_cleaned_operation_id)r   N)$r   r   r   r    r5   __annotations__propertyr9   r<   r>   r@   rB   rD   rJ   rP   rQ   rZ   r\   r]   r_   staticmethodrf   r)   ri   rv   r|   r   r   r   r   r   r   r   r   r   __classcell__r   r   rs   r   r3   ;   sV   
	  	

r3   )'r    
__future__r   rj   rx   loggingr   enumr   pathlibr   typingr   r   r   r   r	   r   r{   Zpydanticr
   	getLoggerr   rd   r   r   rM   r*   r+   r,   r-   r.   r/   r0   r1   r2   ImportErrorobjectr3   r   r   r   r   <module>   s(   
(
