From 80b1be1281f739b1793a482802e81a15e4eb7247 Mon Sep 17 00:00:00 2001 From: Paul Hallett Date: Wed, 26 Jul 2023 10:23:12 +1200 Subject: [PATCH] 0.3.1 --- CHANGELOG.md | 6 ++++++ docs/CHANGELOG.md | 6 ++++++ pyproject.toml | 2 +- src/generators/clients.py | 2 +- src/generators/http.py | 3 ++- src/generators/schemas.py | 7 +++---- src/settings.py | 2 +- src/utils.py | 17 ++++++++++++++--- 8 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 284028a..6531e06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ - Use [pydantic settings](https://docs.pydantic.dev/latest/usage/pydantic_settings/) for settings management in the `constants.py` file. This is a **breaking change** and you will need to delete your `constants.py` file. +## 0.3.1 + +- Fixes a bug when generating HTTP Authentication schema. +- Fixes a bug when generating input classes for post functions, when the input schema doesn't exist yet. +- Generates pythonic function names in clients now, always (like `lower_case_snake_case`). + ## 0.3.0 - Now generates a `MANIFEST` file with information about the build versions diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 90b7ed4..bdededc 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,11 @@ # Change log +## 0.3.1 + +- Fixes a bug when generating HTTP Authentication schema. +- Fixes a bug when generating input classes for post functions, when the input schema doesn't exist yet. +- Generates pythonic function names in clients now, always (like `lower_case_snake_case`). + ## 0.3.0 - Now generates a `MANIFEST` file with information about the build versions diff --git a/pyproject.toml b/pyproject.toml index 448186d..9758da8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "clientele" -version = "0.3.0" +version = "0.3.1" description = "Typed API Clients from OpenAPI schemas" authors = ["Paul Hallett "] license = "MIT" diff --git a/src/generators/clients.py b/src/generators/clients.py index e1614c1..5cc57b7 100644 --- a/src/generators/clients.py +++ b/src/generators/clients.py @@ -161,7 +161,7 @@ def generate_post_content(self, operation: Dict, path: str) -> None: if not operation.get("requestBody"): input_class_name = "None" else: - input_class_name = self.generate_input_types(operation.get("requestBody")) + input_class_name = self.generate_input_types(operation.get("requestBody", {})) function_arguments = self.generate_function_args( operation.get("parameters", []) ) diff --git a/src/generators/http.py b/src/generators/http.py index 5e4ddf1..bedd8b0 100644 --- a/src/generators/http.py +++ b/src/generators/http.py @@ -9,7 +9,8 @@ console = Console() BEARER_CLIENT = """ -headers = dict(Authorization=f'Bearer {c.get_bearer_token()}') +auth_key = c.get_bearer_token() +headers = dict(Authorization=f'Bearer ' + auth_key) client = httpx.{client_type}(headers=headers) """ diff --git a/src/generators/schemas.py b/src/generators/schemas.py index a2e2f42..bbf8d88 100644 --- a/src/generators/schemas.py +++ b/src/generators/schemas.py @@ -53,8 +53,7 @@ def generate_class_properties(self, properties: Dict) -> str: return content def generate_input_class(self, schema: Dict) -> None: - for _, schema_details in schema.items(): - content = schema_details["content"] + if content := schema.get("content"): for encoding, input_schema in content.items(): class_name = "" if ref := input_schema["schema"].get("$ref", False): @@ -69,12 +68,12 @@ def generate_input_class(self, schema: Dict) -> None: properties = self.generate_class_properties( input_schema["schema"].get("properties", {}) ) - content = f""" + out_content = f""" class {class_name}(BaseModel): {properties if properties else " pass"} """ write_to_schemas( - content, + out_content, output_dir=self.output_dir, ) diff --git a/src/settings.py b/src/settings.py index 4dbacdc..dceece2 100644 --- a/src/settings.py +++ b/src/settings.py @@ -2,4 +2,4 @@ TEMPLATE_ROOT = dirname(dirname(abspath(__file__))) + "/src/template/" CONSTANTS_ROOT = dirname(dirname(abspath(__file__))) + "/src/" -VERSION = "0.3.0" +VERSION = "0.3.1" diff --git a/src/utils.py b/src/utils.py index 414b2a3..c37d773 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,3 +1,4 @@ +import re from typing import Dict @@ -41,11 +42,21 @@ def clean_prop(input_str: str) -> str: return input_str +def _split_upper(s): + return "_".join(re.findall(".[^A-Z]*", s)) + + +def _snake_case(s): + for badchar in ["/", "-", "."]: + s = s.replace(badchar, "_") + s = _split_upper(s) + return s.lower() + + def get_func_name(operation: Dict, path: str) -> str: if operation.get("operationId"): - return class_name_titled(operation["operationId"].split("__")[0]) - # Probably 3.0.1 - return path.replace("/", "_").replace("-", "_")[1:] + return _snake_case(operation["operationId"].split("__")[0]) + return _snake_case(path) def get_type(t):