11""" Generate modern Python clients from OpenAPI """
2+ from __future__ import annotations
3+
24import json
5+ import shutil
36from pathlib import Path
47from typing import Any , Dict , Optional
58
912from .openapi_parser import OpenAPI , import_string_from_reference
1013
1114
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 :
1416 data_dict = _get_json (url = url , path = path )
1517 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 )
1724 project .build ()
1825
1926
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+
2033def _get_json (* , url : Optional [str ], path : Optional [Path ]) -> Dict [str , Any ]:
2134 json_bytes : bytes
2235 if url is not None and path is not None :
@@ -41,29 +54,44 @@ def __init__(self, *, openapi: OpenAPI) -> None:
4154
4255 self .package_name : str = self .project_name .replace ("-" , "_" )
4356 self .package_dir : Path = self .project_dir / self .package_name
57+ self .package_description = f"A client library for accessing { self .openapi .title } "
4458
4559 def build (self ) -> None :
4660 """ Create the project from templates """
61+
4762 print (f"Generating { self .project_name } " )
4863 self .project_dir .mkdir ()
49- self .package_dir . mkdir ()
64+ self ._create_package ()
5065 self ._build_metadata ()
5166 self ._build_models ()
5267 self ._build_api ()
5368
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 ()
5582 # Package __init__.py
5683 package_init = self .package_dir / "__init__.py"
57- package_description = f"A client library for accessing { self . openapi . title } "
84+
5885 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 ))
6087
88+ def _build_metadata (self ) -> None :
6189 # Create a pyproject.toml file
6290 pyproject_template = self .env .get_template ("pyproject.toml" )
6391 pyproject_path = self .project_dir / "pyproject.toml"
6492 pyproject_path .write_text (
6593 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
6795 )
6896 )
6997
@@ -72,7 +100,7 @@ def _build_metadata(self) -> None:
72100 readme_template = self .env .get_template ("README.md" )
73101 readme .write_text (
74102 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
76104 )
77105 )
78106
0 commit comments