Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-primitive.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-primitive.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Q Consolidate duplicate QUICKSTARTs on the top-level file 503f830 7 months ago History
import.py · 5.5 KB · 202 lines
1"""
2Object Import API
3
4POST /cluster/import - Receive object files from another station
5
6Used by the migration system to receive files during object migration.
7This endpoint is called by the source station to push files to the destination.
8
9Example:
10 POST /cluster/import
11 {
12 "object_id": "calculator",
13 "code_file": "examples/calculator.py",
14 "code_content": "<base64>",
15 "state_files": {
16 "state.tsv": "<base64>",
17 "logs.tsv": "<base64>"
18 },
19 "version_files": {
20 "v1.txt": "<base64>",
21 "metadata.tsv": "<base64>"
22 }
23 }
24
25 Response:
26 {
27 "status": "ok",
28 "message": "Object imported successfully",
29 "files_copied": {
30 "code": "examples/calculator.py",
31 "state": ["state.tsv", "logs.tsv"],
32 "versions": 1
33 }
34 }
35"""
36import json
37import base64
38from pathlib import Path
39from dbbasic_web.responses import json as json_response, json_error
40
41
42def POST(request):
43 """
44 Import object files from another station
45
46 Request body:
47 {
48 "object_id": str,
49 "code_file": str, # Path where code should be written
50 "code_content": str, # Base64-encoded code
51 "state_files": { # Base64-encoded state files
52 "state.tsv": str,
53 "logs.tsv": str,
54 ...
55 },
56 "version_files": { # Base64-encoded version files
57 "v1.txt": str,
58 "metadata.tsv": str,
59 ...
60 }
61 }
62
63 Response:
64 {
65 "status": "ok",
66 "message": "Object imported successfully",
67 "files_copied": {
68 "code": str,
69 "state": [str],
70 "versions": int
71 }
72 }
73
74 Errors:
75 400 - Missing required fields or invalid base64
76 500 - File write failed
77 """
78 # Parse request body
79 try:
80 if request.body:
81 data = json.loads(request.body.decode('utf-8'))
82 else:
83 return json_error('Missing request body', status=400)
84 except json.JSONDecodeError as e:
85 return json_error(f'Invalid JSON: {e}', status=400)
86
87 # Validate required fields
88 object_id = data.get('object_id')
89 code_file_path = data.get('code_file')
90 code_content_b64 = data.get('code_content')
91 state_files_b64 = data.get('state_files', {})
92 version_files_b64 = data.get('version_files', {})
93
94 if not object_id:
95 return json_error('object_id is required', status=400)
96 if not code_file_path:
97 return json_error('code_file is required', status=400)
98 if not code_content_b64:
99 return json_error('code_content is required', status=400)
100
101 # Decode files
102 try:
103 code_content = base64.b64decode(code_content_b64)
104
105 state_files = {
106 name: base64.b64decode(content)
107 for name, content in state_files_b64.items()
108 }
109
110 version_files = {
111 name: base64.b64decode(content)
112 for name, content in version_files_b64.items()
113 }
114 except Exception as e:
115 return json_error(f'Failed to decode base64: {e}', status=400)
116
117 # Write files to filesystem
118 try:
119 files_dict = {
120 'code_file': code_file_path,
121 'code_content': code_content,
122 'state_files': state_files,
123 'version_files': version_files
124 }
125
126 result = write_files(object_id, files_dict)
127
128 except Exception as e:
129 import traceback
130 tb = traceback.format_exc()
131 return json_error(f'Failed to write files: {e}\n\n{tb}', status=500)
132
133 return json_response(json.dumps({
134 'status': 'ok',
135 'message': 'Object imported successfully',
136 'files_copied': result['files_copied']
137 }))
138
139
140def write_files(object_id: str, files: dict) -> dict:
141 """
142 Write files to local filesystem
143
144 Args:
145 object_id: Object ID
146 files: {
147 'code_file': str,
148 'code_content': bytes,
149 'state_files': {name: bytes},
150 'version_files': {rel_path: bytes}
151 }
152
153 Returns:
154 {
155 'files_copied': {
156 'code': str,
157 'state': [str],
158 'versions': int
159 }
160 }
161 """
162 # Write code file
163 code_file = Path(files['code_file'])
164 code_file.parent.mkdir(parents=True, exist_ok=True)
165
166 with open(code_file, 'wb') as f:
167 f.write(files['code_content'])
168
169 # Write state files
170 state_dir = Path('data') / object_id
171 state_dir.mkdir(parents=True, exist_ok=True)
172
173 state_files_written = []
174 for filename, content in files['state_files'].items():
175 file_path = state_dir / filename
176 with open(file_path, 'wb') as f:
177 f.write(content)
178 state_files_written.append(filename)
179
180 # Write version files
181 version_dir = Path('data/versions') / object_id
182 version_dir.mkdir(parents=True, exist_ok=True)
183
184 version_count = 0
185 for rel_path, content in files['version_files'].items():
186 file_path = version_dir / rel_path
187 file_path.parent.mkdir(parents=True, exist_ok=True)
188
189 with open(file_path, 'wb') as f:
190 f.write(content)
191
192 # Count actual version files (not metadata)
193 if rel_path.endswith('.txt'):
194 version_count += 1
195
196 return {
197 'files_copied': {
198 'code': str(code_file),
199 'state': state_files_written,
200 'versions': version_count
201 }
202 }