Source code for langchain.utilities.openapi
"""Utility functions for parsing an OpenAPI spec."""
from __future__ import annotations
import copy
import json
import logging
import re
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Union
import requests
import yaml
from langchain.pydantic_v1 import _PYDANTIC_MAJOR_VERSION, ValidationError
logger = logging.getLogger(__name__)
[docs]class HTTPVerb(str, Enum):
    """Enumerator of the HTTP verbs."""
    GET = "get"
    PUT = "put"
    POST = "post"
    DELETE = "delete"
    OPTIONS = "options"
    HEAD = "head"
    PATCH = "patch"
    TRACE = "trace"
    @classmethod
    def from_str(cls, verb: str) -> HTTPVerb:
        """Parse an HTTP verb."""
        try:
            return cls(verb)
        except ValueError:
            raise ValueError(f"Invalid HTTP verb. Valid values are {cls.__members__}")
if _PYDANTIC_MAJOR_VERSION == 1:
    if TYPE_CHECKING:
        from openapi_schema_pydantic import (
            Components,
            Operation,
            Parameter,
            PathItem,
            Paths,
            Reference,
            RequestBody,
            Schema,
        )
    try:
        from openapi_schema_pydantic import OpenAPI
    except ImportError:
        OpenAPI = object  # type: ignore
    class OpenAPISpec(OpenAPI):
        """OpenAPI Model that removes mis-formatted parts of the spec."""
        @property
        def _paths_strict(self) -> Paths:
            if not self.paths:
                raise ValueError("No paths found in spec")
            return self.paths