|
1 | 1 | {% from "client/_response.py.jinja" import build_response_model, build_error_models %} |
2 | 2 |
|
| 3 | +{% set TYPE_MAPPING = {"json": "json", "form": "data", "file": "files", "raw": "content"} %} |
| 4 | + |
3 | 5 | {% macro build_path(endpoint) %} |
4 | 6 | {% if endpoint.path_params %} |
5 | 7 | url = f"{{ endpoint.path }}" |
@@ -32,28 +34,17 @@ cookies = { |
32 | 34 | } |
33 | 35 | {% endmacro %} |
34 | 36 |
|
35 | | -{% macro _get_body_name(request_body) %} |
36 | | -{% if request_body.is_model %} |
37 | | -{{ request_body.body_schema.class_name }}(**{ |
38 | | - {% for prop in request_body.body_schema.properties %} |
39 | | - "{{ prop.name }}": {{ prop.prop_name }}, |
40 | | - {% endfor %} |
41 | | -}).dict(by_alias=True) |
42 | | -{% else %} |
43 | | -body |
44 | | -{% endif %} |
45 | | -{% endmacro %} |
46 | | - |
47 | 37 | {% macro build_body(request_body) %} |
48 | | -{% if request_body.type == "json" %} |
49 | | -json = {{ _get_body_name(request_body) }} |
50 | | -{% elif request_body.type == "form" %} |
51 | | -data = {{ _get_body_name(request_body) }} |
52 | | -{% elif request_body.type == "file" %} |
53 | | -files = {{ _get_body_name(request_body) }} |
54 | | -{% elif request_body.type == "raw" %} |
55 | | -content = {{ _get_body_name(request_body) }} |
56 | | -{% endif %} |
| 38 | +{% set name = TYPE_MAPPING[request_body.type] %} |
| 39 | +if not kwargs: |
| 40 | + kwargs = UNSET |
| 41 | + |
| 42 | +{{ name }} = kwargs if data is UNSET else data |
| 43 | +{{ name }} = parse_obj_as( |
| 44 | + {{ request_body.body_schema.get_type_string() }}, |
| 45 | + {{ name }} |
| 46 | +) |
| 47 | +{{ name }} = {{ name }}.dict(by_alias=True) if isinstance({{ name }}, BaseModel) else {{ name }} |
57 | 48 | {% endmacro %} |
58 | 49 |
|
59 | 50 | {% macro build_request(endpoint) %} |
|
73 | 64 | params=exclude_unset(params), |
74 | 65 | {% endif %} |
75 | 66 | {% if endpoint.request_body %} |
76 | | -{% if endpoint.request_body.type == "raw" %} |
77 | | -content=exclude_unset(content), |
78 | | -{% elif endpoint.request_body.type == "form" %} |
79 | | -data=exclude_unset(data), |
80 | | -{% elif endpoint.request_body.type == "file" %} |
81 | | -files=exclude_unset(files), |
82 | | -{% elif endpoint.request_body.type == "json" %} |
83 | | -json=exclude_unset(json), |
84 | | -{% endif %} |
| 67 | +{% set name = TYPE_MAPPING[endpoint.request_body.type] %} |
| 68 | +{{ name }}=exclude_unset({{ name }}), |
85 | 69 | {% endif %} |
86 | 70 | {% if endpoint.header_params %} |
87 | 71 | headers=exclude_unset(headers), |
|
0 commit comments