forked from openapi-generators/openapi-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbodies.py
More file actions
125 lines (112 loc) · 3.74 KB
/
bodies.py
File metadata and controls
125 lines (112 loc) · 3.74 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import sys
from typing import List, Tuple, Union
import attr
from openapi_python_client.parser.properties import (
ModelProperty,
Property,
Schemas,
property_from_data,
)
from .. import schema as oai
from ..config import Config
from ..utils import get_content_type
from .errors import ErrorLevel, ParseError
if sys.version_info >= (3, 11):
from enum import StrEnum
class BodyType(StrEnum):
JSON = "json"
DATA = "data"
FILES = "files"
CONTENT = "content"
else:
from enum import Enum
class BodyType(str, Enum):
JSON = "json"
DATA = "data"
FILES = "files"
CONTENT = "content"
@attr.define
class Body:
content_type: str
prop: Property
body_type: BodyType
def body_from_data(
*,
data: oai.Operation,
schemas: Schemas,
config: Config,
endpoint_name: str,
) -> Tuple[List[Union[Body, ParseError]], Schemas]:
"""Adds form or JSON body to Endpoint if included in data"""
if data.request_body is None or isinstance(data.request_body, oai.Reference):
return [], schemas
bodies: List[Union[Body, ParseError]] = []
body_content = data.request_body.content
prefix_type_names = len(body_content) > 1
for content_type, media_type in body_content.items():
simplified_content_type = get_content_type(content_type)
if simplified_content_type is None:
bodies.append(
ParseError(
detail="Invalid content type",
data=data.request_body,
level=ErrorLevel.WARNING,
)
)
continue
media_type_schema = media_type.media_type_schema
if media_type_schema is None:
bodies.append(
ParseError(
detail="Missing schema",
data=data.request_body,
level=ErrorLevel.WARNING,
)
)
continue
if simplified_content_type == "application/x-www-form-urlencoded":
body_type = BodyType.DATA
elif simplified_content_type == "multipart/form-data":
body_type = BodyType.FILES
elif simplified_content_type == "application/octet-stream":
body_type = BodyType.CONTENT
elif simplified_content_type == "application/json" or simplified_content_type.endswith("+json"):
body_type = BodyType.JSON
else:
bodies.append(
ParseError(
detail=f"Unsupported content type {simplified_content_type}",
data=data.request_body,
level=ErrorLevel.WARNING,
)
)
continue
prop, schemas = property_from_data(
name="body",
required=True,
data=media_type_schema,
schemas=schemas,
parent_name=f"{endpoint_name}_{body_type}" if prefix_type_names else endpoint_name,
config=config,
)
if isinstance(prop, ParseError):
bodies.append(prop)
continue
if isinstance(prop, ModelProperty) and body_type == BodyType.FILES:
# Regardless of if we just made this property or found it, it now needs the `to_multipart` method
prop = attr.evolve(prop, is_multipart_body=True)
schemas = attr.evolve(
schemas,
classes_by_name={
**schemas.classes_by_name,
prop.class_info.name: prop,
},
)
bodies.append(
Body(
content_type=content_type,
prop=prop,
body_type=body_type,
)
)
return bodies, schemas