-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpath.py
More file actions
75 lines (57 loc) · 2.38 KB
/
path.py
File metadata and controls
75 lines (57 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Path builder for API endpoint paths."""
import logging
from typing import Any
from openapi_parser.builders.common import (
PropertyMeta,
extract_extension_attributes,
extract_typed_props,
)
from openapi_parser.builders.operation import OperationBuilder
from openapi_parser.builders.parameter import ParameterBuilder
from openapi_parser.enumeration import OperationMethod
from openapi_parser.specification import Path
logger = logging.getLogger(__name__)
class PathBuilder:
"""Builds path objects from raw specification data."""
_operation_builder: OperationBuilder
_parameter_builder: ParameterBuilder
def __init__(
self,
operation_builder: OperationBuilder,
parameter_builder: ParameterBuilder,
) -> None:
"""Initialize path builder.
Args:
operation_builder: Builder for operation objects
parameter_builder: Builder for parameter objects
"""
self._operation_builder = operation_builder
self._parameter_builder = parameter_builder
def build_list(self, data: dict[str, dict[str, Any]]) -> list[Path]:
"""Build a list of paths from a raw dict of path definitions."""
return [self._build_path(url, path) for url, path in data.items()]
def _build_path(self, url: str, data: dict[str, Any]) -> Path:
logger.info(f"Path item parsing [url={url}]")
attrs_map = {
"summary": PropertyMeta(name="summary", cast=str),
"description": PropertyMeta(name="description", cast=str),
"parameters": PropertyMeta(
name="parameters",
cast=self._parameter_builder.build_list,
),
}
attrs = extract_typed_props(data, attrs_map)
attrs["url"] = url
attrs["operations"] = [
self._operation_builder.build(method, data[method.value])
for method in OperationMethod
if method.value in data
]
if attrs.get("parameters"):
for operation in attrs["operations"]:
merged = operation.parameters + attrs["parameters"]
object.__setattr__(operation, "parameters", merged)
attrs["extensions"] = extract_extension_attributes(data)
if attrs["extensions"]:
logger.debug(f"Extracted custom properties [{attrs['extensions'].keys()}]")
return Path(**attrs)