-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
261 lines (213 loc) · 8.13 KB
/
Copy pathpython.py
File metadata and controls
261 lines (213 loc) · 8.13 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""ClientIR → single-file Python client renderer."""
from __future__ import annotations
import ast
from typing import Any
from hawkapi.openapi.codegen.ir import (
SENTINEL,
ClientIR,
FieldIR,
OperationIR,
SchemaIR,
)
def _py_default_repr(default: Any) -> str:
"""Return a safe Python literal string for a default value."""
if default is None:
return "None"
if isinstance(default, bool):
return "True" if default else "False"
if isinstance(default, (int, float)):
return repr(default)
if isinstance(default, str):
return repr(default)
return "None"
def _render_field_default(field: FieldIR) -> str:
"""Return the default expression for a struct field annotation."""
if field.default is SENTINEL:
return ""
return f" = {_py_default_repr(field.default)}"
def _render_struct(schema: SchemaIR) -> str:
"""Render a ``msgspec.Struct`` class for a struct SchemaIR."""
lines: list[str] = [f"class {schema.name}(msgspec.Struct):"]
if not schema.fields:
lines.append(" pass")
return "\n".join(lines)
for f in schema.fields:
default_expr = _render_field_default(f)
annotation = f" {f.name}: {f.type_str}{default_expr}"
if f.description:
lines.append(f" # {f.description}")
lines.append(annotation)
return "\n".join(lines)
def _render_alias(schema: SchemaIR) -> str:
"""Render a module-level type alias."""
return f"{schema.name} = {schema.alias_of}"
def _render_enum(schema: SchemaIR) -> str:
"""Render a ``Literal[...]`` type alias for an enum SchemaIR."""
values = ", ".join(repr(v) for v in schema.enum_values)
return f"{schema.name} = Literal[{values}]"
def _path_to_fstring(path: str) -> str:
"""Convert ``/items/{id}`` → ``f\"{self._base_url}/items/{id}\"``."""
return f'f"{{self._base_url}}{path}"'
def _render_operation(op: OperationIR) -> str:
"""Render a single async method for an operation."""
lines: list[str] = []
# Build signature parts
sig_parts: list[str] = ["self"]
# Positional path params
for p in op.path_params:
sig_parts.append(f"{p.name}: {p.type_str}")
# Keyword-only params after *
kw_parts: list[str] = []
for p in op.query_params:
if p.required:
kw_parts.append(f"{p.name}: {p.type_str}")
else:
kw_parts.append(f"{p.name}: {p.type_str} | None = None")
for p in op.header_params:
if p.required:
kw_parts.append(f"{p.name}: {p.type_str}")
else:
kw_parts.append(f"{p.name}: {p.type_str} | None = None")
if op.body_type:
kw_parts.append(f"body: {op.body_type} | None = None")
if kw_parts:
sig_parts.append("*")
sig_parts.extend(kw_parts)
return_type = op.response_type if op.response_type else "None"
sig = ", ".join(sig_parts)
lines.append(f" async def {op.operation_id}({sig}) -> {return_type}:")
# Docstring
if op.summary or op.description:
doc = op.summary or op.description or ""
lines.append(f' """{doc}"""')
# URL
url_expr = _path_to_fstring(op.path)
lines.append(f" url = {url_expr}")
# Query params dict
if op.query_params:
lines.append(" params: dict[str, Any] = {}")
for p in op.query_params:
if p.required:
lines.append(f' params["{p.name}"] = {p.name}')
else:
lines.append(f" if {p.name} is not None:")
lines.append(f' params["{p.name}"] = {p.name}')
else:
lines.append(" params: dict[str, Any] = {}")
# Headers
if op.header_params:
lines.append(" extra_headers: dict[str, str] = {}")
for p in op.header_params:
if p.required:
lines.append(f' extra_headers["{p.name}"] = {p.name}')
else:
lines.append(f" if {p.name} is not None:")
lines.append(f' extra_headers["{p.name}"] = str({p.name})')
lines.append(" merged_headers = {**self._headers, **extra_headers}")
else:
lines.append(" merged_headers = self._headers")
# Body + HTTP call
if op.body_type:
lines.append(" content = msgspec.json.encode(body) if body is not None else None")
lines.append(
f" r = await self._client.{op.method}("
"url, params=params, headers=merged_headers, content=content)"
)
else:
lines.append(
f" r = await self._client.{op.method}("
"url, params=params, headers=merged_headers)"
)
# Error check
lines.append(" if r.status_code >= 400:")
lines.append(" raise ApiError(r.status_code, r.json())")
# Return
if op.response_type:
lines.append(f" return msgspec.convert(r.json(), type={return_type})")
else:
lines.append(" return None")
return "\n".join(lines)
def generate_python_client(ir: ClientIR) -> str:
"""Render *ir* as a single-file Python async client.
Parameters
----------
ir:
Populated :class:`~hawkapi.openapi.codegen.ir.ClientIR`.
Returns
-------
str
Complete Python source code for ``client.py``.
The output is validated with :func:`ast.parse` before returning.
"""
parts: list[str] = []
# File header docstring
parts.append(
f'"""Generated HawkAPI client for {ir.title} v{ir.version} — do not edit by hand."""'
)
parts.append("")
parts.append("from __future__ import annotations")
parts.append("")
parts.append("from typing import Any, Literal")
parts.append("")
parts.append("import httpx")
parts.append("import msgspec")
parts.append("")
parts.append("")
# ApiError
parts.append("class ApiError(Exception):")
parts.append(' """Raised when the server returns a 4xx or 5xx response."""')
parts.append("")
parts.append(" def __init__(self, status_code: int, detail: Any) -> None:")
parts.append(' super().__init__(f"HTTP {status_code}: {detail}")')
parts.append(" self.status_code = status_code")
parts.append(" self.detail = detail")
parts.append("")
parts.append("")
# Schemas
for schema in ir.schemas:
if schema.kind == "struct":
parts.append(_render_struct(schema))
elif schema.kind == "alias":
parts.append(_render_alias(schema))
elif schema.kind == "enum":
parts.append(_render_enum(schema))
parts.append("")
parts.append("")
# Client class
parts.append("class Client:")
parts.append(f' """Async HTTP client for {ir.title} v{ir.version}."""')
parts.append("")
# __init__
parts.append(
" def __init__("
"self, base_url: str, *, "
"headers: dict[str, str] | None = None, "
"client: httpx.AsyncClient | None = None"
") -> None:"
)
parts.append(' self._base_url = base_url.rstrip("/")')
parts.append(" self._headers: dict[str, str] = dict(headers) if headers else {}")
parts.append(" self._owned = client is None")
parts.append(" self._client = client or httpx.AsyncClient()")
parts.append("")
# aclose
parts.append(" async def aclose(self) -> None:")
parts.append(' """Close the underlying HTTP client if owned."""')
parts.append(" if self._owned:")
parts.append(" await self._client.aclose()")
parts.append("")
# async context manager
parts.append(" async def __aenter__(self) -> Client:")
parts.append(" return self")
parts.append("")
parts.append(" async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:")
parts.append(" await self.aclose()")
parts.append("")
# Operations
for op in ir.operations:
parts.append(_render_operation(op))
parts.append("")
source = "\n".join(parts)
# Validate — raises SyntaxError on bad output
ast.parse(source)
return source