From 805e22fe0e56003509a8407bc6956957a9a4d22f Mon Sep 17 00:00:00 2001 From: Dan Fellin Date: Mon, 14 Apr 2025 13:53:06 -0700 Subject: [PATCH] 0.3.3.dev9 - add support for optional arguments --- LICENSE | 2 +- polyapi/execute.py | 7 ++++++- polyapi/parser.py | 2 +- polyapi/utils.py | 3 +++ pyproject.toml | 3 ++- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 946285e..6b95772 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 PolyAPI Inc. +Copyright (c) 2025 PolyAPI Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/polyapi/execute.py b/polyapi/execute.py index abd1cee..26b5948 100644 --- a/polyapi/execute.py +++ b/polyapi/execute.py @@ -1,3 +1,4 @@ +from typing import Dict import requests from requests import Response from polyapi.config import get_api_key_and_url @@ -7,10 +8,14 @@ def execute(function_type, function_id, data) -> Response: """ execute a specific function id/type """ + data_without_None = data + if isinstance(data, Dict): + data_without_None = {k: v for k, v in data.items() if v is not None} + api_key, api_url = get_api_key_and_url() headers = {"Authorization": f"Bearer {api_key}"} url = f"{api_url}/functions/{function_type}/{function_id}/execute" - resp = requests.post(url, json=data, headers=headers) + resp = requests.post(url, json=data_without_None, headers=headers) # print(resp.status_code) # print(resp.headers["content-type"]) if resp.status_code < 200 or resp.status_code >= 300: diff --git a/polyapi/parser.py b/polyapi/parser.py index 24dd7bf..8ae3397 100644 --- a/polyapi/parser.py +++ b/polyapi/parser.py @@ -246,7 +246,7 @@ def _get_type_schema(json_type: str, python_type: str, schemas: List[Dict]): return schema -def _get_type(expr: ast.expr | None, schemas: List[Dict]) -> Tuple[str, Dict | None]: +def _get_type(expr: ast.expr | None, schemas: List[Dict]) -> Tuple[Any, Any, Any]: if not expr: return "any", "Any", None python_type = get_python_type_from_ast(expr) diff --git a/polyapi/utils.py b/polyapi/utils.py index a2fc42c..e09383d 100644 --- a/polyapi/utils.py +++ b/polyapi/utils.py @@ -206,6 +206,9 @@ def parse_arguments( arg_string += ( f" {a['name']}: {add_type_import_path(function_name, arg_type)}" ) + if not a["required"]: + arg_string += " = None" + description = a.get("description", "") description = description.replace("\n", " ") if description: diff --git a/pyproject.toml b/pyproject.toml index cb5e394..6240ded 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ requires = ["setuptools>=61.2", "wheel"] [project] name = "polyapi-python" -version = "0.3.3.dev8" +version = "0.3.3.dev9" description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers" authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }] dependencies = [ @@ -33,3 +33,4 @@ exclude = ["polyapi/poly*", "polyapi/vari*", "polyapi/.config.env", "polyapi/cac [tool.mypy] # for now redef errors happen sometimes, we will clean this up in the future! disable_error_code = "no-redef,name-defined" +implicit_optional = true