Skip to content

Commit fbdd0ae

Browse files
committed
Better uniformity with python arguments and typescript arguments, adding the update_one and delete_one tabi methods as well
1 parent 5e605da commit fbdd0ae

File tree

7 files changed

+134
-30
lines changed

7 files changed

+134
-30
lines changed

polyapi/cli.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def setup(args):
5858
initialize_config(force=True)
5959
# setup command should have default cache values
6060
from .config import cache_generate_args
61-
cache_generate_args(contexts=None, names=None, function_ids=None, no_types=False)
61+
cache_generate_args(contexts=None, names=None, ids=None, no_types=False)
6262
generate()
6363

6464
setup_parser.set_defaults(command=setup)
@@ -70,7 +70,7 @@ def setup(args):
7070
generate_parser.add_argument("--no-types", action="store_true", help="Generate SDK without type definitions")
7171
generate_parser.add_argument("--contexts", type=str, required=False, help="Contexts to generate")
7272
generate_parser.add_argument("--names", type=str, required=False, help="Resource names to generate (comma-separated)")
73-
generate_parser.add_argument("--function-ids", type=str, required=False, help="Function IDs to generate (comma-separated)")
73+
generate_parser.add_argument("--ids", "--function-ids", type=str, required=False, help="Resource IDs to generate (comma-separated)")
7474

7575
def generate_command(args):
7676
from .config import cache_generate_args
@@ -79,24 +79,24 @@ def generate_command(args):
7979

8080
contexts = args.contexts.split(",") if args.contexts else None
8181
names = args.names.split(",") if args.names else None
82-
function_ids = args.function_ids.split(",") if args.function_ids else None
82+
ids = args.ids.split(",") if args.ids else None
8383
no_types = args.no_types
8484

8585
# overwrite all cached values with the values passed in from the command line
8686
final_contexts = contexts
8787
final_names = names
88-
final_function_ids = function_ids
88+
final_ids = ids
8989
final_no_types = no_types
9090

9191
# cache the values used for this explicit generate command
9292
cache_generate_args(
9393
contexts=final_contexts,
9494
names=final_names,
95-
function_ids=final_function_ids,
95+
ids=ids,
9696
no_types=final_no_types
9797
)
9898

99-
generate(contexts=final_contexts, names=final_names, function_ids=final_function_ids, no_types=final_no_types)
99+
generate(contexts=final_contexts, names=final_names, ids=ids, no_types=final_no_types)
100100

101101
generate_parser.set_defaults(command=generate_command)
102102

polyapi/config.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
MTLS_CA_PATH = None
1515
LAST_GENERATE_CONTEXTS = None
1616
LAST_GENERATE_NAMES = None
17-
LAST_GENERATE_FUNCTION_IDS = None
17+
LAST_GENERATE_IDS = None
1818
LAST_GENERATE_NO_TYPES = None
1919

2020

@@ -61,13 +61,13 @@ def get_api_key_and_url() -> Tuple[str | None, str | None]:
6161
MTLS_CA_PATH = config.get("polyapi", "mtls_ca_path", fallback=None)
6262

6363
# Read and cache generate command arguments
64-
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_FUNCTION_IDS, LAST_GENERATE_NO_TYPES
64+
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_IDS, LAST_GENERATE_NO_TYPES
6565
contexts_str = config.get("polyapi", "last_generate_contexts_used", fallback=None)
6666
LAST_GENERATE_CONTEXTS = contexts_str.split(",") if contexts_str else None
6767
names_str = config.get("polyapi", "last_generate_names_used", fallback=None)
6868
LAST_GENERATE_NAMES = names_str.split(",") if names_str else None
69-
function_ids_str = config.get("polyapi", "last_generate_function_ids_used", fallback=None)
70-
LAST_GENERATE_FUNCTION_IDS = function_ids_str.split(",") if function_ids_str else None
69+
ids_str = config.get("polyapi", "last_generate_ids_used", fallback=None)
70+
LAST_GENERATE_IDS = ids_str.split(",") if ids_str else None
7171
LAST_GENERATE_NO_TYPES = config.get("polyapi", "last_generate_no_types_used", fallback="false").lower() == "true"
7272

7373
return key, url
@@ -152,14 +152,14 @@ def get_direct_execute_config() -> bool:
152152

153153
def get_cached_generate_args() -> Tuple[list | None, list | None, list | None, bool]:
154154
"""Return cached generate command arguments"""
155-
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_FUNCTION_IDS, LAST_GENERATE_NO_TYPES
156-
if LAST_GENERATE_CONTEXTS is None and LAST_GENERATE_NAMES is None and LAST_GENERATE_FUNCTION_IDS is None and LAST_GENERATE_NO_TYPES is None:
155+
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_IDS, LAST_GENERATE_NO_TYPES
156+
if LAST_GENERATE_CONTEXTS is None and LAST_GENERATE_NAMES is None and LAST_GENERATE_IDS is None and LAST_GENERATE_NO_TYPES is None:
157157
# Force a config read if values aren't cached
158158
get_api_key_and_url()
159-
return LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_FUNCTION_IDS, bool(LAST_GENERATE_NO_TYPES)
159+
return LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_IDS, bool(LAST_GENERATE_NO_TYPES)
160160

161161

162-
def cache_generate_args(contexts: list | None = None, names: list | None = None, function_ids: list | None = None, no_types: bool = False):
162+
def cache_generate_args(contexts: list | None = None, names: list | None = None, ids: list | None = None, no_types: bool = False):
163163
"""Cache generate command arguments to config file"""
164164
from typing import List
165165

@@ -176,10 +176,10 @@ def cache_generate_args(contexts: list | None = None, names: list | None = None,
176176
config["polyapi"] = {}
177177

178178
# Update cached values
179-
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_FUNCTION_IDS, LAST_GENERATE_NO_TYPES
179+
global LAST_GENERATE_CONTEXTS, LAST_GENERATE_NAMES, LAST_GENERATE_IDS, LAST_GENERATE_NO_TYPES
180180
LAST_GENERATE_CONTEXTS = contexts
181181
LAST_GENERATE_NAMES = names
182-
LAST_GENERATE_FUNCTION_IDS = function_ids
182+
LAST_GENERATE_IDS = ids
183183
LAST_GENERATE_NO_TYPES = no_types
184184

185185
# Write values to config
@@ -193,10 +193,10 @@ def cache_generate_args(contexts: list | None = None, names: list | None = None,
193193
elif config.has_option("polyapi", "last_generate_names_used"):
194194
config.remove_option("polyapi", "last_generate_names_used")
195195

196-
if function_ids is not None:
197-
config.set("polyapi", "last_generate_function_ids_used", ",".join(function_ids))
198-
elif config.has_option("polyapi", "last_generate_function_ids_used"):
199-
config.remove_option("polyapi", "last_generate_function_ids_used")
196+
if ids is not None:
197+
config.set("polyapi", "last_generate_ids_used", ",".join(ids))
198+
elif config.has_option("polyapi", "last_generate_ids_used"):
199+
config.remove_option("polyapi", "last_generate_ids_used")
200200

201201
config.set("polyapi", "last_generate_no_types_used", str(no_types).lower())
202202

polyapi/generate.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
path:'''
4343

4444

45-
def get_specs(contexts: Optional[List[str]] = None, names: Optional[List[str]] = None, function_ids: Optional[List[str]] = None, no_types: bool = False) -> List:
45+
def get_specs(contexts: Optional[List[str]] = None, names: Optional[List[str]] = None, ids: Optional[List[str]] = None, no_types: bool = False) -> List:
4646
api_key, api_url = get_api_key_and_url()
4747
assert api_key
4848
headers = get_auth_headers(api_key)
@@ -55,8 +55,8 @@ def get_specs(contexts: Optional[List[str]] = None, names: Optional[List[str]] =
5555
if names:
5656
params["names"] = names
5757

58-
if function_ids:
59-
params["functionIds"] = function_ids
58+
if ids:
59+
params["ids"] = ids
6060

6161
# Add apiFunctionDirectExecute parameter if direct execute is enabled
6262
if get_direct_execute_config():
@@ -297,12 +297,12 @@ def generate_from_cache() -> None:
297297
"""
298298
Generate using cached values after non-explicit call.
299299
"""
300-
cached_contexts, cached_names, cached_function_ids, cached_no_types = get_cached_generate_args()
300+
cached_contexts, cached_names, cached_ids, cached_no_types = get_cached_generate_args()
301301

302302
generate(
303303
contexts=cached_contexts,
304304
names=cached_names,
305-
function_ids=cached_function_ids,
305+
ids=cached_ids,
306306
no_types=cached_no_types
307307
)
308308

@@ -338,12 +338,12 @@ def normalize_args_schema(
338338
return spec
339339

340340

341-
def generate(contexts: Optional[List[str]] = None, names: Optional[List[str]] = None, function_ids: Optional[List[str]] = None, no_types: bool = False) -> None:
341+
def generate(contexts: Optional[List[str]] = None, names: Optional[List[str]] = None, ids: Optional[List[str]] = None, no_types: bool = False) -> None:
342342
generate_msg = f"Generating Poly Python SDK for contexts ${contexts}..." if contexts else "Generating Poly Python SDK..."
343343
print(generate_msg, end="", flush=True)
344344
remove_old_library()
345345

346-
specs = get_specs(contexts=contexts, names=names, function_ids=function_ids, no_types=no_types)
346+
specs = get_specs(contexts=contexts, names=names, ids=ids, no_types=no_types)
347347
cache_specs(specs)
348348

349349
limit_ids: List[str] = [] # useful for narrowing down generation to a single function to debug

polyapi/poly_tables.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def first_result(rsp):
5555
return rsp['results'][0] if rsp['results'] else None
5656
return rsp
5757

58+
def delete_one_response(rsp):
59+
if isinstance(rsp, dict) and isinstance(rsp.get('deleted'), int):
60+
return { 'deleted': bool(rsp.get('deleted')) }
61+
return { 'deleted': false }
5862

5963
_key_transform_map = {
6064
"not_": "not",
@@ -301,6 +305,30 @@ def update_many(*args, **kwargs) -> {table_name}QueryResults:
301305
query = kwargs
302306
return execute_query({table_name}.table_id, "update", transform_query(query))
303307
308+
@overload
309+
@staticmethod
310+
def update_one(id: str, query: {table_name}UpdateManyQuery) -> {table_name}Row: ...
311+
@overload
312+
@staticmethod
313+
def update_one(*, id: str, where: Optional[{table_name}WhereFilter], data: {table_name}Subset) -> {table_name}Row: ...
314+
315+
@staticmethod
316+
def update_one(*args, **kwargs) -> {table_name}Row:
317+
if args:
318+
if len(args) != 2 or or not isinstance(args[0], str) not isinstance(args[1], dict):
319+
raise TypeError("Expected id and query as arguments or as kwargs")
320+
query = args[1]
321+
if not isinstance(query["where"], dict):
322+
query["where"] = {{}}
323+
query["where"]["id"] = args[0]
324+
else:
325+
query = kwargs
326+
if not isinstance(query["where"], dict):
327+
query["where"] = {{}}
328+
query["where"]["id"] = kwargs["id"]
329+
query.pop("id", None)
330+
return first_result(execute_query({table_name}.table_id, "update", transform_query(query)))
331+
304332
@overload
305333
@staticmethod
306334
def delete_many(query: {table_name}DeleteQuery) -> PolyDeleteResults: ...
@@ -316,7 +344,31 @@ def delete_many(*args, **kwargs) -> PolyDeleteResults:
316344
query = args[0]
317345
else:
318346
query = kwargs
319-
return execute_query({table_name}.table_id, "delete", query)
347+
return execute_query({table_name}.table_id, "delete", transform_query(query))
348+
349+
@overload
350+
@staticmethod
351+
def delete_one(query: {table_name}DeleteQuery) -> PolyDeleteResult: ...
352+
@overload
353+
@staticmethod
354+
def delete_one(*, where: Optional[{table_name}WhereFilter]) -> PolyDeleteResult: ...
355+
356+
@staticmethod
357+
def delete_one(*args, **kwargs) -> PolyDeleteResult:
358+
if args:
359+
if len(args) != 2 or or not isinstance(args[0], str) not isinstance(args[1], dict):
360+
raise TypeError("Expected id and query as arguments or as kwargs")
361+
query = args[1]
362+
if not isinstance(query["where"], dict):
363+
query["where"] = {{}}
364+
query["where"]["id"] = args[0]
365+
else:
366+
query = kwargs
367+
if not isinstance(query["where"], dict):
368+
query["where"] = {{}}
369+
query["where"]["id"] = kwargs["id"]
370+
query.pop("id", None)
371+
return delete_one_response(execute_query({table_name}.table_id, "delete", transform_query(query)))
320372
'''
321373

322374

polyapi/typedefs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class PolyDeleteResults(TypedDict):
118118
deleted: int
119119

120120

121+
class PolyDeleteResult(TypedDict):
122+
deleted: bool
123+
124+
121125

122126
QueryMode = Literal["default", "insensitive"]
123127

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"]
33

44
[project]
55
name = "polyapi-python"
6-
version = "0.3.11.dev3"
6+
version = "0.3.11.dev4"
77
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
88
authors = [{ name = "Dan Fellin", email = "[email protected]" }]
99
dependencies = [

tests/test_tabi.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,30 @@ def update_many(*args, **kwargs) -> MyTableQueryResults:
285285
query = kwargs
286286
return execute_query(MyTable.table_id, "update", transform_query(query))
287287
288+
@overload
289+
@staticmethod
290+
def update_one(id: str, query: MyTableUpdateManyQuery) -> MyTableRow: ...
291+
@overload
292+
@staticmethod
293+
def update_one(*, id: str, where: Optional[MyTableWhereFilter], data: MyTableSubset) -> MyTableRow: ...
294+
295+
@staticmethod
296+
def update_one(*args, **kwargs) -> MyTableRow:
297+
if args:
298+
if len(args) != 2 or or not isinstance(args[0], str) not isinstance(args[1], dict):
299+
raise TypeError("Expected id and query as arguments or as kwargs")
300+
query = args[1]
301+
if not isinstance(query["where"], dict):
302+
query["where"] = {}
303+
query["where"]["id"] = args[0]
304+
else:
305+
query = kwargs
306+
if not isinstance(query["where"], dict):
307+
query["where"] = {}
308+
query["where"]["id"] = kwargs["id"]
309+
query.pop("id", None)
310+
return first_result(execute_query(MyTable.table_id, "update", transform_query(query)))
311+
288312
@overload
289313
@staticmethod
290314
def delete_many(query: MyTableDeleteQuery) -> PolyDeleteResults: ...
@@ -300,7 +324,31 @@ def delete_many(*args, **kwargs) -> PolyDeleteResults:
300324
query = args[0]
301325
else:
302326
query = kwargs
303-
return execute_query(MyTable.table_id, "delete", query)
327+
return execute_query(MyTable.table_id, "delete", transform_query(query))
328+
329+
@overload
330+
@staticmethod
331+
def delete_one(query: MyTableDeleteQuery) -> PolyDeleteResult: ...
332+
@overload
333+
@staticmethod
334+
def delete_one(*, where: Optional[MyTableWhereFilter]) -> PolyDeleteResult: ...
335+
336+
@staticmethod
337+
def delete_one(*args, **kwargs) -> PolyDeleteResult:
338+
if args:
339+
if len(args) != 2 or or not isinstance(args[0], str) not isinstance(args[1], dict):
340+
raise TypeError("Expected id and query as arguments or as kwargs")
341+
query = args[1]
342+
if not isinstance(query["where"], dict):
343+
query["where"] = {}
344+
query["where"]["id"] = args[0]
345+
else:
346+
query = kwargs
347+
if not isinstance(query["where"], dict):
348+
query["where"] = {}
349+
query["where"]["id"] = kwargs["id"]
350+
query.pop("id", None)
351+
return delete_one_response(execute_query(MyTable.table_id, "delete", transform_query(query)))
304352
'''
305353

306354
TABLE_SPEC_COMPLEX = {

0 commit comments

Comments
 (0)