-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcontent.py
More file actions
74 lines (60 loc) · 2.27 KB
/
content.py
File metadata and controls
74 lines (60 loc) · 2.27 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
"""Content builder for OpenAPI request/response bodies."""
import logging
from typing import Any
from openapi_parser.builders.encoding import EncodingBuilder
from openapi_parser.builders.schema import SchemaFactory
from openapi_parser.enumeration import ContentType
from openapi_parser.loose_types import LooseContentType
from openapi_parser.specification import Content
logger = logging.getLogger(__name__)
ContentTypeType = type[ContentType] | type[LooseContentType]
class ContentBuilder:
"""Builds content objects for request/response bodies."""
_schema_factory: SchemaFactory
_encoding_builder: EncodingBuilder
_strict_enum: bool
def __init__(
self,
schema_factory: SchemaFactory,
encoding_builder: EncodingBuilder,
strict_enum: bool = True,
) -> None:
"""Initialize content builder.
Args:
schema_factory: Factory for creating schema objects
encoding_builder: Builder for encoding objects
strict_enum: Whether to validate enums strictly
"""
self._schema_factory = schema_factory
self._encoding_builder = encoding_builder
self._strict_enum = strict_enum
def build_list(self, data: dict[str, Any]) -> list[Content]:
"""Build a list of content objects from a dict of media types."""
return [
self._create_content(
content_type,
content_value,
)
for content_type, content_value in data.items()
]
def _create_content(
self,
content_type: str,
content_value: dict[str, Any],
) -> Content:
logger.debug(f"Content building [type={content_type}]")
ContentTypeCls: ContentTypeType = (
ContentType if self._strict_enum else LooseContentType
)
encoding = (
self._encoding_builder.build_dict(content_value["encoding"])
if content_value.get("encoding")
else None
)
return Content(
type=ContentTypeCls(content_type),
schema=self._schema_factory.create(content_value.get("schema", {})),
example=content_value.get("example"),
examples=content_value.get("examples", {}),
encoding=encoding,
)