33
44import httpx
55from httpx import ConnectError , ConnectTimeout
6- from orjson import orjson , JSONDecodeError
6+ import orjson
77import typer
88from pydantic import ValidationError
99import autopep8
@@ -21,34 +21,40 @@ def write_code(path: Path, content):
2121 :param path: The path to the file.
2222 :param content: The content to write.
2323 """
24- with open (path , 'w' ) as f :
24+ with open (path , "w" ) as f :
2525 f .write (autopep8 .fix_code (content ))
2626
2727
28- def get_open_api (path : Union [str ,Path ]) -> OpenAPI :
28+ def get_open_api (path : Union [str , Path ]) -> OpenAPI :
2929 """
3030 Tries to fetch the openapi.json file from the web or load from a local file. Returns the according OpenAPI object.
3131 :param path:
3232 :return:
3333 """
3434 try :
35- if not isinstance (path ,Path ) and (path .startswith ("http://" ) or path .startswith ("https://" )):
35+ if not isinstance (path , Path ) and (
36+ path .startswith ("http://" ) or path .startswith ("https://" )
37+ ):
3638 return OpenAPI (** orjson .loads (httpx .get (path ).text ))
3739
38- with open (path , 'r' ) as f :
40+ with open (path , "r" ) as f :
3941 return OpenAPI (** orjson .loads (f .read ()))
4042 except FileNotFoundError :
41- typer .echo (f"File { path } not found. Please make sure to pass the path to the OpenAPI 3.0 specification." )
43+ typer .echo (
44+ f"File { path } not found. Please make sure to pass the path to the OpenAPI 3.0 specification."
45+ )
4246 raise
43- except (ConnectError ,ConnectTimeout ):
47+ except (ConnectError , ConnectTimeout ):
4448 typer .echo (f"Could not connect to { path } ." )
45- raise ConnectError (f"Could not connect to { path } ." )
46- except (ValidationError ,JSONDecodeError ):
47- typer .echo (f"File { path } is not a valid OpenAPI 3.0 specification, or there may be a problem with your JSON." )
49+ raise ConnectError (f"Could not connect to { path } ." ) from None
50+ except (ValidationError , orjson .JSONDecodeError ):
51+ typer .echo (
52+ f"File { path } is not a valid OpenAPI 3.0 specification, or there may be a problem with your JSON."
53+ )
4854 raise
4955
5056
51- def write_data (data : ConversionResult , output : str ):
57+ def write_data (data : ConversionResult , output : Union [ str , Path ] ):
5258 """
5359 This function will firstly create the folderstrucutre of output, if it doesn't exist. Then it will create the
5460 models from data.models into the models sub module of the output folder. After this, the services will be created
@@ -76,26 +82,42 @@ def write_data(data: ConversionResult, output: str):
7682 write_code (models_path / f"{ model .file_name } .py" , model .content )
7783
7884 # Create models.__init__.py file containing imports to all models.
79- write_code (models_path / "__init__.py" ,"\n " .join ([f'from { file } import *' for file in files ]))
85+ write_code (
86+ models_path / "__init__.py" ,
87+ "\n " .join ([f"from { file } import *" for file in files ]),
88+ )
8089
8190 files = []
8291
8392 # Write the services.
8493 for service in data .services :
8594 files .append (service .file_name )
86- write_code (services_path / f"{ service .file_name } .py" , JINJA_ENV .get_template (SERVICE_TEMPLATE ).render (** service .dict ()))
95+ write_code (
96+ services_path / f"{ service .file_name } .py" ,
97+ JINJA_ENV .get_template (SERVICE_TEMPLATE ).render (** service .dict ()),
98+ )
8799
88100 # Create services.__init__.py file containing imports to all services.
89- write_code (services_path / "__init__.py" , "\n " .join ([f'from { file } import *' for file in files ]))
101+ write_code (
102+ services_path / "__init__.py" ,
103+ "\n " .join ([f"from { file } import *" for file in files ]),
104+ )
90105
91106 # Write the api_config.py file.
92107 write_code (Path (output ) / "api_config.py" , data .api_config .content )
93108
94109 # Write the __init__.py file.
95- write_code (Path (output ) / "__init__.py" , "from models import *\n from services import *\n from api_config import *" )
110+ write_code (
111+ Path (output ) / "__init__.py" ,
112+ "from models import *\n from services import *\n from api_config import *" ,
113+ )
96114
97115
98- def generate_data (file_name : str , output : str , library : Optional [HTTPLibrary ] = HTTPLibrary .httpx ) -> None :
116+ def generate_data (
117+ file_name : Union [str , Path ],
118+ output : Union [str , Path ],
119+ library : Optional [HTTPLibrary ] = HTTPLibrary .httpx ,
120+ ) -> None :
99121 """
100122 Generate Python code from an OpenAPI 3.0 specification.
101123 """
0 commit comments