From 6d34054c58ce91e69109338544f69cee6337fcae Mon Sep 17 00:00:00 2001 From: Dan Fellin Date: Mon, 3 Jun 2024 08:03:21 -0700 Subject: [PATCH 1/3] onward --- polyapi/generate.py | 6 +++--- polyapi/utils.py | 12 ++++++++---- polyapi/webhook.py | 4 ++-- pyproject.toml | 2 +- tests/test_api.py | 9 +++++---- tests/test_auth.py | 1 - tests/test_server.py | 4 +++- 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/polyapi/generate.py b/polyapi/generate.py index a4f429e..93584b8 100644 --- a/polyapi/generate.py +++ b/polyapi/generate.py @@ -11,7 +11,7 @@ from .typedefs import PropertySpecification, SpecificationDto, VariableSpecDto from .api import render_api_function from .server import render_server_function -from .utils import add_import_to_init, get_auth_headers, init_the_init +from .utils import add_import_to_init, get_auth_headers, init_the_init, upper_first from .variables import generate_variables from .config import get_api_key_and_url, initialize_config @@ -232,10 +232,10 @@ def add_function_file( # add function to init init_path = os.path.join(full_path, "__init__.py") with open(init_path, "a") as f: - f.write(f"\n\nfrom . import _{function_name}\n\n{func_str}") + f.write(f"\n\nfrom . import {upper_first(function_name)}\n\n{func_str}") # add type_defs to underscore file - file_path = os.path.join(full_path, f"_{function_name}.py") + file_path = os.path.join(full_path, f"{upper_first(function_name)}.py") with open(file_path, "w") as f: f.write(func_type_defs) diff --git a/polyapi/utils.py b/polyapi/utils.py index d7b105b..7e625ed 100644 --- a/polyapi/utils.py +++ b/polyapi/utils.py @@ -75,11 +75,11 @@ def add_type_import_path(function_name: str, arg: str) -> str: else: if '"' in sub: sub = sub.replace('"', "") - return f'List["_{function_name}.{camelCase(sub)}"]' + return f'List["{upper_first(function_name)}.{camelCase(sub)}"]' else: - return f'List[_{function_name}.{camelCase(sub)}]' + return f'List[{upper_first(function_name)}.{camelCase(sub)}]' - return f'_{function_name}.{camelCase(arg)}' + return f'{upper_first(function_name)}.{camelCase(arg)}' def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]: @@ -183,4 +183,8 @@ def poly_full_path(context, name) -> str: path = context + "." + name else: path = name - return f"poly.{path}" \ No newline at end of file + return f"poly.{path}" + + +def upper_first(s: str) -> str: + return s[0].upper() + s[1:] \ No newline at end of file diff --git a/polyapi/webhook.py b/polyapi/webhook.py index 1524655..f316e93 100644 --- a/polyapi/webhook.py +++ b/polyapi/webhook.py @@ -6,7 +6,7 @@ from polyapi.config import get_api_key_and_url from polyapi.typedefs import PropertySpecification -from polyapi.utils import parse_arguments, poly_full_path +from polyapi.utils import parse_arguments, poly_full_path, upper_first # all active webhook handlers, used by unregister_all to cleanup active_handlers: List[Dict[str, Any]] = [] @@ -124,7 +124,7 @@ def render_webhook_handle( if "WebhookEventType" in function_args: # let's add the function name import! - function_args = function_args.replace("WebhookEventType", f"_{function_name}.WebhookEventType") + function_args = function_args.replace("WebhookEventType", f"{upper_first(function_name)}.WebhookEventType") func_str = WEBHOOK_TEMPLATE.format( description=function_description, diff --git a/pyproject.toml b/pyproject.toml index 376e0e1..5139838 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"] [project] name = "polyapi-python" -version = "0.2.5" +version = "0.2.6.dev0" description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers" authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }] dependencies = [ diff --git a/tests/test_api.py b/tests/test_api.py index eb05c7b..0dcabc7 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,6 +1,7 @@ import unittest from polyapi.api import render_api_function +from polyapi.utils import upper_first ACCUWEATHER = { "id": "f7588018-2364-4586-b60d", @@ -235,7 +236,7 @@ def test_render_function_accuweather(self): ) self.assertIn(ACCUWEATHER["id"], func_str) self.assertIn("locationId: int,", func_str) - self.assertIn(f"-> _{name}.{name}Response", func_str) + self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) def test_render_function_zillow(self): name = ZILLOW["name"] @@ -249,7 +250,7 @@ def test_render_function_zillow(self): ) self.assertIn(ZILLOW["id"], func_str) self.assertIn("locationId: int,", func_str) - self.assertIn(f"-> _{name}.{name}Response", func_str) + self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) def test_render_function_twilio_api(self): name = TWILIO["name"] @@ -264,7 +265,7 @@ def test_render_function_twilio_api(self): self.assertIn(TWILIO["id"], func_str) self.assertIn("conversationSID: str", func_str) self.assertIn("authToken: str", func_str) - self.assertIn(f"-> _{name}.{name}Response", func_str) + self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) def test_render_function_twilio_get_details(self): # same test but try it as a serverFunction rather than an apiFunction @@ -278,6 +279,6 @@ def test_render_function_twilio_get_details(self): TWILIO_GET_DETAILS["function"]["returnType"], ) self.assertIn(TWILIO_GET_DETAILS["id"], func_str) - self.assertIn(f"-> _{name}.{name}Response", func_str) + self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) self.assertIn("class SubresourceUris", func_type_defs) # self.assertIn('Required["SubresourceUris"]', func_type_defs) diff --git a/tests/test_auth.py b/tests/test_auth.py index ed626d8..6d399f7 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -136,4 +136,3 @@ def test_render_get_token(self): self.assertIn(GET_TOKEN["id"], func_str) # self.assertIn("conversationSID: str", func_str) # self.assertIn("authToken: str", func_str) - # self.assertIn(f"-> _{name}.ResponseType", func_str) diff --git a/tests/test_server.py b/tests/test_server.py index d08bea6..e7dadc0 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,5 +1,7 @@ import unittest +from polyapi.utils import upper_first + from .test_api import TWILIO from polyapi.server import render_server_function @@ -45,7 +47,7 @@ def test_render_function_twilio_server(self): self.assertIn(TWILIO["id"], func_str) self.assertIn("conversationSID: str", func_str) self.assertIn("authToken: str", func_str) - self.assertIn(f"-> _{name}.ResponseType", func_str) + self.assertIn(f"-> {upper_first(name)}.ResponseType", func_str) def test_render_function_get_products_count(self): return_type = GET_PRODUCTS_COUNT["function"]["returnType"] From ad5be66545f2472ad29fb1983172ed1881b409ea Mon Sep 17 00:00:00 2001 From: Dan Fellin Date: Mon, 3 Jun 2024 08:16:30 -0700 Subject: [PATCH 2/3] lets make it `to_func_namespace` --- polyapi/generate.py | 6 +++--- polyapi/utils.py | 20 +++++++++++++++----- polyapi/webhook.py | 4 ++-- tests/test_api.py | 10 +++++----- tests/test_server.py | 4 ++-- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/polyapi/generate.py b/polyapi/generate.py index 93584b8..d004c6f 100644 --- a/polyapi/generate.py +++ b/polyapi/generate.py @@ -11,7 +11,7 @@ from .typedefs import PropertySpecification, SpecificationDto, VariableSpecDto from .api import render_api_function from .server import render_server_function -from .utils import add_import_to_init, get_auth_headers, init_the_init, upper_first +from .utils import add_import_to_init, get_auth_headers, init_the_init, to_func_namespace from .variables import generate_variables from .config import get_api_key_and_url, initialize_config @@ -232,10 +232,10 @@ def add_function_file( # add function to init init_path = os.path.join(full_path, "__init__.py") with open(init_path, "a") as f: - f.write(f"\n\nfrom . import {upper_first(function_name)}\n\n{func_str}") + f.write(f"\n\nfrom . import {to_func_namespace(function_name)}\n\n{func_str}") # add type_defs to underscore file - file_path = os.path.join(full_path, f"{upper_first(function_name)}.py") + file_path = os.path.join(full_path, f"{to_func_namespace(function_name)}.py") with open(file_path, "w") as f: f.write(func_type_defs) diff --git a/polyapi/utils.py b/polyapi/utils.py index 7e625ed..3349ff6 100644 --- a/polyapi/utils.py +++ b/polyapi/utils.py @@ -75,11 +75,11 @@ def add_type_import_path(function_name: str, arg: str) -> str: else: if '"' in sub: sub = sub.replace('"', "") - return f'List["{upper_first(function_name)}.{camelCase(sub)}"]' + return f'List["{to_func_namespace(function_name)}.{camelCase(sub)}"]' else: - return f'List[{upper_first(function_name)}.{camelCase(sub)}]' + return f'List[{to_func_namespace(function_name)}.{camelCase(sub)}]' - return f'{upper_first(function_name)}.{camelCase(arg)}' + return f'{to_func_namespace(function_name)}.{camelCase(arg)}' def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]: @@ -186,5 +186,15 @@ def poly_full_path(context, name) -> str: return f"poly.{path}" -def upper_first(s: str) -> str: - return s[0].upper() + s[1:] \ No newline at end of file +RESERVED_TYPES = {"List", "Dict", "Any", "Optional", "Callable"} + + +def to_func_namespace(s: str) -> str: + """ convert a function name to some function namespace + by default it is + """ + rv = s[0].upper() + s[1:] + if rv in RESERVED_TYPES: + return "_" + rv + else: + return rv \ No newline at end of file diff --git a/polyapi/webhook.py b/polyapi/webhook.py index f316e93..855c300 100644 --- a/polyapi/webhook.py +++ b/polyapi/webhook.py @@ -6,7 +6,7 @@ from polyapi.config import get_api_key_and_url from polyapi.typedefs import PropertySpecification -from polyapi.utils import parse_arguments, poly_full_path, upper_first +from polyapi.utils import parse_arguments, poly_full_path, to_func_namespace # all active webhook handlers, used by unregister_all to cleanup active_handlers: List[Dict[str, Any]] = [] @@ -124,7 +124,7 @@ def render_webhook_handle( if "WebhookEventType" in function_args: # let's add the function name import! - function_args = function_args.replace("WebhookEventType", f"{upper_first(function_name)}.WebhookEventType") + function_args = function_args.replace("WebhookEventType", f"{to_func_namespace(function_name)}.WebhookEventType") func_str = WEBHOOK_TEMPLATE.format( description=function_description, diff --git a/tests/test_api.py b/tests/test_api.py index 0dcabc7..ceba99c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,7 +1,7 @@ import unittest from polyapi.api import render_api_function -from polyapi.utils import upper_first +from polyapi.utils import to_func_namespace ACCUWEATHER = { "id": "f7588018-2364-4586-b60d", @@ -236,7 +236,7 @@ def test_render_function_accuweather(self): ) self.assertIn(ACCUWEATHER["id"], func_str) self.assertIn("locationId: int,", func_str) - self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) + self.assertIn(f"-> {to_func_namespace(name)}.{name}Response", func_str) def test_render_function_zillow(self): name = ZILLOW["name"] @@ -250,7 +250,7 @@ def test_render_function_zillow(self): ) self.assertIn(ZILLOW["id"], func_str) self.assertIn("locationId: int,", func_str) - self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) + self.assertIn(f"-> {to_func_namespace(name)}.{name}Response", func_str) def test_render_function_twilio_api(self): name = TWILIO["name"] @@ -265,7 +265,7 @@ def test_render_function_twilio_api(self): self.assertIn(TWILIO["id"], func_str) self.assertIn("conversationSID: str", func_str) self.assertIn("authToken: str", func_str) - self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) + self.assertIn(f"-> {to_func_namespace(name)}.{name}Response", func_str) def test_render_function_twilio_get_details(self): # same test but try it as a serverFunction rather than an apiFunction @@ -279,6 +279,6 @@ def test_render_function_twilio_get_details(self): TWILIO_GET_DETAILS["function"]["returnType"], ) self.assertIn(TWILIO_GET_DETAILS["id"], func_str) - self.assertIn(f"-> {upper_first(name)}.{name}Response", func_str) + self.assertIn(f"-> {to_func_namespace(name)}.{name}Response", func_str) self.assertIn("class SubresourceUris", func_type_defs) # self.assertIn('Required["SubresourceUris"]', func_type_defs) diff --git a/tests/test_server.py b/tests/test_server.py index e7dadc0..c767d2d 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,6 +1,6 @@ import unittest -from polyapi.utils import upper_first +from polyapi.utils import to_func_namespace from .test_api import TWILIO from polyapi.server import render_server_function @@ -47,7 +47,7 @@ def test_render_function_twilio_server(self): self.assertIn(TWILIO["id"], func_str) self.assertIn("conversationSID: str", func_str) self.assertIn("authToken: str", func_str) - self.assertIn(f"-> {upper_first(name)}.ResponseType", func_str) + self.assertIn(f"-> {to_func_namespace(name)}.ResponseType", func_str) def test_render_function_get_products_count(self): return_type = GET_PRODUCTS_COUNT["function"]["returnType"] From 1b59fb0920435a0e820b53496d9736285568414d Mon Sep 17 00:00:00 2001 From: Dan Fellin Date: Thu, 13 Jun 2024 10:34:42 -0700 Subject: [PATCH 3/3] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5139838..a6017ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"] [project] name = "polyapi-python" -version = "0.2.6.dev0" +version = "0.2.6.dev1" description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers" authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }] dependencies = [