Skip to content

Commit d20794b

Browse files
authored
4010 generate contexts (#43)
1 parent 9c1a2a1 commit d20794b

File tree

7 files changed

+34
-26
lines changed

7 files changed

+34
-26
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
.env
2-
.env*
3-
.venv/
4-
.venv/*
1+
*env
2+
*venv
53
.DS_Store
64

75
# Pip

polyapi/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ def setup(args):
4545
# Generate command
4646
generate_parser = subparsers.add_parser("generate", help="Generates Poly library")
4747
generate_parser.add_argument("--no-types", action="store_true", help="Generate SDK without type definitions")
48+
generate_parser.add_argument("--contexts", type=str, required=False, help="Contexts to generate")
4849

4950
def generate_command(args):
5051
initialize_config()
51-
generate(no_types=args.no_types)
52+
contexts = args.contexts.split(",") if args.contexts else None
53+
generate(contexts=contexts, no_types=args.no_types)
5254

5355
generate_parser.set_defaults(command=generate_command)
5456

@@ -69,6 +71,7 @@ def generate_command(args):
6971
fn_add_parser.add_argument("--logs", choices=["enabled", "disabled"], default=None, help="Enable or disable logs for the function.")
7072
fn_add_parser.add_argument("--execution-api-key", required=False, default="", help="API key for execution (for server functions only).")
7173
fn_add_parser.add_argument("--disable-ai", "--skip-generate", action="store_true", help="Pass --disable-ai skip AI generation of missing descriptions")
74+
fn_add_parser.add_argument("--generate-contexts", type=str, help="Server function only – only include certain contexts to speed up function execution")
7275

7376
def add_function(args):
7477
initialize_config()
@@ -80,6 +83,8 @@ def add_function(args):
8083
err = "You must specify `--server` or `--client`."
8184
elif logs_enabled and not args.server:
8285
err = "Option `logs` is only for server functions (--server)."
86+
elif args.generate_contexts and not args.server:
87+
err = "Option `generate-contexts` is only for server functions (--server)."
8388

8489
if err:
8590
print_red("ERROR")
@@ -95,7 +100,8 @@ def add_function(args):
95100
server=args.server,
96101
logs_enabled=logs_enabled,
97102
generate=not args.disable_ai,
98-
execution_api_key=args.execution_api_key
103+
execution_api_key=args.execution_api_key,
104+
generate_contexts=args.generate_contexts
99105
)
100106

101107
fn_add_parser.set_defaults(command=add_function)

polyapi/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import configparser
44
from typing import Tuple
55

6-
from polyapi.utils import is_valid_polyapi_url, is_valid_uuid, print_green, print_yellow
6+
from polyapi.utils import is_valid_polyapi_url, print_green, print_yellow
77

88
# cached values
99
API_KEY = None
@@ -78,8 +78,6 @@ def initialize_config(force=False):
7878
errors = []
7979
if not is_valid_polyapi_url(url):
8080
errors.append(f"{url} is not a valid Poly API Base URL")
81-
if not is_valid_uuid(key):
82-
errors.append(f"{key} is not a valid Poly App Key or User Key")
8381
if errors:
8482
print_yellow("\n".join(errors))
8583
sys.exit(1)

polyapi/function_cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def function_add_or_update(
2424
client: bool,
2525
server: bool,
2626
logs_enabled: Optional[bool],
27+
generate_contexts: Optional[str],
2728
generate: bool = True,
2829
execution_api_key: str = ""
2930
):
@@ -59,6 +60,9 @@ def function_add_or_update(
5960
"logsEnabled": logs_enabled,
6061
}
6162

63+
if generate_contexts:
64+
data["generateContexts"] = generate_contexts.split(",")
65+
6266
if server and parsed["dependencies"]:
6367
print_yellow(
6468
"\nPlease note that deploying your functions will take a few minutes because it makes use of libraries other than polyapi."
@@ -87,7 +91,8 @@ def function_add_or_update(
8791
function_id = resp.json()["id"]
8892
print(f"Function ID: {function_id}")
8993
if generate:
90-
generate_library()
94+
contexts=generate_contexts.split(",") if generate_contexts else None
95+
generate_library(contexts=contexts)
9196
else:
9297
print("Error adding function.")
9398
print(resp.status_code)

polyapi/generate.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import requests
33
import os
44
import shutil
5-
from typing import List, Tuple, cast
5+
from typing import List, Optional, Tuple, cast
66

77
from .auth import render_auth_function
88
from .client import render_client_function
@@ -36,12 +36,16 @@
3636
path:'''
3737

3838

39-
def get_specs(no_types: bool = False) -> List:
39+
def get_specs(contexts=Optional[List[str]], no_types: bool = False) -> List:
4040
api_key, api_url = get_api_key_and_url()
4141
assert api_key
4242
headers = get_auth_headers(api_key)
4343
url = f"{api_url}/specs"
4444
params = {"noTypes": str(no_types).lower()}
45+
46+
if contexts:
47+
params["contexts"] = contexts
48+
4549
resp = requests.get(url, headers=headers, params=params)
4650
if resp.status_code == 200:
4751
return resp.json()
@@ -197,11 +201,12 @@ def remove_old_library():
197201
shutil.rmtree(path)
198202

199203

200-
def generate(no_types: bool = False) -> None:
201-
print("Generating Poly Python SDK...", end="", flush=True)
204+
def generate(contexts: Optional[List[str]], no_types: bool = False) -> None:
205+
generate_msg = f"Generating Poly Python SDK for contexts ${contexts}..." if contexts else "Generating Poly Python SDK..."
206+
print(generate_msg, end="", flush=True)
202207
remove_old_library()
203208

204-
specs = get_specs(no_types=no_types)
209+
specs = get_specs(no_types=no_types, contexts=contexts)
205210
cache_specs(specs)
206211

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

polyapi/utils.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import os
44
import uuid
5+
from urllib.parse import urlparse
56
from typing import Tuple, List
67
from colorama import Fore, Style
78
from polyapi.constants import BASIC_PYTHON_TYPES
@@ -261,21 +262,16 @@ def rewrite_arg_name(s: str):
261262

262263

263264
def is_valid_polyapi_url(_url: str):
265+
# in dev allow localhost (and 127.0.0.1) over http *or* https
266+
parsed = urlparse(_url)
267+
if parsed.scheme in ("http", "https") and parsed.hostname in ("localhost", "127.0.0.1"):
268+
return True
269+
264270
# Join the subdomains into a pattern
265271
subdomain_pattern = "|".join(valid_subdomains)
266272
pattern = rf"^https://({subdomain_pattern})\.polyapi\.io$"
267273
return re.match(pattern, _url) is not None
268274

269-
270-
def is_valid_uuid(uuid_string, version=4):
271-
try:
272-
uuid_obj = uuid.UUID(uuid_string, version=version)
273-
except ValueError:
274-
return False
275-
276-
return str(uuid_obj) == uuid_string
277-
278-
279275
def return_type_already_defined_in_args(return_type_name: str, args_def: str) -> bool:
280276
"""
281277
Checks if the return_type_name preceded optionally by 'class ' and followed by ' =' exists in args_def.

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.4.dev2"
6+
version = "0.3.5.dev0"
77
description = "The Python Client for PolyAPI, the IPaaS by Developers for Developers"
88
authors = [{ name = "Dan Fellin", email = "dan@polyapi.io" }]
99
dependencies = [

0 commit comments

Comments
 (0)