
    Ng-                    6   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j        e          Z G d d	ee          Zerdd
lmZmZmZmZmZm Z m!Z!m"Z" 	 ddlm#Z# n# e$$ r e%Z#Y nw xY w G d de#          Z&dS )z.Utility functions for parsing an OpenAPI spec.    )annotationsN)Enum)Path)TYPE_CHECKINGDictListOptionalUnion)ValidationErrorc                  J    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            ZdS )HTTPVerbzEnumerator of the HTTP verbs.getputpostdeleteoptionsheadpatchtraceverbstrreturnc                d    	  | |          S # t           $ r t          d| j                   w xY w)zParse an HTTP verb.z$Invalid HTTP verb. Valid values are )
ValueError__members__)clsr   s     a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/openapi.pyfrom_strzHTTPVerb.from_str    sN    	W3t99 	W 	W 	WUCOUUVVV	Ws   
 "/N)r   r   r   r   )__name__
__module____qualname____doc__GETPUTPOSTDELETEOPTIONSHEADPATCHTRACEclassmethodr        r   r   r      sf        ''
C
CDFGDEEW W W [W W Wr-   r   )
Components	Operation	ParameterPathItemPaths	ReferenceRequestBodySchema)OpenAPIc                      e Zd ZU dZdZded<   edBd            ZdCd
ZedDd            Z	edEd            Z
edFd            ZedGd            ZdHdZdIdZdJdZ	 	 dKdLd#ZdJd$ZdMd&ZdNd(ZedOd,            ZedP fd-            ZedQd/            ZedRd1            ZedSd3            ZedTd5            ZedUd6            ZdVd8ZdWd:ZdXd=ZdYd?Z dZd@Z!ed[dA            Z" xZ#S )\OpenAPISpecz;OpenAPI Model that removes mis-formatted parts of the spec.3.1.0r   openapir   r2   c                <    | j         st          d          | j         S )NzNo paths found in spec)pathsr   selfs    r   _paths_strictzOpenAPISpec._paths_strict@   s#    z 	75666zr-   pathr1   c                b    | j                             |          }|st          d|           |S )NzNo path found for )r?   r   r   )r>   r@   	path_items      r   _get_path_strictzOpenAPISpec._get_path_strictF   s=    &**400	 	:8$88999r-   r.   c                <    | j         t          d          | j         S )zGet components or err.NzNo components found in spec. )
componentsr   r=   s    r   _components_strictzOpenAPISpec._components_strictL   s#     ?"<===r-   &Dict[str, Union[Parameter, Reference]]c                @    | j         j        }|t          d          |S )zGet parameters or err.NzNo parameters found in spec. )rF   
parametersr   )r>   rI   s     r   _parameters_strictzOpenAPISpec._parameters_strictS   s*     ,7
<===r-   Dict[str, Schema]c                @    | j         j        }|t          d          |S )z%Get the dictionary of schemas or err.NzNo schemas found in spec. )rF   schemasr   )r>   rM   s     r   _schemas_strictzOpenAPISpec._schemas_strict[   s(     )1?9:::r-   (Dict[str, Union[RequestBody, Reference]]c                @    | j         j        }|t          d          |S )zGet the request body or err.NzNo request body found in spec. )rF   requestBodiesr   )r>   request_bodiess     r   _request_bodies_strictz"OpenAPISpec._request_bodies_strictc   s*     0>!>???r-   refr3   Union[Parameter, Reference]c                    |j                             d          d         }| j        }||vrt          d|           ||         S )z-Get a parameter (or nested reference) or err./zNo parameter found for )rT   splitrJ   r   )r>   rT   ref_namerI   s       r   _get_referenced_parameterz%OpenAPISpec._get_referenced_parameterk   sO    7==%%b),
:%%AxAABBB(##r-   r0   c                    ddl m} |                     |          }t          ||          r%|                     |          }t          ||          %|S zGet the root reference or err.r   r3   )openapi_pydanticr3   r[   
isinstance)r>   rT   r3   	parameters       r   _get_root_referenced_parameterz*OpenAPISpec._get_root_referenced_parameters   sh    ......22377	I.. 	B66yAAI I.. 	Br-   r5   c                    |j                             d          d         }| j        }||vrt          d|           ||         S )z*Get a schema (or nested reference) or err.rW   rX   zNo schema found for )rT   rY   rN   r   )r>   rT   rZ   rM   s       r   get_referenced_schemaz!OpenAPISpec.get_referenced_schema|   sO    7==%%b)&7"">H>>???x  r-   r   NschemaUnion[Reference, Schema]depthint	max_depthOptional[int]c                x   |||k    rt          d| d          ddlm} t          ||          r|                     |          }|j        A|j                                        D ]'\  }}|                     ||dz   |          |j        |<   (|j        $|                     |j        |dz   |          |_        |S )NzMax depth of z- has been exceeded when resolving references.r   r^      )RecursionErrorr_   r3   r`   rd   
propertiesitems
get_schema)r>   re   rg   ri   r3   p_nameps          r   rp   zOpenAPISpec.get_schema   s      Ui%7%7 X	XXX   	/.....fi(( 	8//77F (#.4466 U U	,0OOAuqy),T,T!&))<#??6<INNFLr-   c                    ddl m} |                     |          }t          ||          r%|                     |          }t          ||          %|S r]   )r_   r3   rd   r`   )r>   rT   r3   re   s       r   _get_root_referenced_schemaz'OpenAPISpec._get_root_referenced_schema   se    ......++C00++ 	8//77F ++ 	8r-   'Optional[Union[Reference, RequestBody]]c                    |j                             d          d         }| j        }||vrt          d|           ||         S )z0Get a request body (or nested reference) or err.rW   rX   zNo request body found for )rT   rY   rS   r   )r>   rT   rZ   rR   s       r   _get_referenced_request_bodyz(OpenAPISpec._get_referenced_request_body   sQ     7==%%b)4>))D(DDEEEh''r-   Optional[RequestBody]c                    ddl m} |                     |          }t          ||          r%|                     |          }t          ||          %|S )z!Get the root request Body or err.r   r^   )r_   r3   rw   r`   )r>   rT   r3   request_bodys       r   !_get_root_referenced_request_bodyz-OpenAPISpec._get_root_referenced_request_body   sj     	/.....88==y11 	K<<\JJL y11 	Kr-   objdictNonec                p   d}|                      d          }|                      d          }t          |t                    r*|dk    r"t                              d| d|            d
S d
S t          |t                    r"t                              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.swaggerr:   r9   zAttempting to load an OpenAPI z spec. zAttempting to load a Swagger z)Attempting to load an unsupported spec:


N)r   r`   r   loggerwarningr   )r|   warning_messageswagger_versionopenapi_versions       r   _alert_unsupported_specz#OpenAPISpec._alert_unsupported_spec   s   % 	
 ''),,''),,os++ 	'))0_ 0 0-0 0    
 -- 		NN, , ,), ,    
 00 0-0 0  r-   c                   	 |                      |           t                                          |          S # t          $ r}t	          j        |          }|                                D ]=}|d         }|}|d d         D ]
}||         }|                    |d         d            >|                     |          cY d }~S d }~ww xY w)NlocrX   )r   super	parse_objr   copydeepcopyerrorspop)	r   r|   enew_objerrorkeysitemkey	__class__s	           r   r   zOpenAPISpec.parse_obj   s    	*'',,,77$$S))) 
	* 
	* 
	* mC((G ) )U|9 % %C9DDb4((((==))))))))
	*s   59 
CA:C=CC	spec_dictc                ,    |                      |          S )z Get an OpenAPI spec from a dict.)r   )r   r   s     r   from_spec_dictzOpenAPISpec.from_spec_dict   s     }}Y'''r-   textc                    	 t          j        |          }n)# t           j        $ r t          j        |          }Y nw xY w|                     |          S )z Get an OpenAPI spec from a text.)jsonloadsJSONDecodeErroryaml	safe_loadr   )r   r   r   s      r   	from_textzOpenAPISpec.from_text   s]    	-
4((II# 	- 	- 	-t,,III	-!!),,,s    #==Union[str, Path]c                F   t          |t                    r|nt          |          }|                                st          | d          |                    d          5 }|                     |                                          cddd           S # 1 swxY w Y   dS )z%Get an OpenAPI spec from a file path.z does not existrN)r`   r   existsFileNotFoundErroropenr   read)r   r@   path_fs       r   	from_filezOpenAPISpec.from_file   s     #4..>DJJ||~~ 	>#t$<$<$<===ZZ__ 	+==**	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   "'BBBurlc                ^    t          j        |          }|                     |j                  S )zGet an OpenAPI spec from a URL.)requestsr   r   r   )r   r   responses      r   from_urlzOpenAPISpec.from_url  s'     <$$}}X]+++r-   c                &    | j         d         j        S )zGet the base url.r   )serversr   r=   s    r   base_urlzOpenAPISpec.base_url  s     |A""r-   	List[str]c                    ddl m} |                     |          }g }t          D ]B}t	          ||j        d          }t          ||          r|                    |j                   C|S )z6Return a list of valid methods for the specified path.r   r/   N)r_   r/   rC   r   getattrvaluer`   append)r>   r@   r/   rB   resultsmethod	operations          r   get_methods_for_pathz OpenAPISpec.get_methods_for_path  sz    ......))$//	 	- 	-F	6<>>I)Y// -v|,,,r-   List[Parameter]c                    ddl m} |                     |          }g }|j        sg S |j        D ]<}t	          ||          r|                     |          }|                    |           =|S )Nr   r^   )r_   r3   rC   rI   r`   rb   r   )r>   r@   r3   rB   rI   ra   s         r   get_parameters_for_pathz#OpenAPISpec.get_parameters_for_path  s    ......))$//	
# 	I"- 	) 	)I)Y// K ??	JJ	i((((r-   r   r/   c                    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 )r_   r/   rC   r   r`   r   )r>   r@   r   r/   rB   operation_objs         r   get_operationzOpenAPISpec.get_operation&  sp    ......))$//		6488-33 	EC6CCTCCDDDr-   r   c                    ddl m} g }|j        rD|j        D ]<}t          ||          r|                     |          }|                    |           =|S )z)Get the components for a given operation.r   r^   )r_   r3   rI   r`   rb   r   )r>   r   r3   rI   ra   s        r   get_parameters_for_operationz(OpenAPISpec.get_parameters_for_operation0  sy    ......
 	-&1 - -	i33 O $ C CI N NI!!),,,,r-   c                j    ddl m} |j        }t          ||          r|                     |          }|S )z+Get the request body for a given operation.r   r^   )r_   r3   requestBodyr`   r{   )r>   r   r3   rz   s       r   get_request_body_for_operationz*OpenAPISpec.get_request_body_for_operation<  sJ     	/..... ,lI.. 	PAA,OOLr-   c                    | j         }|0t          j        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]_rW   -.)operationIdresublstripreplace)r   r@   r   operation_ids       r   get_cleaned_operation_idz$OpenAPISpec.get_cleaned_operation_idG  sz     !,6/3C0@0@AAD"--V--L##C--55c3??GGSQQQr-   )r   r2   )r@   r   r   r1   )r   r.   )r   rG   )r   rK   )r   rO   )rT   r3   r   rU   )rT   r3   r   r0   )rT   r3   r   r5   )r   N)re   rf   rg   rh   ri   rj   r   r5   )rT   r3   r   ru   )rT   r3   r   rx   )r|   r}   r   r~   )r|   r}   r   r8   )r   r}   r   r8   )r   r   r   r8   )r@   r   r   r8   )r   r   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   rx   )r   r/   r@   r   r   r   r   r   )$r   r    r!   r"   r:   __annotations__propertyr?   rC   rF   rJ   rN   rS   r[   rb   rd   rp   rt   rw   r{   staticmethodr   r+   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r8   r8   ;   s        EEG   X
       X    X    X    X$ $ $ $   ! ! ! ! #'	    6   ( ( ( (	 	 	 	    \8 * * * * * [*  ( ( ( [( - - - [- + + + [+ , , , [,
 # # # X#
 
 
 
      
 
 
 
	 	 	 	 R R R \R R R R Rr-   r8   )'r"   
__future__r   r   r   loggingr   enumr   pathlibr   typingr   r   r   r	   r
   r   r   pydanticr   	getLoggerr   r   r   r   r_   r.   r/   r0   r1   r2   r3   r4   r5   r6   ImportErrorobjectr8   r,   r-   r   <module>r      s   4 4 " " " " " "    				             = = = = = = = = = = = = = =   $ $ $ $ $ $		8	$	$W W W W WsD W W W*  
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	(((((((   GGGTR TR TR TR TR' TR TR TR TR TRs   7A> >BB