-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathlink.py
More file actions
51 lines (38 loc) · 1.62 KB
/
link.py
File metadata and controls
51 lines (38 loc) · 1.62 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
"""Link builder for response links."""
import logging
from typing import Any
from openapi_parser.builders.common import (
PropertyMeta,
extract_extension_attributes,
extract_typed_props,
)
from openapi_parser.specification import Link, Server
logger = logging.getLogger(__name__)
def build_server(value: dict[str, Any]) -> Server:
"""Build a Server object from raw data."""
return Server(
url=value["url"],
description=value.get("description"),
)
class LinkBuilder:
"""Builds link objects from raw specification data."""
def build_dict(self, data: dict[str, dict[str, Any]]) -> dict[str, Link]:
"""Build a dict of links from a dict of raw link definitions."""
return {
link_name: self._build(link_data) for link_name, link_data in data.items()
}
def _build(self, data: dict[str, Any]) -> Link:
logger.debug("Link building")
attrs_map = {
"operation_ref": PropertyMeta(name="operationRef", cast=str),
"operation_id": PropertyMeta(name="operationId", cast=str),
"parameters": PropertyMeta(name="parameters", cast=dict),
"request_body": PropertyMeta(name="requestBody", cast=None),
"description": PropertyMeta(name="description", cast=str),
"server": PropertyMeta(name="server", cast=build_server),
}
attrs = extract_typed_props(data, attrs_map)
attrs["extensions"] = extract_extension_attributes(data)
if attrs["extensions"]:
logger.debug(f"Extracted custom properties [{attrs['extensions'].keys()}]")
return Link(**attrs)