Skip to content

Commit

Permalink
add concept of detectors
Browse files Browse the repository at this point in the history
remove some unneeded stuff
  • Loading branch information
sh-rp committed May 6, 2024
1 parent 13d900e commit 8db4b8b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
27 changes: 27 additions & 0 deletions openapi_python_client/detectors/base_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
Basic detector class
"""

from abc import ABC, abstractmethod


class BaseDetector(ABC):
@abstractmethod
def detect_pagination(self) -> None:
...

@abstractmethod
def detect_payload_path(self) -> None:
...

@abstractmethod
def detect_authentication(self) -> None:
...

@abstractmethod
def detect_primary_key(self) -> None:
...

@abstractmethod
def detect_parent_endpoint(self) -> None:
...
22 changes: 22 additions & 0 deletions openapi_python_client/detectors/default/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Default open source detector
"""

from openapi_python_client.detectors.base_detector import BaseDetector


class DefaultDetector(BaseDetector):
def detect_pagination(self) -> None:
...

def detect_payload_path(self) -> None:
...

def detect_authentication(self) -> None:
...

def detect_primary_key(self) -> None:
...

def detect_parent_endpoint(self) -> None:
...
File renamed without changes.
6 changes: 4 additions & 2 deletions openapi_python_client/parser/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from openapi_python_client.parser.config import Config
from openapi_python_client.utils import ClassName

from openapi_python_client.detectors.base_detector import BaseDetector

TComponentClass = Union[
osp.Schema,
Expand Down Expand Up @@ -46,16 +46,18 @@ def location(self) -> str:
class OpenapiContext:
spec: osp.OpenAPI
spec_raw: Dict[str, Any]
detector: BaseDetector

_component_cache: Dict[str, Dict[str, Any]]
security_schemes: Dict[str, SecurityScheme]

def __init__(self, config: Config, spec: osp.OpenAPI, spec_raw: Dict[str, Any]) -> None:
def __init__(self, config: Config, spec: osp.OpenAPI, spec_raw: Dict[str, Any], detector: BaseDetector) -> None:
self.config = config
self.spec = spec
self.spec_raw = spec_raw
self._component_cache = {}
self.security_schemes = {}
self.detector = detector
resource = referencing.Resource( # type: ignore[var-annotated, call-arg]
contents=self.spec_raw, specification=referencing.jsonschema.DRAFT202012
)
Expand Down
19 changes: 1 addition & 18 deletions openapi_python_client/parser/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Optional, Literal, cast, Union, List, Dict, Iterable, Tuple, Set
from typing import Optional, Literal, cast, Union, List, Dict, Iterable, Set

from dataclasses import dataclass, field

Expand All @@ -10,7 +10,6 @@
from openapi_python_client.parser.paths import table_names_from_paths, get_path_parts, is_var_part
from openapi_python_client.parser.models import SchemaWrapper, DataPropertyPath
from openapi_python_client.utils import PythonIdentifier
from openapi_python_client.parser.credentials import CredentialsProperty
from openapi_python_client.parser.pagination import Pagination
from openapi_python_client.parser.parameters import Parameter
from .const import RE_MATCH_ALL
Expand Down Expand Up @@ -52,20 +51,6 @@ class Response:
payload: Optional[DataPropertyPath] = None
"""Payload set initially before comparing other endpoints"""

@property
def list_properties(self) -> Dict[Tuple[str, ...], SchemaWrapper]:
"""Paths to list properties"""
if not self.content_schema:
return {}
return self.content_schema.crawled_properties.list_properties

@property
def object_properties(self) -> Dict[Tuple[str, ...], SchemaWrapper]:
"""Paths to object properties"""
if not self.content_schema:
return {}
return self.content_schema.crawled_properties.object_properties

@classmethod
def from_reference(
cls,
Expand Down Expand Up @@ -132,7 +117,6 @@ class Endpoint:
operation_id: str

python_name: PythonIdentifier
credentials: Optional[CredentialsProperty]

_parent: Optional["Endpoint"] = None
children: List["Endpoint"] = field(default_factory=list)
Expand Down Expand Up @@ -296,7 +280,6 @@ def from_operation(
description=operation.description,
path_summary=path_summary,
path_description=path_description,
credentials=None,
)
endpoint.pagination = Pagination.from_endpoint(endpoint)

Expand Down
9 changes: 7 additions & 2 deletions openapi_python_client/parser/openapi_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from openapi_python_client.parser.config import Config
from openapi_python_client.parser.info import OpenApiInfo
from openapi_python_client.parser.credentials import CredentialsProperty

from openapi_python_client.detectors.base_detector import BaseDetector

log = logging.getLogger(__name__)

Expand All @@ -26,6 +26,7 @@ class OpenapiParser:
info: OpenApiInfo
credentials: Optional[CredentialsProperty] = None
context: OpenapiContext
detector: BaseDetector

def __init__(self, spec_file: Union[Path, str], config: Config = Config()) -> None:
self.spec_file = spec_file
Expand Down Expand Up @@ -63,8 +64,12 @@ def parse(self) -> None:
# log.info("Pydantic parse")
spec = osp.OpenAPI.parse_obj(self.spec_raw)

from openapi_python_client.detectors.default import DefaultDetector

self.detector = DefaultDetector()

log.info("Extracting metadata")
self.context = OpenapiContext(self.config, spec, self.spec_raw)
self.context = OpenapiContext(self.config, spec, self.spec_raw, self.detector)
self.info = OpenApiInfo.from_context(self.context)

log.info("Parsing endpoints")
Expand Down

0 comments on commit 8db4b8b

Please sign in to comment.