polyapi-python 0.3.20.dev4__py3-none-any.whl → 0.3.21__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
polyapi/ai_function.py CHANGED
@@ -75,8 +75,7 @@ def render_ai_function(
75
75
 
76
76
 
77
77
  def _normalize_return_type_for_annotation(function_name: str, return_type_name: str) -> str:
78
- if return_type_name == "ReturnType":
79
- return "ReturnType"
78
+ # "ReturnType" lives in a separate module, so it must be qualified (e.g. _SFX.ReturnType).
80
79
  return add_type_import_path(function_name, return_type_name)
81
80
 
82
81
 
polyapi/cli.py CHANGED
@@ -9,7 +9,6 @@ from .cli_constants import CLI_COMMANDS
9
9
  from .config import initialize_config, set_api_key_and_url
10
10
  from .generate import generate, clear
11
11
  from .function_cli import function_add_or_update, function_execute
12
- from .rendered_spec import get_and_update_rendered_spec
13
12
  from .prepare import prepare_deployables
14
13
  from .sync import sync_deployables
15
14
 
@@ -196,22 +195,6 @@ def execute_from_cli():
196
195
  clear_parser.set_defaults(command=clear_command)
197
196
 
198
197
 
199
- ###########################################################################
200
- # Update rendered spec command
201
- update_spec_parser = subparsers.add_parser("update_rendered_spec", help="Update the rendered spec file")
202
- update_spec_parser.add_argument("spec", help="Specification file to update")
203
-
204
- def update_rendered_spec(args):
205
- updated = get_and_update_rendered_spec(args.spec)
206
- if updated:
207
- print("Updated rendered spec!")
208
- else:
209
- print("Failed to update rendered spec!")
210
- exit(1)
211
-
212
- update_spec_parser.set_defaults(command=update_rendered_spec)
213
-
214
-
215
198
  ###########################################################################
216
199
  # Prepare command
217
200
  prepare_parser = subparsers.add_parser('prepare', help="Find and prepare all Poly deployables")
polyapi/cli_constants.py CHANGED
@@ -4,7 +4,6 @@ CLI_COMMANDS = (
4
4
  "function",
5
5
  "clear",
6
6
  "help",
7
- "update_rendered_spec",
8
7
  "prepare",
9
8
  "sync",
10
9
  )
polyapi/deployables.py CHANGED
@@ -51,7 +51,7 @@ class DeployableFunctionTypes(TypedDict):
51
51
  params: List[DeployableFunctionParam]
52
52
  returns: DeployableFunctionParamBase
53
53
 
54
- class DeployableRecord(ParsedDeployableConfig, total=False):
54
+ class DeployableRecord(ParsedDeployableConfig):
55
55
  gitRevision: str
56
56
  fileRevision: str
57
57
  file: str
@@ -64,7 +64,7 @@ class DeployableRecord(ParsedDeployableConfig, total=False):
64
64
  docEndIndex: int
65
65
  dirty: Optional[bool]
66
66
 
67
- class SyncDeployment(TypedDict, total=False):
67
+ class SyncDeployment(TypedDict):
68
68
  context: str
69
69
  name: str
70
70
  description: str
@@ -195,30 +195,12 @@ def write_cache_revision(git_revision: Optional[str] = None) -> None:
195
195
  with open(CACHE_VERSION_FILE, 'w', encoding='utf-8') as file:
196
196
  file.write(git_revision)
197
197
 
198
- def is_cache_up_to_date() -> bool:
199
- if not Path(CACHE_VERSION_FILE).exists():
200
- return False
201
- with open(CACHE_VERSION_FILE, 'r', encoding='utf-8') as file:
202
- cached_revision = file.read().strip()
203
- git_revision = get_git_revision()
204
- return cached_revision == git_revision
205
-
206
198
  def is_cache_up_to_date() -> bool:
207
199
  """Check if the cached revision matches the current Git revision."""
208
200
  cached_revision = get_cache_deployments_revision()
209
201
  git_revision = get_git_revision() # This function needs to be defined or imported
210
202
  return cached_revision == git_revision
211
203
 
212
- def write_deploy_comments(deployments: List[Dict]) -> str:
213
- """Generate a string of deployment comments for each deployment."""
214
- canopy_path = 'polyui/collections' if 'localhost' in os.getenv('POLY_API_BASE_URL', '') else 'canopy/polyui/collections'
215
- comments = []
216
- for d in deployments:
217
- instance_url = d['instance'].replace(':8000', ':3000') if d['instance'].endswith(':8000') else d['instance']
218
- comment = f"# Poly deployed @ {d['deployed']} - {d['context']}.{d['name']} - {instance_url}/{canopy_path}/{d['type']}s/{d['id']} - {d['fileRevision']}"
219
- comments.append(comment)
220
- return '\n'.join(comments)
221
-
222
204
  def print_docstring_function_comment(description: str, args: list, returns: dict) -> str:
223
205
  docstring = f'"""{description}\n\n'
224
206
  if args:
@@ -243,7 +225,7 @@ def print_docstring_function_comment(description: str, args: list, returns: dict
243
225
  return docstring
244
226
 
245
227
 
246
- def update_deployment_comments(file_content: str, deployable: dict) -> str:
228
+ def update_deployment_comments(file_content: str, deployable: DeployableRecord) -> str:
247
229
  """
248
230
  Remove old deployment comments based on the provided ranges and add new ones.
249
231
  """
@@ -255,7 +237,7 @@ def update_deployment_comments(file_content: str, deployable: dict) -> str:
255
237
  file_content = f"{deployment_comments}\n{file_content}"
256
238
  return file_content
257
239
 
258
- def update_deployable_function_comments(file_content: str, deployable: dict, disable_docs: bool = False) -> str:
240
+ def update_deployable_function_comments(file_content: str, deployable: DeployableRecord, disable_docs: bool = False) -> str:
259
241
  """
260
242
  Update the docstring in the file content based on the deployable's documentation data.
261
243
  """
@@ -263,7 +245,7 @@ def update_deployable_function_comments(file_content: str, deployable: dict, dis
263
245
  docstring = print_docstring_function_comment(
264
246
  deployable['types']['description'],
265
247
  deployable['types']['params'],
266
- deployable['types']['returns']
248
+ deployable['types']['returns'] # pyright: ignore[reportArgumentType]
267
249
  )
268
250
  if deployable["docStartIndex"] == deployable["docEndIndex"]:
269
251
  # Function doesn't yet have any docstrings so we need to add additional whitespace
@@ -272,7 +254,7 @@ def update_deployable_function_comments(file_content: str, deployable: dict, dis
272
254
  return f"{file_content[:deployable['docStartIndex']]}{docstring}{file_content[deployable['docEndIndex']:]}"
273
255
  return file_content
274
256
 
275
- def write_updated_deployable(deployable: dict, disable_docs: bool = False) -> dict:
257
+ def write_updated_deployable(deployable: DeployableRecord, disable_docs: bool = False) -> DeployableRecord:
276
258
  """
277
259
  Read the deployable's file, update its comments and docstring, and write back to the file.
278
260
  """
polyapi/error_handler.py CHANGED
@@ -42,7 +42,8 @@ async def unregister_all():
42
42
  _, base_url = get_api_key_and_url()
43
43
  # need to reconnect because maybe socketio client disconnected after Ctrl+C?
44
44
  try:
45
- await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
45
+ if client:
46
+ await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
46
47
  except ConnectionError:
47
48
  pass
48
49
  await asyncio.gather(*[unregister(handler) for handler in active_handlers])
@@ -63,10 +64,11 @@ async def on(
63
64
  data["apiKey"] = api_key
64
65
 
65
66
  def registerCallback(id: int):
66
- nonlocal handler_id
67
- handler_id = id
68
- client.on(f"handleError:{handler_id}", callback, namespace="/events")
69
- active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
67
+ if client:
68
+ nonlocal handler_id
69
+ handler_id = id
70
+ client.on(f"handleError:{handler_id}", callback, namespace="/events")
71
+ active_handlers.append({"path": path, "id": handler_id, "apiKey": api_key})
70
72
 
71
73
  await client.emit("registerErrorHandler", data, "/events", registerCallback)
72
74
 
polyapi/generate.py CHANGED
@@ -7,7 +7,7 @@ import tempfile
7
7
  import stat
8
8
 
9
9
  from copy import deepcopy
10
- from typing import Any, List, Optional, Tuple, cast
10
+ from typing import Any, Dict, List, Optional, Tuple, cast
11
11
 
12
12
  from .auth import render_auth_function
13
13
  from .client import render_client_function
@@ -104,7 +104,7 @@ def replace_poly_refs_in_functions(specs: List[SpecificationDto], schema_index):
104
104
  func = spec.get("function")
105
105
  if func:
106
106
  try:
107
- spec["function"] = resolve_poly_refs(func, schema_index)
107
+ spec["function"] = resolve_poly_refs(func, schema_index) # pyright: ignore[reportGeneralTypeIssues]
108
108
  except Exception:
109
109
  # print()
110
110
  # print(f"{spec['context']}.{spec['name']} (id: {spec['id']}) failed to resolve poly refs, skipping!")
@@ -123,7 +123,7 @@ def replace_poly_refs_in_schemas(specs: List[SchemaSpecDto], schema_index):
123
123
  spec_idxs_to_remove = []
124
124
  for idx, spec in enumerate(specs):
125
125
  try:
126
- spec["definition"] = resolve_poly_refs(spec["definition"], schema_index)
126
+ spec["definition"] = cast(Dict[str, Any], resolve_poly_refs(spec["definition"], schema_index))
127
127
  except Exception:
128
128
  # print()
129
129
  print(f"{spec['context']}.{spec['name']} (id: {spec['id']}) failed to resolve poly refs, skipping!")
@@ -445,16 +445,16 @@ def render_spec(spec: SpecificationDto) -> Tuple[str, str]:
445
445
  arguments: List[PropertySpecification] = []
446
446
  return_type: Any = {}
447
447
  if spec.get("function"):
448
- assert spec["function"]
448
+ function_spec = cast(Dict[str, Any], spec["function"]) # pyright: ignore[reportTypedDictNotRequiredAccess]
449
449
  # Handle cases where arguments might be missing or None
450
- if spec["function"].get("arguments"):
450
+ if function_spec.get("arguments"):
451
451
  arguments = [
452
- arg for arg in spec["function"]["arguments"]
452
+ arg for arg in function_spec["arguments"]
453
453
  ]
454
-
454
+
455
455
  # Handle cases where returnType might be missing or None
456
- if spec["function"].get("returnType"):
457
- return_type = spec["function"]["returnType"]
456
+ if function_spec.get("returnType"):
457
+ return_type = function_spec["returnType"]
458
458
  else:
459
459
  # Provide a fallback return type when missing
460
460
  return_type = {"kind": "any"}
polyapi/parser.py CHANGED
@@ -5,13 +5,18 @@ import sys
5
5
  import re
6
6
  from typing import Dict, List, Mapping, Optional, Tuple, Any, Union
7
7
  from typing import _TypedDictMeta as BaseTypedDict # type: ignore
8
- from typing_extensions import _TypedDictMeta, cast # type: ignore
8
+ from typing_extensions import TypedDict, _TypedDictMeta, cast # type: ignore
9
9
  from stdlib_list import stdlib_list
10
10
  from pydantic import TypeAdapter
11
11
  from importlib.metadata import packages_distributions
12
12
  from polyapi.constants import PYTHON_TO_JSONSCHEMA_TYPE_MAP
13
13
  from polyapi.utils import print_red
14
- from polyapi.deployables import Deployment, DeployableRecord, get_deployable_file_revision
14
+ from polyapi.deployables import (
15
+ Deployment,
16
+ DeployableRecord,
17
+ DeployableTypes,
18
+ get_deployable_file_revision,
19
+ )
15
20
 
16
21
 
17
22
  # these libraries are already installed in the base docker image
@@ -116,15 +121,53 @@ def _parse_sphinx_docstring(docstring: str) -> Dict[str, Any]:
116
121
  }
117
122
 
118
123
 
119
- def _parse_google_docstring(docstring: str) -> Dict[str, Any]:
124
+ class _GoogleParamState(TypedDict):
125
+ name: str
126
+ type: str
127
+ description: List[str]
128
+
129
+
130
+ class _GoogleReturnsState(TypedDict):
131
+ type: str
132
+ description: List[str]
133
+
134
+
135
+ class GoogleDocstringParam(TypedDict):
136
+ name: str
137
+ type: str
138
+ typeSchema: Optional[Dict[str, Any]]
139
+ description: str
140
+
141
+
142
+ class GoogleDocstringReturns(TypedDict):
143
+ type: str
144
+ typeSchema: Optional[Dict[str, Any]]
145
+ description: str
146
+
147
+
148
+ class GoogleDocstring(TypedDict):
149
+ description: str
150
+ params: List[GoogleDocstringParam]
151
+ returns: GoogleDocstringReturns
152
+ raises: Dict[str, str]
153
+
154
+
155
+ class _GoogleDocstringState(TypedDict):
156
+ description: List[str]
157
+ params: Dict[str, _GoogleParamState]
158
+ returns: _GoogleReturnsState
159
+ raises: Dict[str, str]
160
+
161
+
162
+ def _parse_google_docstring(docstring: str) -> GoogleDocstring:
120
163
  import re
121
164
  lines = docstring.split('\n')
122
165
  mode = None
123
- params = {}
124
- parsed = {
166
+ params: Dict[str, _GoogleParamState] = {}
167
+ parsed: _GoogleDocstringState = {
125
168
  'description': [],
126
- 'params': [],
127
- 'returns': {'description': []},
169
+ 'params': params,
170
+ 'returns': {'type': '', 'description': []},
128
171
  'raises': {}
129
172
  }
130
173
  current_key = None
@@ -169,12 +212,25 @@ def _parse_google_docstring(docstring: str) -> Dict[str, Any]:
169
212
  elif mode is None:
170
213
  parsed['description'].append(line.strip())
171
214
 
172
- # Consolidate descriptions
173
- parsed['description'] = ' '.join(parsed['description']).strip()
174
- parsed['returns']['description'] = ' '.join(parsed['returns']['description']).strip()
175
- parsed['params'] = [{ **v, 'description': ' '.join(v['description']).strip() } for v in params.values()]
215
+ parsed_result: GoogleDocstring = {
216
+ 'description': ' '.join(parsed['description']).strip(),
217
+ 'params': [
218
+ {
219
+ **value,
220
+ 'typeSchema': None,
221
+ 'description': ' '.join(value['description']).strip(),
222
+ }
223
+ for value in params.values()
224
+ ],
225
+ 'returns': {
226
+ 'type': parsed['returns']['type'],
227
+ 'typeSchema': None,
228
+ 'description': ' '.join(parsed['returns']['description']).strip(),
229
+ },
230
+ 'raises': parsed['raises'],
231
+ }
176
232
 
177
- return parsed
233
+ return parsed_result
178
234
 
179
235
 
180
236
  def _get_schemas(code: str) -> List[Dict]:
@@ -299,12 +355,12 @@ def _parse_deploy_comment(comment: str) -> Optional[Deployment]:
299
355
 
300
356
  # Local development puts canopy on a different port than the poly-server
301
357
  if instance.endswith("localhost:3000"):
302
- instance = instance.replace(":3000', ':8000")
358
+ instance = instance.replace(":3000", ":8000")
303
359
 
304
360
  return {
305
361
  "name": name,
306
362
  "context": context,
307
- "type": deploy_type,
363
+ "type": DeployableTypes(deploy_type),
308
364
  "id": id,
309
365
  "deployed": deployed,
310
366
  "fileRevision": file_revision,
@@ -398,9 +454,9 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
398
454
  ):
399
455
  # We've found a polyConfig dictionary assignment
400
456
  if node.annotation.id == "PolyServerFunction":
401
- deployable["type"] = "server-function"
457
+ deployable["type"] = "server-function" # pyright: ignore[reportGeneralTypeIssues]
402
458
  elif node.annotation.id == "PolyClientFunction":
403
- deployable["type"] = "client-function"
459
+ deployable["type"] = "client-function" # pyright: ignore[reportGeneralTypeIssues]
404
460
  else:
405
461
  print_red("ERROR")
406
462
  print(f"Unsupported polyConfig type '${node.annotation.id}'")
@@ -466,7 +522,7 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
466
522
  if deployment:
467
523
  start = self._line_offsets[i]
468
524
  deployable["deployments"].append(deployment)
469
- deployable["deploymentCommentRanges"].append([start, start + len(line)])
525
+ deployable["deploymentCommentRanges"].append((start, start + len(line)))
470
526
 
471
527
  def visit_Import(self, node: ast.Import):
472
528
  # TODO maybe handle `import foo.bar` case?
@@ -539,7 +595,7 @@ def parse_function_code(code: str, name: Optional[str] = "", context: Optional[s
539
595
 
540
596
  def generic_visit(self, node):
541
597
  if hasattr(node, 'lineno') and hasattr(node, 'col_offset'):
542
- self._current_offset = self._line_offsets[node.lineno - 1] + node.col_offset
598
+ self._current_offset = self._line_offsets[node.lineno - 1] + node.col_offset # pyright: ignore[reportAttributeAccessIssue]
543
599
  super().generic_visit(node)
544
600
 
545
601
  tree = ast.parse(code)
polyapi/poly_tables.py CHANGED
@@ -74,7 +74,12 @@ def scrub_keys(e: Exception) -> Dict[str, Any]:
74
74
 
75
75
  def execute_query(table_id, method, query):
76
76
  from polyapi import polyCustom
77
- from polyapi.poly.client_id import client_id
77
+
78
+ client_id: Optional[str] = None
79
+ try:
80
+ from polyapi.poly.client_id import client_id # pyright: ignore[reportMissingImports]
81
+ except ImportError:
82
+ pass
78
83
 
79
84
  try:
80
85
  api_key, base_url = get_api_key_and_url()
@@ -84,7 +89,9 @@ def execute_query(table_id, method, query):
84
89
  )
85
90
 
86
91
  auth_key = polyCustom.get("executionApiKey") or api_key
87
- url = f"{base_url.rstrip('/')}/tables/{table_id}/{method}?clientId={client_id}"
92
+ url = f"{base_url.rstrip('/')}/tables/{table_id}/{method}"
93
+ if client_id:
94
+ url += f"?clientId={client_id}"
88
95
  headers = {"x-poly-execution-id": polyCustom.get("executionId") or ""}
89
96
  if auth_key:
90
97
  headers["Authorization"] = f"Bearer {auth_key}"
polyapi/prepare.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import os
2
2
  import sys
3
3
  import subprocess
4
- from typing import List, Tuple, Literal
4
+ from typing import Any, Dict, List, Tuple, Literal, TypeGuard
5
5
  from polyapi import http_client
6
6
  from polyapi.utils import get_auth_headers
7
7
  from polyapi.config import get_api_key_and_url
@@ -10,7 +10,7 @@ from polyapi.deployables import (
10
10
  prepare_deployable_directory, write_cache_revision,
11
11
  save_deployable_records, get_all_deployable_files,
12
12
  is_cache_up_to_date, get_git_revision,
13
- write_updated_deployable, DeployableRecord
13
+ write_updated_deployable, DeployableRecord, PolyDeployConfig
14
14
  )
15
15
 
16
16
  class FunctionArgumentDto:
@@ -19,7 +19,15 @@ class FunctionArgumentDto:
19
19
  self.type = type
20
20
  self.description = description
21
21
 
22
- def get_function_description(deploy_type: Literal["server-function", "client-function"], description: str, arguments, code: str) -> str:
22
+
23
+ FunctionDeployType = Literal["server-function", "client-function"]
24
+
25
+
26
+ def _is_function_deploy_type(deploy_type: str) -> TypeGuard[FunctionDeployType]:
27
+ return deploy_type in ("server-function", "client-function")
28
+
29
+
30
+ def get_function_description(deploy_type: FunctionDeployType, description: str, arguments, code: str) -> Dict[str, Any]:
23
31
  if deploy_type == "server-function":
24
32
  return get_server_function_description(description, arguments, code)
25
33
  elif deploy_type == "client-function":
@@ -27,14 +35,14 @@ def get_function_description(deploy_type: Literal["server-function", "client-fun
27
35
  else:
28
36
  raise ValueError("Unsupported deployable type")
29
37
 
30
- def get_server_function_description(description: str, arguments, code: str) -> str:
38
+ def get_server_function_description(description: str, arguments, code: str) -> Dict[str, Any]:
31
39
  api_key, api_url = get_api_key_and_url()
32
40
  headers = get_auth_headers(api_key)
33
41
  data = {"description": description, "arguments": arguments, "code": code}
34
42
  response = http_client.post(f"{api_url}/functions/server/description-generation", headers=headers, json=data)
35
43
  return response.json()
36
44
 
37
- def get_client_function_description(description: str, arguments, code: str) -> str:
45
+ def get_client_function_description(description: str, arguments, code: str) -> Dict[str, Any]:
38
46
  api_key, api_url = get_api_key_and_url()
39
47
  headers = get_auth_headers(api_key)
40
48
  # Simulated API call to generate client function descriptions
@@ -50,8 +58,12 @@ def fill_in_missing_function_details(deployable: DeployableRecord, code: str) ->
50
58
  )
51
59
  if is_missing_descriptions:
52
60
  try:
61
+ deploy_type = deployable["type"]
62
+ if not _is_function_deploy_type(deploy_type):
63
+ raise ValueError(f'Unsupported deployable type: "{deploy_type}"')
64
+
53
65
  ai_generated = get_function_description(
54
- deployable["type"],
66
+ deploy_type,
55
67
  deployable["types"]["description"],
56
68
  [{"name": p["name"], "type": p["type"], "description": p.get("description")} for p in deployable["types"]["params"]],
57
69
  code
@@ -84,7 +96,13 @@ def get_base_url() -> str:
84
96
  def get_all_deployables(disable_docs: bool, disable_ai: bool, git_revision: str) -> List[DeployableRecord]:
85
97
  print("Searching for poly deployables.")
86
98
  base_url = get_base_url() or "."
87
- possible_deployables = get_all_deployable_files({"includeDirs": [base_url]})
99
+ deployable_search_config: PolyDeployConfig = {
100
+ "type_names": [],
101
+ "include_dirs": [base_url],
102
+ "include_files_or_extensions": [],
103
+ "exclude_dirs": [],
104
+ }
105
+ possible_deployables = get_all_deployable_files(deployable_search_config)
88
106
  print(f'Found {len(possible_deployables)} possible deployable file{"s" if len(possible_deployables) != 1 else ""}.')
89
107
 
90
108
  found = {}
polyapi/server.py CHANGED
@@ -75,8 +75,7 @@ def render_server_function(
75
75
 
76
76
 
77
77
  def _normalize_return_type_for_annotation(function_name: str, return_type_name: str) -> str:
78
- if return_type_name == "ReturnType":
79
- return "ReturnType"
78
+ # "ReturnType" lives in a separate module, so it must be qualified (e.g. _SFX.ReturnType).
80
79
  return add_type_import_path(function_name, return_type_name)
81
80
 
82
81
 
polyapi/sync.py CHANGED
@@ -22,7 +22,7 @@ def read_file(file_path: str) -> str:
22
22
  with open(file_path, 'r', encoding='utf-8') as file:
23
23
  return file.read()
24
24
 
25
- def group_by(items: List[Dict], key: str) -> Dict[str, List[Dict]]:
25
+ def group_by(items: List[DeployableRecord], key: str) -> Dict[str, List[DeployableRecord]]:
26
26
  grouped = {} # type: ignore
27
27
  for item in items:
28
28
  grouped.setdefault(item[key], []).append(item)
@@ -50,7 +50,7 @@ def remove_deployable(deployable: SyncDeployment) -> bool:
50
50
  def sync_function_and_get_id(deployable: SyncDeployment, code: str) -> str:
51
51
  api_key, _ = get_api_key_and_url()
52
52
  if not api_key:
53
- raise Error("Missing api key!")
53
+ raise Exception("Missing api key!")
54
54
  headers = get_auth_headers(api_key)
55
55
  url = f'{deployable["instance"]}/functions/{deployable["type"].replace("-function", "")}'
56
56
  payload = {
polyapi/utils.py CHANGED
@@ -41,7 +41,9 @@ def add_import_to_init(full_path: str, next: str, code_imports: Optional[str] =
41
41
  f.write(import_stmt)
42
42
 
43
43
 
44
- def get_auth_headers(api_key: str):
44
+ def get_auth_headers(api_key: Optional[str]) -> dict[str, str]:
45
+ if api_key is None:
46
+ raise ValueError("An API key is required to build authorization headers")
45
47
  return {"Authorization": f"Bearer {api_key}"}
46
48
 
47
49
 
@@ -102,7 +104,8 @@ def normalize_cross_language_type(type_name: str) -> str:
102
104
  normalized = [normalize_cross_language_type(part) for part in parts]
103
105
  return " | ".join(normalized) if normalized else "Any"
104
106
 
105
- if value == "ReturnType" or value.startswith("ReturnType<") or "typeof" in value:
107
+ # Only TS's ReturnType<...>/typeof syntax falls back to Any, not our own "ReturnType" name.
108
+ if value.startswith("ReturnType<") or "typeof" in value:
106
109
  return "Any"
107
110
 
108
111
  return primitive_map.get(value, value)
@@ -220,8 +223,12 @@ def get_type_and_def( # noqa: C901
220
223
  return "List", ""
221
224
  elif title and title == "ReturnType" and schema.get("type"):
222
225
  assert isinstance(title, str)
223
- schema_type = schema.get("type", "Any")
224
- root_type, generated_code = wrapped_generate_schema_types(schema, schema_type, "Dict") # type: ignore
226
+ # Use `title` as the class name, not schema["type"] (e.g. "object" collides with dict).
227
+ root_type, generated_code = wrapped_generate_schema_types(schema, title, "Dict") # type: ignore
228
+ if f"class {root_type}(" not in generated_code:
229
+ # A property-less object schema (e.g. bare {"type": "object"}) generates no
230
+ # class at all, so root_type would reference a name that doesn't exist.
231
+ return "Dict", ""
225
232
  return (map_primitive_types(root_type), "") if is_primitive(root_type) else (root_type, generated_code) # type: ignore
226
233
  elif title:
227
234
  assert isinstance(title, str)
polyapi/variables.py CHANGED
@@ -118,14 +118,16 @@ def render_variable(variable: VariableSpecDto):
118
118
  def _get_variable_type(type_spec: PropertyType) -> str:
119
119
  # simplified version of _get_type from api.py
120
120
  if type_spec["kind"] == "plain":
121
- value = type_spec["value"]
122
- if value.endswith("[]"):
121
+ value = type_spec.get("value")
122
+ if value and value.endswith("[]"):
123
123
  primitive = map_primitive_types(value[:-2])
124
124
  return f"List[{primitive}]"
125
- else:
125
+ elif value:
126
126
  return map_primitive_types(value)
127
+ else:
128
+ return "Any"
127
129
  elif type_spec["kind"] == "primitive":
128
- return map_primitive_types(type_spec["type"])
130
+ return map_primitive_types(type_spec.get("type", "not gonna find this so it will fallback to any"))
129
131
  elif type_spec["kind"] == "array":
130
132
  return "List"
131
133
  elif type_spec["kind"] == "void":
polyapi/webhook.py CHANGED
@@ -107,7 +107,8 @@ async def unregister_all():
107
107
  # maybe need to reconnect because maybe socketio client disconnected after Ctrl+C?
108
108
  # feels like Linux disconnects but Windows stays connected
109
109
  try:
110
- await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
110
+ if client:
111
+ await client.connect(base_url, transports=["websocket"], namespaces=["/events"])
111
112
  except ConnectionError:
112
113
  pass
113
114
  await asyncio.gather(*[unregister(handler) for handler in active_handlers])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: polyapi-python
3
- Version: 0.3.20.dev4
3
+ Version: 0.3.21
4
4
  Summary: The Python SDK for the integration platform PolyAPI
5
5
  Author-email: PolyAPI <support@polyapi.io>
6
6
  License: MIT License
@@ -28,15 +28,23 @@ Project-URL: Homepage, https://github.com/polyapi/polyapi-python
28
28
  Requires-Python: >=3.10
29
29
  Description-Content-Type: text/markdown
30
30
  License-File: LICENSE
31
- Requires-Dist: requests==2.32.3
31
+ Requires-Dist: requests==2.33.0
32
32
  Requires-Dist: typing_extensions==4.15.0
33
33
  Requires-Dist: jsonschema-gentypes==2.13.0
34
34
  Requires-Dist: pydantic==2.13.4
35
35
  Requires-Dist: stdlib_list==0.11.1
36
36
  Requires-Dist: colorama==0.4.4
37
- Requires-Dist: python-socketio[asyncio_client]==5.11.1
37
+ Requires-Dist: python-socketio[asyncio_client]==5.16.2
38
38
  Requires-Dist: truststore==0.9.1
39
39
  Requires-Dist: httpx==0.28.1
40
+ Provides-Extra: dev
41
+ Requires-Dist: mock==5.2.0; extra == "dev"
42
+ Requires-Dist: pytest; extra == "dev"
43
+ Requires-Dist: flask==3.1.3; extra == "dev"
44
+ Requires-Dist: mypy; extra == "dev"
45
+ Requires-Dist: types-requests; extra == "dev"
46
+ Requires-Dist: types-colorama; extra == "dev"
47
+ Requires-Dist: pip-audit; extra == "dev"
40
48
  Dynamic: license-file
41
49
 
42
50
  # PolyAPI Python Library
@@ -0,0 +1,34 @@
1
+ polyapi/__init__.py,sha256=oEijoOVghgeEbwqtWK_74QU38HBDt4THXHBbsortJ1w,5505
2
+ polyapi/__main__.py,sha256=V4zhAh_YGxno5f_KSrlkELxcuDh9bR3WSd0n-2r-qQQ,93
3
+ polyapi/ai_function.py,sha256=ZvB0ojfDCa4Y7Qe-iwNXdhoR0f5f-wPKu6UwDIxeOgE,3043
4
+ polyapi/api.py,sha256=CzqX99GekhQJVqAAWlL03hUenIi_5iTln9cWgQzz6Ko,2784
5
+ polyapi/auth.py,sha256=l0pGAoYvEWqa2TMsrM9GS8imXu3GsWvpn4eFPy1vksI,6195
6
+ polyapi/cli.py,sha256=52bskBYsPAXbGjzy1Wiizrg4Rc-VBRNPs_n_avIJcG0,11242
7
+ polyapi/cli_constants.py,sha256=b2cYiobmorzpr20dUKv1K2AKhA-bL7eIqLYIH4leWLk,116
8
+ polyapi/client.py,sha256=DW6ljG_xCwAo2yz23A9QfLooE6ZUDvSpdA4e_dCQjiQ,1418
9
+ polyapi/config.py,sha256=HArQg9BtZQxUCbi4OHLqvkwvjnnnDTJsuJW7niLo_5k,7328
10
+ polyapi/constants.py,sha256=i3j0-ptd7XEs6ovnYegEhiP0BRs1JSKAq1oE5CWbDVo,707
11
+ polyapi/deployables.py,sha256=mt0zkBMXmgx6kSixKdiERyxiF2UJvamtbcb8ZiJoaEA,11355
12
+ polyapi/error_handler.py,sha256=eBFawL9zfi02c_R1_6OsiIKwvHTy9J6nslmzcVvY9qs,2456
13
+ polyapi/exceptions.py,sha256=Zh7i7eCUhDuXEdUYjatkLFTeZkrx1BJ1P5ePgbJ9eIY,89
14
+ polyapi/execute.py,sha256=4jLkQely6cM7pNN9pYpGZTbEis-DfwENi5Fl07xSiig,8966
15
+ polyapi/function_cli.py,sha256=gJE72lRvuWnGl6CG9GpqL78TOnUzxVe4kJMvg0PQixw,4493
16
+ polyapi/generate.py,sha256=1M9JqTVBnv3qGy3auYTtXqWqG9J736kCTA6lWbSI6T0,22537
17
+ polyapi/http_client.py,sha256=5N-zWSsjdMCPnrICR45EJWtXtBNyMP42L6Q8_Bh7n1A,15198
18
+ polyapi/parser.py,sha256=ep5UbcWToTQuUiL8a4E2nlWd4bfjOydLytl7s5CnBl0,23198
19
+ polyapi/poly_schemas.py,sha256=JGMQbELPFw4V_0nA0o71WJKFTdxjCGoKqJvIid_Y-a4,10759
20
+ polyapi/poly_tables.py,sha256=DSmzALnTDNrDkBnEia_AQyfD0s5pJewJHlBD8k1TROo,20466
21
+ polyapi/prepare.py,sha256=fDJcBH54AZGHetBxnVFuQywU_ru39vCwNPuR715tfH0,8071
22
+ polyapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
+ polyapi/schema.py,sha256=SPO5GDvIE1BjRfJR89vJMEGCXERfEbhNXQm2A05UAOc,6959
24
+ polyapi/server.py,sha256=OFyMiNT8z4393e0hD7UzMQMpH1dqFpZGSUe5dzA0aEY,3075
25
+ polyapi/sync.py,sha256=9rBQauAqYvKjkXIoQocCHRXN8GiOHhNv8U9OF9yGKOY,6794
26
+ polyapi/typedefs.py,sha256=UeAdyYrgqxrvBRkjisK_xgzQmuTePPvOM74HKf1y0II,5656
27
+ polyapi/utils.py,sha256=e4wZpAnlMAOfeSS2Zvs8BJtnVjr0cw7Gu1Y-3PwN-mk,16837
28
+ polyapi/variables.py,sha256=yg9WT-sLMBPL7DFSFN9RGSB9-8sud8fbmmCoFHrt0Qc,7695
29
+ polyapi/webhook.py,sha256=1wBKXQQjKskcGjRelrI5P3AgLqlJ1fUJyc4LxUthzi8,5131
30
+ polyapi_python-0.3.21.dist-info/licenses/LICENSE,sha256=6b_I7aPVp8JXhqQwdw7_B84Ca0S4JGjHj0sr_1VOdB4,1068
31
+ polyapi_python-0.3.21.dist-info/METADATA,sha256=WMkKPtoOzDtppfAgAjrXaQlS9Bk700tdUVuCpC2MsDw,8046
32
+ polyapi_python-0.3.21.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
33
+ polyapi_python-0.3.21.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
34
+ polyapi_python-0.3.21.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (84.0.0)
2
+ Generator: setuptools (84.2.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
polyapi/rendered_spec.py DELETED
@@ -1,69 +0,0 @@
1
- import os
2
- from typing import Optional
3
-
4
- from polyapi import http_client
5
- from polyapi.config import get_api_key_and_url
6
- from polyapi.generate import read_cached_specs, render_spec
7
- from polyapi.typedefs import SpecificationDto
8
-
9
-
10
- def update_rendered_spec(spec: SpecificationDto):
11
- print("Updating rendered spec...")
12
- func_str, type_defs = render_spec(spec)
13
- data = {
14
- "language": "python",
15
- "signature": func_str,
16
- "typedefs": type_defs,
17
- }
18
- if spec["type"] == "apiFunction":
19
- data["apiFunctionId"] = spec["id"]
20
- elif spec["type"] == "serverFunction":
21
- data["customFunctionId"] = spec["id"]
22
- elif spec["type"] == "clientFunction":
23
- data["customFunctionId"] = spec["id"]
24
- elif spec["type"] == "webhookHandle":
25
- data["webhookHandleId"] = spec["id"]
26
- else:
27
- raise NotImplementedError("todo")
28
-
29
- # use super key on develop-k8s here!
30
- api_key, base_url = get_api_key_and_url()
31
-
32
- url = f"{base_url}/functions/rendered-specs"
33
- headers = {"Authorization": f"Bearer {api_key}"}
34
- resp = http_client.post(url, json=data, headers=headers)
35
- assert resp.status_code == 201, (resp.text, resp.status_code)
36
-
37
-
38
- def _get_spec(spec_id: str, no_types: bool = False) -> Optional[SpecificationDto]:
39
- api_key, base_url = get_api_key_and_url()
40
- url = f"{base_url}/specs"
41
- headers = {"Authorization": f"Bearer {api_key}"}
42
- params = {"noTypes": str(no_types).lower()}
43
- resp = http_client.get(url, headers=headers, params=params)
44
- if resp.status_code == 200:
45
- specs = resp.json()
46
- for spec in specs:
47
- if spec['id'] == spec_id:
48
- return spec
49
- return None
50
- else:
51
- raise NotImplementedError(resp.content)
52
-
53
-
54
- def get_and_update_rendered_spec(spec_id: str) -> bool:
55
- spec = _get_spec(spec_id)
56
- if spec:
57
- update_rendered_spec(spec)
58
- return True
59
- return False
60
-
61
-
62
- def save_rendered_specs() -> None:
63
- specs = read_cached_specs()
64
- # right now we just support rendered apiFunctions
65
- api_specs = [spec for spec in specs if spec["type"] == "apiFunction"]
66
- for spec in api_specs:
67
- assert spec["function"]
68
- print("adding", spec["context"], spec["name"])
69
- update_rendered_spec(spec)
@@ -1,35 +0,0 @@
1
- polyapi/__init__.py,sha256=oEijoOVghgeEbwqtWK_74QU38HBDt4THXHBbsortJ1w,5505
2
- polyapi/__main__.py,sha256=V4zhAh_YGxno5f_KSrlkELxcuDh9bR3WSd0n-2r-qQQ,93
3
- polyapi/ai_function.py,sha256=GvJKpBZFbI11y9JAKTHXMkztbMHZxNTfL37LLbGKmwI,3017
4
- polyapi/api.py,sha256=CzqX99GekhQJVqAAWlL03hUenIi_5iTln9cWgQzz6Ko,2784
5
- polyapi/auth.py,sha256=l0pGAoYvEWqa2TMsrM9GS8imXu3GsWvpn4eFPy1vksI,6195
6
- polyapi/cli.py,sha256=V3586ElV1927u2AhQZAo_4UYKoJPqE8lz6b3_gJfMj0,11918
7
- polyapi/cli_constants.py,sha256=eZnAqAo8HLxHH3cdAoGWbrsALAtEWJFXRdSS2lvQvzE,144
8
- polyapi/client.py,sha256=DW6ljG_xCwAo2yz23A9QfLooE6ZUDvSpdA4e_dCQjiQ,1418
9
- polyapi/config.py,sha256=HArQg9BtZQxUCbi4OHLqvkwvjnnnDTJsuJW7niLo_5k,7328
10
- polyapi/constants.py,sha256=i3j0-ptd7XEs6ovnYegEhiP0BRs1JSKAq1oE5CWbDVo,707
11
- polyapi/deployables.py,sha256=njmqTU0W7d3JdDWhjjjr2UOghEv4bTKUN-3ztJvcC7o,12233
12
- polyapi/error_handler.py,sha256=I_e0iz6VM23FLVQWJljxs2NGcl_OODbi43OcbnqBlp8,2398
13
- polyapi/exceptions.py,sha256=Zh7i7eCUhDuXEdUYjatkLFTeZkrx1BJ1P5ePgbJ9eIY,89
14
- polyapi/execute.py,sha256=4jLkQely6cM7pNN9pYpGZTbEis-DfwENi5Fl07xSiig,8966
15
- polyapi/function_cli.py,sha256=gJE72lRvuWnGl6CG9GpqL78TOnUzxVe4kJMvg0PQixw,4493
16
- polyapi/generate.py,sha256=U8JQ_XR2gJI_eYNnmvXReILUxapj95QKoBronqMFkyE,22403
17
- polyapi/http_client.py,sha256=5N-zWSsjdMCPnrICR45EJWtXtBNyMP42L6Q8_Bh7n1A,15198
18
- polyapi/parser.py,sha256=t3jJI6g5F7Qg_tirPOzSkQ02VLkSVhnqs9YGapyHx2k,21901
19
- polyapi/poly_schemas.py,sha256=JGMQbELPFw4V_0nA0o71WJKFTdxjCGoKqJvIid_Y-a4,10759
20
- polyapi/poly_tables.py,sha256=SlP7ZwqS3nIji2uGR2VJQH6IdddZ832rRItcmoYdIBc,20293
21
- polyapi/prepare.py,sha256=DBlrQu_A0PqdaQ3KlMs-C6PkV3qncatkAiBlC7j2hnk,7441
22
- polyapi/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- polyapi/rendered_spec.py,sha256=J2Num7MXnW_mKAe2MSVIL1coWopNYZ-6nNNwQRfseV0,2262
24
- polyapi/schema.py,sha256=SPO5GDvIE1BjRfJR89vJMEGCXERfEbhNXQm2A05UAOc,6959
25
- polyapi/server.py,sha256=_14lP4_BmRYMBXwaqXVWmiSTHH3h7ecCHxSalM9UiSc,3049
26
- polyapi/sync.py,sha256=t0Khh8B5GylxB7KWRP_xvkrvkuyVarSLCOvtmaz7lcc,6766
27
- polyapi/typedefs.py,sha256=UeAdyYrgqxrvBRkjisK_xgzQmuTePPvOM74HKf1y0II,5656
28
- polyapi/utils.py,sha256=LUqTkbb0b7y3gjsOU-WO1LVYaTMyExTGpYjKY8wVKg8,16300
29
- polyapi/variables.py,sha256=hfSDPGQK6YphNCa9MqE0W88WIFfqCQWgpBDWkExMq-A,7582
30
- polyapi/webhook.py,sha256=0ceLwHNjNd2Yx_8MX5jOIxiQ5Lwhy2pe81ProAuKon0,5108
31
- polyapi_python-0.3.20.dev4.dist-info/licenses/LICENSE,sha256=6b_I7aPVp8JXhqQwdw7_B84Ca0S4JGjHj0sr_1VOdB4,1068
32
- polyapi_python-0.3.20.dev4.dist-info/METADATA,sha256=wYIJAgguqGC3FNXxYZHtQcLtumtopR5vf29f4X7W9rI,7737
33
- polyapi_python-0.3.20.dev4.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
34
- polyapi_python-0.3.20.dev4.dist-info/top_level.txt,sha256=CEFllOnzowci_50RYJac-M54KD2IdAptFsayVVF_f04,8
35
- polyapi_python-0.3.20.dev4.dist-info/RECORD,,