Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ function_add_test.py
lib_test*.py
polyapi/poly
polyapi/vari
polyapi/tabi
polyapi/schemas
6 changes: 3 additions & 3 deletions polyapi/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List, Dict, Any, Tuple
import uuid

from polyapi.typedefs import PropertySpecification
from polyapi.utils import parse_arguments, get_type_and_def
Expand All @@ -26,7 +25,8 @@ async def getToken(clientId: str, clientSecret: str, scopes: List[str], callback

Function ID: {function_id}
\"""
eventsClientId = "{client_id}"
from polyapi.poly.client_id import client_id
eventsClientId = client_id
function_id = "{function_id}"

options = options or {{}}
Expand Down Expand Up @@ -165,7 +165,7 @@ def render_auth_function(
func_str = ""

if function_name == "getToken":
func_str = GET_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description, client_id=uuid.uuid4().hex)
func_str = GET_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description)
elif function_name == "introspectToken":
func_str = INTROSPECT_TOKEN_TEMPLATE.format(function_id=function_id, description=function_description)
elif function_name == "refreshToken":
Expand Down
16 changes: 8 additions & 8 deletions polyapi/deployables.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ class SyncDeployment(TypedDict, total=False):
context: str
name: str
description: str
type: str
type: DeployableTypes
fileRevision: str
file: str
types: DeployableFunctionTypes
typeSchemas: Dict[str, any]
typeSchemas: Dict[str, Any]
dependencies: List[str]
config: Dict[str, any]
config: Dict[str, Any]
instance: str
id: Optional[str] = None
deployed: Optional[str] = None
id: Optional[str]
deployed: Optional[str]

DeployableTypeEntries: List[Tuple[DeployableTypeNames, DeployableTypes]] = [
("PolyServerFunction", "server-function"),
("PolyClientFunction", "client-function"),
("PolyServerFunction", "server-function"), # type: ignore
("PolyClientFunction", "client-function"), # type: ignore
]

DeployableTypeToName: Dict[DeployableTypeNames, DeployableTypes] = {name: type for name, type in DeployableTypeEntries}
Expand Down Expand Up @@ -175,7 +175,7 @@ def get_git_revision(branch_or_tag: str = "HEAD") -> str:
return check_output(["git", "rev-parse", "--short", branch_or_tag], text=True).strip()
except CalledProcessError:
# Return a random 7-character hash as a fallback
return "".join(format(ord(c), 'x') for c in os.urandom(4))[:7]
return "".join(format(ord(str(c)), 'x') for c in os.urandom(4))[:7]

def get_cache_deployments_revision() -> str:
"""Retrieve the cache deployments revision from a file."""
Expand Down
47 changes: 31 additions & 16 deletions polyapi/generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import requests
import os
import uuid
import shutil
import logging
import tempfile
Expand All @@ -13,11 +14,12 @@
from .poly_schemas import generate_schemas
from .webhook import render_webhook_handle

from .typedefs import PropertySpecification, SchemaSpecDto, SpecificationDto, VariableSpecDto
from .typedefs import PropertySpecification, SchemaSpecDto, SpecificationDto, VariableSpecDto, TableSpecDto
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, print_green, to_func_namespace
from .variables import generate_variables
from .poly_tables import generate_tables
from .config import get_api_key_and_url, get_direct_execute_config, get_cached_generate_args

SUPPORTED_FUNCTION_TYPES = {
Expand All @@ -28,7 +30,7 @@
"webhookHandle",
}

SUPPORTED_TYPES = SUPPORTED_FUNCTION_TYPES | {"serverVariable", "schema", "snippet"}
SUPPORTED_TYPES = SUPPORTED_FUNCTION_TYPES | {"serverVariable", "schema", "snippet", "table"}


X_POLY_REF_WARNING = '''"""
Expand Down Expand Up @@ -195,16 +197,18 @@ def read_cached_specs() -> List[SpecificationDto]:
return json.loads(f.read())


def get_variables() -> List[VariableSpecDto]:
specs = read_cached_specs()
def get_variables(specs: List[SpecificationDto]) -> List[VariableSpecDto]:
return [cast(VariableSpecDto, spec) for spec in specs if spec["type"] == "serverVariable"]


def get_schemas() -> List[SchemaSpecDto]:
specs = read_cached_specs()
def get_schemas(specs: List[SpecificationDto]) -> List[SchemaSpecDto]:
return [cast(SchemaSpecDto, spec) for spec in specs if spec["type"] == "schema"]


def get_tables(specs: List[SpecificationDto]) -> List[TableSpecDto]:
return [cast(TableSpecDto, spec) for spec in specs if spec["type"] == "table"]


def remove_old_library():
currdir = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(currdir, "poly")
Expand All @@ -219,6 +223,10 @@ def remove_old_library():
if os.path.exists(path):
shutil.rmtree(path)

path = os.path.join(currdir, "tabi")
if os.path.exists(path):
shutil.rmtree(path)


def create_empty_schemas_module():
"""Create an empty schemas module for no-types mode so user code can still import from polyapi.schemas"""
Expand Down Expand Up @@ -277,6 +285,14 @@ def __class_getitem__(cls, item):
''')


def _generate_client_id() -> None:
full_path = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(full_path, "poly", "client_id.py")
with open(full_path, "w") as f:
f.write(f'client_id = "{uuid.uuid4().hex}"')



def generate_from_cache() -> None:
"""
Generate using cached values after non-explicit call.
Expand Down Expand Up @@ -333,9 +349,11 @@ def generate(contexts: Optional[List[str]] = None, names: Optional[List[str]] =
limit_ids: List[str] = [] # useful for narrowing down generation to a single function to debug
functions = parse_function_specs(specs, limit_ids=limit_ids)

_generate_client_id()

# Only process schemas if no_types is False
if not no_types:
schemas = get_schemas()
schemas = get_schemas(specs)
schema_index = build_schema_index(schemas)
if schemas:
schema_limit_ids: List[str] = [] # useful for narrowing down generation to a single function to debug
Expand All @@ -359,7 +377,11 @@ def generate(contexts: Optional[List[str]] = None, names: Optional[List[str]] =
)
exit()

variables = get_variables()
tables = get_tables(specs)
if tables:
generate_tables(tables)

variables = get_variables(specs)
if variables:
generate_variables(variables)

Expand All @@ -371,14 +393,7 @@ def generate(contexts: Optional[List[str]] = None, names: Optional[List[str]] =


def clear() -> None:
base = os.path.dirname(os.path.abspath(__file__))
poly_path = os.path.join(base, "poly")
if os.path.exists(poly_path):
shutil.rmtree(poly_path)

vari_path = os.path.join(base, "vari")
if os.path.exists(vari_path):
shutil.rmtree(vari_path)
remove_old_library()
print("Cleared!")


Expand Down
Loading