1
1
""" Generate modern Python clients from OpenAPI """
2
+ from __future__ import annotations
3
+
2
4
import json
5
+ import shutil
3
6
from pathlib import Path
4
7
from typing import Any , Dict , Optional
5
8
9
12
from .openapi_parser import OpenAPI , import_string_from_reference
10
13
11
14
12
- def main (* , url : Optional [str ], path : Optional [Path ]) -> None :
13
- """ Generate the client library """
15
+ def _get_project_for_url_or_path (url : Optional [str ], path : Optional [Path ]) -> _Project :
14
16
data_dict = _get_json (url = url , path = path )
15
17
openapi = OpenAPI .from_dict (data_dict )
16
- project = _Project (openapi = openapi )
18
+ return _Project (openapi = openapi )
19
+
20
+
21
+ def create_new_client (* , url : Optional [str ], path : Optional [Path ]) -> None :
22
+ """ Generate the client library """
23
+ project = _get_project_for_url_or_path (url = url , path = path )
17
24
project .build ()
18
25
19
26
27
+ def update_existing_client (* , url : Optional [str ], path : Optional [Path ]) -> None :
28
+ """ Update an existing client library """
29
+ project = _get_project_for_url_or_path (url = url , path = path )
30
+ project .update ()
31
+
32
+
20
33
def _get_json (* , url : Optional [str ], path : Optional [Path ]) -> Dict [str , Any ]:
21
34
json_bytes : bytes
22
35
if url is not None and path is not None :
@@ -41,29 +54,44 @@ def __init__(self, *, openapi: OpenAPI) -> None:
41
54
42
55
self .package_name : str = self .project_name .replace ("-" , "_" )
43
56
self .package_dir : Path = self .project_dir / self .package_name
57
+ self .package_description = f"A client library for accessing { self .openapi .title } "
44
58
45
59
def build (self ) -> None :
46
60
""" Create the project from templates """
61
+
47
62
print (f"Generating { self .project_name } " )
48
63
self .project_dir .mkdir ()
49
- self .package_dir . mkdir ()
64
+ self ._create_package ()
50
65
self ._build_metadata ()
51
66
self ._build_models ()
52
67
self ._build_api ()
53
68
54
- def _build_metadata (self ) -> None :
69
+ def update (self ) -> None :
70
+ """ Update an existing project """
71
+
72
+ if not self .package_dir .is_dir ():
73
+ raise FileNotFoundError ()
74
+ print (f"Updating { self .project_name } " )
75
+ shutil .rmtree (self .package_dir )
76
+ self ._create_package ()
77
+ self ._build_models ()
78
+ self ._build_api ()
79
+
80
+ def _create_package (self ) -> None :
81
+ self .package_dir .mkdir ()
55
82
# Package __init__.py
56
83
package_init = self .package_dir / "__init__.py"
57
- package_description = f"A client library for accessing { self . openapi . title } "
84
+
58
85
package_init_template = self .env .get_template ("package_init.pyi" )
59
- package_init .write_text (package_init_template .render (description = package_description ))
86
+ package_init .write_text (package_init_template .render (description = self . package_description ))
60
87
88
+ def _build_metadata (self ) -> None :
61
89
# Create a pyproject.toml file
62
90
pyproject_template = self .env .get_template ("pyproject.toml" )
63
91
pyproject_path = self .project_dir / "pyproject.toml"
64
92
pyproject_path .write_text (
65
93
pyproject_template .render (
66
- project_name = self .project_name , package_name = self .package_name , description = package_description
94
+ project_name = self .project_name , package_name = self .package_name , description = self . package_description
67
95
)
68
96
)
69
97
@@ -72,7 +100,7 @@ def _build_metadata(self) -> None:
72
100
readme_template = self .env .get_template ("README.md" )
73
101
readme .write_text (
74
102
readme_template .render (
75
- project_name = self .project_name , description = package_description , package_name = self .package_name
103
+ project_name = self .project_name , description = self . package_description , package_name = self .package_name
76
104
)
77
105
)
78
106
0 commit comments