diff --git a/polyapi/generate.py b/polyapi/generate.py index a4f429e..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 +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 _{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"_{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 d7b105b..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["_{function_name}.{camelCase(sub)}"]' + return f'List["{to_func_namespace(function_name)}.{camelCase(sub)}"]' else: - return f'List[_{function_name}.{camelCase(sub)}]' + return f'List[{to_func_namespace(function_name)}.{camelCase(sub)}]' - return f'_{function_name}.{camelCase(arg)}' + return f'{to_func_namespace(function_name)}.{camelCase(arg)}' def get_type_and_def(type_spec: PropertyType) -> Tuple[str, str]: @@ -183,4 +183,18 @@ 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}" + + +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 1524655..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 +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"_{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/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 = [ diff --git a/tests/test_api.py b/tests/test_api.py index eb05c7b..ceba99c 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 to_func_namespace 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"-> {to_func_namespace(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"-> {to_func_namespace(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"-> {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 @@ -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"-> {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_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..c767d2d 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,5 +1,7 @@ import unittest +from polyapi.utils import to_func_namespace + 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"-> {to_func_namespace(name)}.ResponseType", func_str) def test_render_function_get_products_count(self): return_type = GET_PRODUCTS_COUNT["function"]["returnType"]