
    7|h-                    *   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#  G d de#      Z&y# e$$ r e%Z#Y w xY w)z.Utility functions for parsing an OpenAPI spec.    )annotationsN)Enum)Path)TYPE_CHECKINGDictListOptionalUnion)ValidationErrorc                  B    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y)HTTPVerbzEnumerator of the HTTP verbs.getputpostdeleteoptionsheadpatchtracec                ^    	  | |      S # t         $ r t        d| j                         w xY w)zParse an HTTP verb.z$Invalid HTTP verb. Valid values are )
ValueError__members__)clsverbs     d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/openapi.pyfrom_strzHTTPVerb.from_str    s;    	Wt9 	WCCOOCTUVV	Ws   
 ",N)r   strreturnr   )__name__
__module____qualname____doc__GETPUTPOSTDELETEOPTIONSHEADPATCHTRACEclassmethodr        r   r   r      sA    '
C
CDFGDEE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dd       Z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'dZ	 	 d(	 	 	 	 	 	 	 d)dZd'dZ	 	 	 	 d*dZ	 	 	 	 d+dZed,d       Zed- fd       Zed.d       Zed/d       Zed0d       Zed1d       Zed2d       Zd3dZd4dZd5dZd6dZ 	 	 	 	 d7dZ!ed8d       Z" xZ#S )9OpenAPISpecz;OpenAPI Model that removes mis-formatted parts of the spec.3.1.0r   openapic                H    | j                   st        d      | j                   S )NzNo paths found in spec)pathsr   selfs    r   _paths_strictzOpenAPISpec._paths_strict@   s    zz566zzr-   c                \    | j                   j                  |      }|st        d|       |S )NzNo path found for )r?   r   r   )r>   path	path_items      r   _get_path_strictzOpenAPISpec._get_path_strictF   s4    &&**40	1$899r-   c                H    | j                   t        d      | j                   S )zGet components or err.zNo components found in spec. )
componentsr   r=   s    r   _components_strictzOpenAPISpec._components_strictL   s$     ??"<==r-   c                L    | j                   j                  }|t        d      |S )zGet parameters or err.zNo parameters found in spec. )rF   
parametersr   )r>   rH   s     r   _parameters_strictzOpenAPISpec._parameters_strictS   s-     ,,77
<==r-   c                L    | j                   j                  }|t        d      |S )z%Get the dictionary of schemas or err.zNo schemas found in spec. )rF   schemasr   )r>   rK   s     r   _schemas_strictzOpenAPISpec._schemas_strict[   s+     ))11?9::r-   c                L    | j                   j                  }|t        d      |S )zGet the request body or err.zNo request body found in spec. )rF   requestBodiesr   )r>   request_bodiess     r   _request_bodies_strictz"OpenAPISpec._request_bodies_strictc   s-     00>>!>??r-   c                    |j                   j                  d      d   }| j                  }||vrt        d|       ||   S )z-Get a parameter (or nested reference) or err./zNo parameter found for )refsplitrI   r   )r>   rT   ref_namerH   s       r   _get_referenced_parameterz%OpenAPISpec._get_referenced_parameterk   sJ    77==%b),,
:%6xjABB(##r-   c                    ddl m} | j                  |      }t        ||      r| j                  |      }t        ||      r|S zGet the root reference or err.r   r3   )openapi_pydanticr3   rW   
isinstance)r>   rT   r3   	parameters       r   _get_root_referenced_parameterz*OpenAPISpec._get_root_referenced_parameters   sA    .2237	I.66yAI I.r-   c                    |j                   j                  d      d   }| j                  }||vrt        d|       ||   S )z*Get a schema (or nested reference) or err.rR   rS   zNo schema found for )rT   rU   rL   r   )r>   rT   rV   rK   s       r   get_referenced_schemaz!OpenAPISpec.get_referenced_schema|   sJ    77==%b)&&7"3H:>??x  r-   c                |   |||k\  rt        d| d      ddlm} t        ||      r| j	                  |      }|j
                  E|j
                  j                         D ](  \  }}| j                  ||dz   |      |j
                  |<   * |j                  %| j                  |j                  |dz   |      |_        |S )NzMax depth of z- has been exceeded when resolving references.r   rZ      )RecursionErrorr[   r3   r\   r`   
propertiesitems
get_schema)r>   schemadepth	max_depthr3   p_nameps          r   rf   zOpenAPISpec.get_schema   s      Ui%7 	{*WX  	/fi(//7F (#..446 U	,0OOAuqy),T!!&)U <<#??6<<INFLr-   c                    ddl m} | j                  |      }t        ||      r| j                  |      }t        ||      r|S rY   )r[   r3   r`   r\   )r>   rT   r3   rg   s       r   _get_root_referenced_schemaz'OpenAPISpec._get_root_referenced_schema   s@    .++C0+//7F +r-   c                    |j                   j                  d      d   }| j                  }||vrt        d|       ||   S )z0Get a request body (or nested reference) or err.rR   rS   zNo request body found for )rT   rU   rP   r   )r>   rT   rV   rO   s       r   _get_referenced_request_bodyz(OpenAPISpec._get_referenced_request_body   sL     77==%b)44>)9(DEEh''r-   c                    ddl m} | j                  |      }t        ||      r| j                  |      }t        ||      r|S )z!Get the root request Body or err.r   rZ   )r[   r3   ro   r\   )r>   rT   r3   request_bodys       r   !_get_root_referenced_request_bodyz-OpenAPISpec._get_root_referenced_request_body   sC     	/88=y1<<\JL y1r-   c                (   d}| j                  d      }| j                  d      }t        |t              r"|dk7  rt        j	                  d| d|        y
y
t        |t              rt        j	                  d| d|        y
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   )objwarning_messageswagger_versionopenapi_versions       r   _alert_unsupported_specz#OpenAPISpec._alert_unsupported_spec   s    % 	
 ''),''),os+')4_4Eo.0
 -NN//@/*,
 =cU"_DUV r-   c                >   	 | j                  |       t        | 	  |      S # t        $ rp}t	        j
                  |      }|j                         D ]-  }|d   }|}|d d D ]  }||   }	 |j                  |d   d        / | j                  |      cY d }~S d }~ww xY w)NlocrS   )r|   super	parse_objr   copydeepcopyerrorspop)	r   rx   enew_objerrorkeysitemkey	__class__s	           r   r   zOpenAPISpec.parse_obj   s    	*'',7$S)) 
	* mmC(G )U|9 %C9D%b4() ==))
	*s   # 	BA%BBBc                $    | j                  |      S )z Get an OpenAPI spec from a dict.)r   )r   	spec_dicts     r   from_spec_dictzOpenAPISpec.from_spec_dict   s     }}Y''r-   c                    	 t        j                  |      }| j                  |      S # t         j                  $ r t        j                  |      }Y ;w xY w)z Get an OpenAPI spec from a text.)jsonloadsJSONDecodeErroryaml	safe_loadr   )r   textr   s      r   	from_textzOpenAPISpec.from_text   sM    	-

4(I !!),, ## 	-t,I	-s   ( (AAc                   t        |t              r|n
t        |      }|j                         st        | d      |j	                  d      5 }| j                  |j                               cddd       S # 1 sw Y   yxY w)z%Get an OpenAPI spec from a file path.z does not existrN)r\   r   existsFileNotFoundErroropenr   read)r   rA   path_fs       r   	from_filezOpenAPISpec.from_file   sg     #4.DJ||~#tfO$<==ZZ_ 	+==*	+ 	+ 	+s   A66A?c                b    t        j                  |      }| j                  |j                        S )zGet an OpenAPI spec from a URL.)requestsr   r   r   )r   urlresponses      r   from_urlzOpenAPISpec.from_url  s%     <<$}}X]]++r-   c                4    | j                   d   j                  S )zGet the base url.r   )serversr   r=   s    r   base_urlzOpenAPISpec.base_url  s     ||A"""r-   c                    ddl m} | j                  |      }g }t        D ]A  }t	        ||j
                  d      }t        ||      s'|j                  |j
                         C |S )z6Return a list of valid methods for the specified path.r   r/   N)r[   r/   rC   r   getattrvaluer\   append)r>   rA   r/   rB   resultsmethod	operations          r   get_methods_for_pathz OpenAPISpec.get_methods_for_path  s\    .))$/	 	-F	6<<>I)Y/v||,	- r-   c                    ddl m} | j                  |      }g }|j                  sg S |j                  D ]0  }t	        ||      r| j                  |      }|j                  |       2 |S )Nr   rZ   )r[   r3   rC   rH   r\   r^   r   )r>   rA   r3   rB   rH   r]   s         r   get_parameters_for_pathz#OpenAPISpec.get_parameters_for_path  sm    .))$/	
##I"-- 	)I)Y/ ??	J	i(	) r-   c                    ddl m} | j                  |      }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>   rA   r   r/   rB   operation_objs         r   get_operationzOpenAPISpec.get_operation%  sL    .))$/		648-3s6(*<TFCDDr-   c                    ddl m} g }|j                  r?|j                  D ]0  }t        ||      r| j	                  |      }|j                  |       2 |S )z)Get the components for a given operation.r   rZ   )r[   r3   rH   r\   r^   r   )r>   r   r3   rH   r]   s        r   get_parameters_for_operationz(OpenAPISpec.get_parameters_for_operation/  sX    .
&11 -	i3 $ C CI NI!!),- r-   c                d    ddl m} |j                  }t        ||      r| j	                  |      }|S )z+Get the request body for a given operation.r   rZ   )r[   r3   requestBodyr\   rr   )r>   r   r3   rq   s       r   get_request_body_for_operationz*OpenAPISpec.get_request_body_for_operation;  s2     	/ ,,lI.AA,OLr-   c                    | j                   }|-t        j                  dd|j                  d            }| d| }|j	                  dd      j	                  dd      j	                  dd      S )z0Get a cleaned operation id from an operation id.z[^a-zA-Z0-9]_rR   -.)operationIdresublstripreplace)r   rA   r   operation_ids       r   get_cleaned_operation_idz$OpenAPISpec.get_cleaned_operation_idF  sm     !,,66/3C0@AD"V1VH-L##C-55c3?GGSQQr-   )r   r2   )rA   r   r   r1   )r   r.   )r   z&Dict[str, Union[Parameter, Reference]])r   zDict[str, Schema])r   z(Dict[str, Union[RequestBody, Reference]])rT   r3   r   zUnion[Parameter, Reference])rT   r3   r   r0   )rT   r3   r   r5   )r   N)rg   zUnion[Reference, Schema]rh   intri   zOptional[int]r   r5   )rT   r3   r   z'Optional[Union[Reference, RequestBody]])rT   r3   r   Optional[RequestBody])rx   dictr   None)rx   r   r   r8   )r   r   r   r8   )r   r   r   r8   )rA   zUnion[str, Path]r   r8   )r   r   r   r8   )r   r   )rA   r   r   z	List[str])rA   r   r   List[Parameter])rA   r   r   r   r   r/   )r   r/   r   r   )r   r/   r   r   )r   r/   rA   r   r   r   r   r   )$r   r    r!   r"   r:   __annotations__propertyr?   rC   rF   rI   rL   rP   rW   r^   r`   rf   rm   ro   rr   staticmethodr|   r+   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r8   r8   ;   s   EGS 
        $! #'	(  !	
 
6((	0(				  6 * *  ( ( - - + + , ,
 # #

	"			 R Rr-   r8   )'r"   
__future__r   r   r   loggingr   enumr   pathlibr   typingr   r   r   r	   r
   r   r   pydanticr   	getLoggerr   rv   r   r   r[   r.   r/   r0   r1   r2   r3   r4   r5   r6   ImportErrorobjectr8   r,   r-   r   <module>r      s    4 "    	   = =   $			8	$WsD W* 	 	 	(
SR' SR	  Gs   6B BB