-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathencoding.py
More file actions
57 lines (43 loc) · 1.8 KB
/
encoding.py
File metadata and controls
57 lines (43 loc) · 1.8 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
"""Encoding builder for request body property encodings."""
import logging
from typing import Any
from openapi_parser.builders.common import (
PropertyMeta,
extract_extension_attributes,
extract_typed_props,
)
from openapi_parser.builders.header import HeaderBuilder
from openapi_parser.specification import Encoding
logger = logging.getLogger(__name__)
class EncodingBuilder:
"""Builds encoding objects from raw specification data."""
_header_builder: HeaderBuilder
def __init__(self, header_builder: HeaderBuilder) -> None:
"""Initialize encoding builder.
Args:
header_builder: Builder for header objects
"""
self._header_builder = header_builder
def build_dict(self, data: dict[str, dict[str, Any]]) -> dict[str, Encoding]:
"""Build a dict of encodings from a dict of raw encoding definitions."""
return {
property_name: self._build(encoding_data)
for property_name, encoding_data in data.items()
}
def _build(self, data: dict[str, Any]) -> Encoding:
logger.debug("Encoding building")
attrs_map = {
"content_type": PropertyMeta(name="contentType", cast=str),
"headers": PropertyMeta(
name="headers",
cast=self._header_builder.build_list,
),
"style": PropertyMeta(name="style", cast=str),
"explode": PropertyMeta(name="explode", cast=bool),
"allow_reserved": PropertyMeta(name="allowReserved", cast=bool),
}
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 Encoding(**attrs)