FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openapi-python-client/openapi_python_client/__init__.py at editable · ramnes/openapi-python-client · GitHub
ramnes
openapi-python-client
Repository navigation
Code
Pull requests
10
(10)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openapi-python-client
/
openapi_python_client
/
__init__.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
337 lines (286 loc) · 12 KB
Breadcrumbs
openapi-python-client
/
openapi_python_client
/
__init__.py
Copy path
File metadata and controls
337 lines (286 loc) · 12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
""" Generate modern Python clients from OpenAPI """
import
shutil
import
subprocess
import
sys
from
enum
import
Enum
from
pathlib
import
Path
from
typing
import
Any
,
Dict
,
Optional
,
Sequence
,
Union
import
httpcore
import
httpx
import
yaml
from
jinja2
import
BaseLoader
,
ChoiceLoader
,
Environment
,
FileSystemLoader
,
PackageLoader
from
openapi_python_client
import
utils
from
.
parser
import
GeneratorData
,
import_string_from_reference
from
.
parser
.
errors
import
GeneratorError
from
.
utils
import
snake_case
if
sys
.
version_info
.
minor
<
8
:
# version did not exist before 3.8, need to use a backport
from
importlib_metadata
import
version
else
:
from
importlib
.
metadata
import
version
# type: ignore
__version__
=
version
(
__package__
)
class
MetaType
(
str
,
Enum
):
NONE
=
"none"
POETRY
=
"poetry"
SETUP
=
"setup"
TEMPLATE_FILTERS
=
{
"snakecase"
:
utils
.
snake_case
,
"kebabcase"
:
utils
.
kebab_case
,
"pascalcase"
:
utils
.
pascal_case
,
"any"
:
any
,
}
class
Project
:
project_name_override
:
Optional
[
str
]
=
None
package_name_override
:
Optional
[
str
]
=
None
package_version_override
:
Optional
[
str
]
=
None
def
__init__
(
self
,
*
,
openapi
:
GeneratorData
,
meta
:
MetaType
,
custom_template_path
:
Optional
[
Path
]
=
None
,
file_encoding
:
str
=
"utf-8"
,
)
->
None
:
self
.
openapi
:
GeneratorData
=
openapi
self
.
meta
:
MetaType
=
meta
self
.
file_encoding
=
file_encoding
package_loader
=
PackageLoader
(
__package__
)
loader
:
BaseLoader
if
custom_template_path
is
not
None
:
loader
=
ChoiceLoader
(
[
FileSystemLoader
(
str
(
custom_template_path
)),
package_loader
,
]
)
else
:
loader
=
package_loader
self
.
env
:
Environment
=
Environment
(
loader
=
loader
,
trim_blocks
=
True
,
lstrip_blocks
=
True
)
self
.
project_name
:
str
=
self
.
project_name_override
or
f"
{
utils
.
kebab_case
(
openapi
.
title
).
lower
()
}
-client"
self
.
project_dir
:
Path
=
Path
.
cwd
()
if
meta
!=
MetaType
.
NONE
:
self
.
project_dir
/=
self
.
project_name
self
.
package_name
:
str
=
self
.
package_name_override
or
self
.
project_name
.
replace
(
"-"
,
"_"
)
self
.
package_dir
:
Path
=
self
.
project_dir
/
self
.
package_name
self
.
package_description
:
str
=
utils
.
remove_string_escapes
(
f"A client library for accessing
{
self
.
openapi
.
title
}
"
)
self
.
version
:
str
=
self
.
package_version_override
or
openapi
.
version
self
.
env
.
filters
.
update
(
TEMPLATE_FILTERS
)
def
build
(
self
)
->
Sequence
[
GeneratorError
]:
""" Create the project from templates """
if
self
.
meta
==
MetaType
.
NONE
:
print
(
f"Generating
{
self
.
package_name
}
"
)
else
:
print
(
f"Generating
{
self
.
project_name
}
"
)
try
:
self
.
project_dir
.
mkdir
()
except
FileExistsError
:
return
[
GeneratorError
(
detail
=
"Directory already exists. Delete it or use the update command."
)]
self
.
_create_package
()
self
.
_build_metadata
()
self
.
_build_models
()
self
.
_build_api
()
self
.
_reformat
()
return
self
.
_get_errors
()
def
update
(
self
)
->
Sequence
[
GeneratorError
]:
""" Update an existing project """
if
not
self
.
package_dir
.
is_dir
():
raise
FileNotFoundError
()
print
(
f"Updating
{
self
.
package_name
}
"
)
shutil
.
rmtree
(
self
.
package_dir
)
self
.
_create_package
()
self
.
_build_models
()
self
.
_build_api
()
self
.
_reformat
()
return
self
.
_get_errors
()
def
_reformat
(
self
)
->
None
:
subprocess
.
run
(
"autoflake -i -r --remove-all-unused-imports --remove-unused-variables --ignore-init-module-imports ."
,
cwd
=
self
.
package_dir
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
)
subprocess
.
run
(
"isort ."
,
cwd
=
self
.
project_dir
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
)
subprocess
.
run
(
"black ."
,
cwd
=
self
.
project_dir
,
shell
=
True
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
def
_get_errors
(
self
)
->
Sequence
[
GeneratorError
]:
errors
=
[]
for
collection
in
self
.
openapi
.
endpoint_collections_by_tag
.
values
():
errors
.
extend
(
collection
.
parse_errors
)
errors
.
extend
(
self
.
openapi
.
errors
)
return
errors
def
_create_package
(
self
)
->
None
:
self
.
package_dir
.
mkdir
()
# Package __init__.py
package_init
=
self
.
package_dir
/
"__init__.py"
package_init_template
=
self
.
env
.
get_template
(
"package_init.py.jinja"
)
package_init
.
write_text
(
package_init_template
.
render
(
description
=
self
.
package_description
),
encoding
=
self
.
file_encoding
)
if
self
.
meta
!=
MetaType
.
NONE
:
pytyped
=
self
.
package_dir
/
"py.typed"
pytyped
.
write_text
(
"# Marker file for PEP 561"
,
encoding
=
self
.
file_encoding
)
types_template
=
self
.
env
.
get_template
(
"types.py.jinja"
)
types_path
=
self
.
package_dir
/
"types.py"
types_path
.
write_text
(
types_template
.
render
(),
encoding
=
self
.
file_encoding
)
def
_build_metadata
(
self
)
->
None
:
if
self
.
meta
==
MetaType
.
NONE
:
return
self
.
_build_pyproject_toml
(
use_poetry
=
self
.
meta
==
MetaType
.
POETRY
)
if
self
.
meta
==
MetaType
.
SETUP
:
self
.
_build_setup_py
()
# README.md
readme
=
self
.
project_dir
/
"README.md"
readme_template
=
self
.
env
.
get_template
(
"README.md.jinja"
)
readme
.
write_text
(
readme_template
.
render
(
project_name
=
self
.
project_name
,
description
=
self
.
package_description
,
package_name
=
self
.
package_name
),
encoding
=
self
.
file_encoding
,
)
# .gitignore
git_ignore_path
=
self
.
project_dir
/
".gitignore"
git_ignore_template
=
self
.
env
.
get_template
(
".gitignore.jinja"
)
git_ignore_path
.
write_text
(
git_ignore_template
.
render
(),
encoding
=
self
.
file_encoding
)
def
_build_pyproject_toml
(
self
,
*
,
use_poetry
:
bool
)
->
None
:
template
=
"pyproject.toml.jinja"
if
use_poetry
else
"pyproject_no_poetry.toml.jinja"
pyproject_template
=
self
.
env
.
get_template
(
template
)
pyproject_path
=
self
.
project_dir
/
"pyproject.toml"
pyproject_path
.
write_text
(
pyproject_template
.
render
(
project_name
=
self
.
project_name
,
package_name
=
self
.
package_name
,
version
=
self
.
version
,
description
=
self
.
package_description
,
),
encoding
=
self
.
file_encoding
,
)
def
_build_setup_py
(
self
)
->
None
:
template
=
self
.
env
.
get_template
(
"setup.py.jinja"
)
path
=
self
.
project_dir
/
"setup.py"
path
.
write_text
(
template
.
render
(
project_name
=
self
.
project_name
,
package_name
=
self
.
package_name
,
version
=
self
.
version
,
description
=
self
.
package_description
,
),
encoding
=
self
.
file_encoding
,
)
def
_build_models
(
self
)
->
None
:
# Generate models
models_dir
=
self
.
package_dir
/
"models"
models_dir
.
mkdir
()
models_init
=
models_dir
/
"__init__.py"
imports
=
[]
model_template
=
self
.
env
.
get_template
(
"model.py.jinja"
)
for
model
in
self
.
openapi
.
models
.
values
():
module_path
=
models_dir
/
f"
{
model
.
reference
.
module_name
}
.py"
module_path
.
write_text
(
model_template
.
render
(
model
=
model
),
encoding
=
self
.
file_encoding
)
imports
.
append
(
import_string_from_reference
(
model
.
reference
))
# Generate enums
str_enum_template
=
self
.
env
.
get_template
(
"str_enum.py.jinja"
)
int_enum_template
=
self
.
env
.
get_template
(
"int_enum.py.jinja"
)
for
enum
in
self
.
openapi
.
enums
.
values
():
module_path
=
models_dir
/
f"
{
enum
.
reference
.
module_name
}
.py"
if
enum
.
value_type
is
int
:
module_path
.
write_text
(
int_enum_template
.
render
(
enum
=
enum
),
encoding
=
self
.
file_encoding
)
else
:
module_path
.
write_text
(
str_enum_template
.
render
(
enum
=
enum
),
encoding
=
self
.
file_encoding
)
imports
.
append
(
import_string_from_reference
(
enum
.
reference
))
models_init_template
=
self
.
env
.
get_template
(
"models_init.py.jinja"
)
models_init
.
write_text
(
models_init_template
.
render
(
imports
=
imports
),
encoding
=
self
.
file_encoding
)
def
_build_api
(
self
)
->
None
:
# Generate Client
client_path
=
self
.
package_dir
/
"client.py"
client_template
=
self
.
env
.
get_template
(
"client.py.jinja"
)
client_path
.
write_text
(
client_template
.
render
(),
encoding
=
self
.
file_encoding
)
# Generate endpoints
api_dir
=
self
.
package_dir
/
"api"
api_dir
.
mkdir
()
api_init
=
api_dir
/
"__init__.py"
api_init
.
write_text
(
'""" Contains methods for accessing the API """'
,
encoding
=
self
.
file_encoding
)
endpoint_template
=
self
.
env
.
get_template
(
"endpoint_module.py.jinja"
)
for
tag
,
collection
in
self
.
openapi
.
endpoint_collections_by_tag
.
items
():
tag_dir
=
api_dir
/
tag
tag_dir
.
mkdir
()
(
tag_dir
/
"__init__.py"
).
touch
()
for
endpoint
in
collection
.
endpoints
:
module_path
=
tag_dir
/
f"
{
snake_case
(
endpoint
.
name
)
}
.py"
module_path
.
write_text
(
endpoint_template
.
render
(
endpoint
=
endpoint
),
encoding
=
self
.
file_encoding
)
def
_get_project_for_url_or_path
(
url
:
Optional
[
str
],
path
:
Optional
[
Path
],
meta
:
MetaType
,
custom_template_path
:
Optional
[
Path
]
=
None
,
file_encoding
:
str
=
"utf-8"
,
)
->
Union
[
Project
,
GeneratorError
]:
data_dict
=
_get_document
(
url
=
url
,
path
=
path
)
if
isinstance
(
data_dict
,
GeneratorError
):
return
data_dict
openapi
=
GeneratorData
.
from_dict
(
data_dict
)
if
isinstance
(
openapi
,
GeneratorError
):
return
openapi
return
Project
(
openapi
=
openapi
,
custom_template_path
=
custom_template_path
,
meta
=
meta
,
file_encoding
=
file_encoding
)
def
create_new_client
(
*
,
url
:
Optional
[
str
],
path
:
Optional
[
Path
],
meta
:
MetaType
,
custom_template_path
:
Optional
[
Path
]
=
None
,
file_encoding
:
str
=
"utf-8"
,
)
->
Sequence
[
GeneratorError
]:
"""
Generate the client library
Returns:
A list containing any errors encountered when generating.
"""
project
=
_get_project_for_url_or_path
(
url
=
url
,
path
=
path
,
custom_template_path
=
custom_template_path
,
meta
=
meta
,
file_encoding
=
file_encoding
)
if
isinstance
(
project
,
GeneratorError
):
return
[
project
]
return
project
.
build
()
def
update_existing_client
(
*
,
url
:
Optional
[
str
],
path
:
Optional
[
Path
],
meta
:
MetaType
,
custom_template_path
:
Optional
[
Path
]
=
None
,
file_encoding
:
str
=
"utf-8"
,
)
->
Sequence
[
GeneratorError
]:
"""
Update an existing client library
Returns:
A list containing any errors encountered when generating.
"""
project
=
_get_project_for_url_or_path
(
url
=
url
,
path
=
path
,
custom_template_path
=
custom_template_path
,
meta
=
meta
,
file_encoding
=
file_encoding
)
if
isinstance
(
project
,
GeneratorError
):
return
[
project
]
return
project
.
update
()
def
_get_document
(
*
,
url
:
Optional
[
str
],
path
:
Optional
[
Path
])
->
Union
[
Dict
[
str
,
Any
],
GeneratorError
]:
yaml_bytes
:
bytes
if
url
is
not
None
and
path
is
not
None
:
return
GeneratorError
(
header
=
"Provide URL or Path, not both."
)
if
url
is
not
None
:
try
:
response
=
httpx
.
get
(
url
)
yaml_bytes
=
response
.
content
except
(
httpx
.
HTTPError
,
httpcore
.
NetworkError
):
return
GeneratorError
(
header
=
"Could not get OpenAPI document from provided URL"
)
elif
path
is
not
None
:
yaml_bytes
=
path
.
read_bytes
()
else
:
return
GeneratorError
(
header
=
"No URL or Path provided"
)
try
:
return
yaml
.
safe_load
(
yaml_bytes
)
except
yaml
.
YAMLError
:
return
GeneratorError
(
header
=
"Invalid YAML from provided source"
)
Back
|
FazBrowse Home
|
New Git URL