Skip to content

Commit

Permalink
Use black when generating a client
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Hallett committed Oct 4, 2023
1 parent 259388b commit d030127
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
9 changes: 9 additions & 0 deletions clientele/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand All @@ -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()
2 changes: 1 addition & 1 deletion clientele/templates/bearer_client.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
4 changes: 4 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions tests/async_test_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
)
Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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
)
Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion tests/async_test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
16 changes: 8 additions & 8 deletions tests/test_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)


Expand All @@ -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
)
Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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)


Expand All @@ -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
)
Expand All @@ -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)


Expand Down
2 changes: 1 addition & 1 deletion tests/test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit d030127

Please sign in to comment.