-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrendered_spec.py
More file actions
69 lines (58 loc) · 2.21 KB
/
rendered_spec.py
File metadata and controls
69 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from typing import Optional
from polyapi import http_client
from polyapi.config import get_api_key_and_url
from polyapi.generate import read_cached_specs, render_spec
from polyapi.typedefs import SpecificationDto
def update_rendered_spec(spec: SpecificationDto):
print("Updating rendered spec...")
func_str, type_defs = render_spec(spec)
data = {
"language": "python",
"signature": func_str,
"typedefs": type_defs,
}
if spec["type"] == "apiFunction":
data["apiFunctionId"] = spec["id"]
elif spec["type"] == "serverFunction":
data["customFunctionId"] = spec["id"]
elif spec["type"] == "clientFunction":
data["customFunctionId"] = spec["id"]
elif spec["type"] == "webhookHandle":
data["webhookHandleId"] = spec["id"]
else:
raise NotImplementedError("todo")
# use super key on develop-k8s here!
api_key, base_url = get_api_key_and_url()
url = f"{base_url}/functions/rendered-specs"
headers = {"Authorization": f"Bearer {api_key}"}
resp = http_client.post(url, json=data, headers=headers)
assert resp.status_code == 201, (resp.text, resp.status_code)
def _get_spec(spec_id: str, no_types: bool = False) -> Optional[SpecificationDto]:
api_key, base_url = get_api_key_and_url()
url = f"{base_url}/specs"
headers = {"Authorization": f"Bearer {api_key}"}
params = {"noTypes": str(no_types).lower()}
resp = http_client.get(url, headers=headers, params=params)
if resp.status_code == 200:
specs = resp.json()
for spec in specs:
if spec['id'] == spec_id:
return spec
return None
else:
raise NotImplementedError(resp.content)
def get_and_update_rendered_spec(spec_id: str) -> bool:
spec = _get_spec(spec_id)
if spec:
update_rendered_spec(spec)
return True
return False
def save_rendered_specs() -> None:
specs = read_cached_specs()
# right now we just support rendered apiFunctions
api_specs = [spec for spec in specs if spec["type"] == "apiFunction"]
for spec in api_specs:
assert spec["function"]
print("adding", spec["context"], spec["name"])
update_rendered_spec(spec)