diff --git a/CHANGELOG.md b/CHANGELOG.md index f4682e3..06a279b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * `constants.py` has been renamed to `config.py` to better reflect how it is used. * If you are using Python 3.10 or later, the `typing.Unions` types will generate as the short hand `|` instead. This required a big rework of the code to enable templating. * To regenerate a client (and to prevent accidental overrides) you must now pass `--regen t` or `-r t` to the `generate` command. This is automatically added to the line in `MANIFEST.md` to help. +* Clientele will now automatically run [black](https://black.readthedocs.io/en/stable/) code formatter once a client is generated or regenerated. ## 0.6.3 diff --git a/Makefile b/Makefile index 5920586..9e38f69 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,5 @@ shell: ## Run an ipython shell generate-test-clients: ## regenerate the test clients in the tests/ directory poetry install - clientele generate -f example_openapi_specs/best.json -o tests/test_client/ - clientele generate -f example_openapi_specs/best.json -o tests/async_test_client/ --asyncio t - black tests/ + clientele generate -f example_openapi_specs/best.json -o tests/test_client/ --regen t + clientele generate -f example_openapi_specs/best.json -o tests/async_test_client/ --asyncio t --regen t diff --git a/README.md b/README.md index f350ce9..db45097 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,4 @@ response = await client.simple_request_simple_request_get() * Support your own configuration - we provide an entry point that will never be overwritten. * Designed for easy testing with [respx](https://lundberg.github.io/respx/). * API updated? Just run the same command again and check the git diff. +* Automatically formats the generated client with [black](https://black.readthedocs.io/en/stable/index.html). diff --git a/clientele/generator.py b/clientele/generator.py index 332637f..e10b465 100644 --- a/clientele/generator.py +++ b/clientele/generator.py @@ -6,6 +6,7 @@ from openapi_core import Spec from rich.console import Console +import subprocess from clientele.generators.clients import ClientsGenerator from clientele.generators.http import HTTPGenerator @@ -104,6 +105,13 @@ def prevent_accidental_regens(self) -> bool: return False return True + def format_client(self) -> None: + subprocess.run( + ["black", self.output_dir], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + def generate(self) -> None: copy_tree(src=CLIENT_TEMPLATE_ROOT, dst=self.output_dir) if not exists(f"{self.output_dir}/config.py"): @@ -117,3 +125,4 @@ def generate(self) -> None: self.clients_generator.generate_paths() self.http_generator.generate_http_content() self.schemas_generator.write_helpers() + self.format_client() diff --git a/clientele/templates/bearer_client.jinja2 b/clientele/templates/bearer_client.jinja2 index 6d3ac06..c629df7 100644 --- a/clientele/templates/bearer_client.jinja2 +++ b/clientele/templates/bearer_client.jinja2 @@ -2,5 +2,5 @@ auth_key = c.get_bearer_token() headers = c.additional_headers() -headers.update(Authorization=f'Bearer ' + auth_key) +headers.update(Authorization=f'Bearer {auth_key}') client = httpx.{{client_type}}(headers=headers) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f4682e3..06a279b 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,7 @@ * `constants.py` has been renamed to `config.py` to better reflect how it is used. * If you are using Python 3.10 or later, the `typing.Unions` types will generate as the short hand `|` instead. This required a big rework of the code to enable templating. * To regenerate a client (and to prevent accidental overrides) you must now pass `--regen t` or `-r t` to the `generate` command. This is automatically added to the line in `MANIFEST.md` to help. +* Clientele will now automatically run [black](https://black.readthedocs.io/en/stable/) code formatter once a client is generated or regenerated. ## 0.6.3 diff --git a/docs/index.md b/docs/index.md index f350ce9..db45097 100644 --- a/docs/index.md +++ b/docs/index.md @@ -65,3 +65,4 @@ response = await client.simple_request_simple_request_get() * Support your own configuration - we provide an entry point that will never be overwritten. * Designed for easy testing with [respx](https://lundberg.github.io/respx/). * API updated? Just run the same command again and check the git diff. +* Automatically formats the generated client with [black](https://black.readthedocs.io/en/stable/index.html). diff --git a/docs/usage.md b/docs/usage.md index 3f599e0..4ae7275 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -9,6 +9,10 @@ Generate a Python HTTP Client from an OpenAPI Schema. +!!! note + + Clientele will use [black](* Automatically formats the generated client with [black](https://black.readthedocs.io/en/stable/index.html).) to format the generated client for you. + ### From a URL Use the `-u` or `--url` argument. diff --git a/poetry.lock b/poetry.lock index 53d041e..28f8c8e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1927,4 +1927,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "c2e36808f3f1dec4df6a3f35a1a36702bde244fd2cc3953644737f274d4f412b" +content-hash = "61d6bbfd291cef0e404790dab92370c583d46342137deba52b3487c6e20a5f9c" diff --git a/pyproject.toml b/pyproject.toml index f8b0f18..ca64b87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,9 +26,9 @@ openapi-core = "0.18.0" pyyaml = "^6.0.1" types-pyyaml = "^6.0.12.11" jinja2 = "^3.1.2" +black = "^23.9.1" [tool.poetry.group.dev.dependencies] -black = "^23.3.0" mypy = "1.4.0" ruff = "^0.0.272" mkdocs = "^1.4.3" diff --git a/tests/async_test_client/client.py b/tests/async_test_client/client.py index d04164c..be02741 100644 --- a/tests/async_test_client/client.py +++ b/tests/async_test_client/client.py @@ -11,7 +11,7 @@ async def complex_model_request_complex_model_request_get() -> schemas.ComplexModelResponse: """Complex Model Request""" - response = await http.get(url=f"/complex-model-request") + response = await http.get(url="/complex-model-request") return http.handle_response( complex_model_request_complex_model_request_get, response ) @@ -24,14 +24,14 @@ async def header_request_header_request_get( headers_dict = ( headers and headers.model_dump(by_alias=True, exclude_unset=True) or None ) - response = await http.get(url=f"/header-request", headers=headers_dict) + response = await http.get(url="/header-request", headers=headers_dict) return http.handle_response(header_request_header_request_get, response) async def optional_parameters_request_optional_parameters_get() -> schemas.OptionalParametersResponse: """Optional Parameters Request""" - response = await http.get(url=f"/optional-parameters") + response = await http.get(url="/optional-parameters") return http.handle_response( optional_parameters_request_optional_parameters_get, response ) @@ -42,7 +42,7 @@ async def request_data_request_data_post( ) -> schemas.HTTPValidationError | schemas.RequestDataResponse: """Request Data""" - response = await http.post(url=f"/request-data", data=data.model_dump()) + response = await http.post(url="/request-data", data=data.model_dump()) return http.handle_response(request_data_request_data_post, response) @@ -51,7 +51,7 @@ async def request_data_request_data_put( ) -> schemas.HTTPValidationError | schemas.RequestDataResponse: """Request Data""" - response = await http.put(url=f"/request-data", data=data.model_dump()) + response = await http.put(url="/request-data", data=data.model_dump()) return http.handle_response(request_data_request_data_put, response) @@ -69,14 +69,14 @@ async def request_data_path_request_data( async def request_delete_request_delete_delete() -> schemas.DeleteResponse: """Request Delete""" - response = await http.delete(url=f"/request-delete") + response = await http.delete(url="/request-delete") return http.handle_response(request_delete_request_delete_delete, response) async def security_required_request_security_required_get() -> schemas.SecurityRequiredResponse: """Security Required Request""" - response = await http.get(url=f"/security-required") + response = await http.get(url="/security-required") return http.handle_response( security_required_request_security_required_get, response ) @@ -103,7 +103,7 @@ async def query_request_optional_query_get( async def simple_request_simple_request_get() -> schemas.SimpleResponse: """Simple Request""" - response = await http.get(url=f"/simple-request") + response = await http.get(url="/simple-request") return http.handle_response(simple_request_simple_request_get, response) diff --git a/tests/async_test_client/http.py b/tests/async_test_client/http.py index 16f3555..26a2ca5 100644 --- a/tests/async_test_client/http.py +++ b/tests/async_test_client/http.py @@ -117,7 +117,7 @@ def handle_response(func, response): auth_key = c.get_bearer_token() headers = c.additional_headers() -headers.update(Authorization=f"Bearer " + auth_key) +headers.update(Authorization=f"Bearer {auth_key}") client = httpx.AsyncClient(headers=headers) diff --git a/tests/test_client/client.py b/tests/test_client/client.py index 75e82c8..86b0a70 100644 --- a/tests/test_client/client.py +++ b/tests/test_client/client.py @@ -11,7 +11,7 @@ def complex_model_request_complex_model_request_get() -> schemas.ComplexModelResponse: """Complex Model Request""" - response = http.get(url=f"/complex-model-request") + response = http.get(url="/complex-model-request") return http.handle_response( complex_model_request_complex_model_request_get, response ) @@ -24,7 +24,7 @@ def header_request_header_request_get( headers_dict = ( headers and headers.model_dump(by_alias=True, exclude_unset=True) or None ) - response = http.get(url=f"/header-request", headers=headers_dict) + response = http.get(url="/header-request", headers=headers_dict) return http.handle_response(header_request_header_request_get, response) @@ -33,7 +33,7 @@ def optional_parameters_request_optional_parameters_get() -> ( ): """Optional Parameters Request""" - response = http.get(url=f"/optional-parameters") + response = http.get(url="/optional-parameters") return http.handle_response( optional_parameters_request_optional_parameters_get, response ) @@ -44,7 +44,7 @@ def request_data_request_data_post( ) -> schemas.HTTPValidationError | schemas.RequestDataResponse: """Request Data""" - response = http.post(url=f"/request-data", data=data.model_dump()) + response = http.post(url="/request-data", data=data.model_dump()) return http.handle_response(request_data_request_data_post, response) @@ -53,7 +53,7 @@ def request_data_request_data_put( ) -> schemas.HTTPValidationError | schemas.RequestDataResponse: """Request Data""" - response = http.put(url=f"/request-data", data=data.model_dump()) + response = http.put(url="/request-data", data=data.model_dump()) return http.handle_response(request_data_request_data_put, response) @@ -69,7 +69,7 @@ def request_data_path_request_data( def request_delete_request_delete_delete() -> schemas.DeleteResponse: """Request Delete""" - response = http.delete(url=f"/request-delete") + response = http.delete(url="/request-delete") return http.handle_response(request_delete_request_delete_delete, response) @@ -78,7 +78,7 @@ def security_required_request_security_required_get() -> ( ): """Security Required Request""" - response = http.get(url=f"/security-required") + response = http.get(url="/security-required") return http.handle_response( security_required_request_security_required_get, response ) @@ -105,7 +105,7 @@ def query_request_optional_query_get( def simple_request_simple_request_get() -> schemas.SimpleResponse: """Simple Request""" - response = http.get(url=f"/simple-request") + response = http.get(url="/simple-request") return http.handle_response(simple_request_simple_request_get, response) diff --git a/tests/test_client/http.py b/tests/test_client/http.py index 70f6eff..13a0e1f 100644 --- a/tests/test_client/http.py +++ b/tests/test_client/http.py @@ -117,7 +117,7 @@ def handle_response(func, response): auth_key = c.get_bearer_token() headers = c.additional_headers() -headers.update(Authorization=f"Bearer " + auth_key) +headers.update(Authorization=f"Bearer {auth_key}") client = httpx.Client(headers=headers)