askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
objects.py · 1.3 KB · 49 lines
| 1 | """ |
| 2 | API handler for listing all objects |
| 3 | |
| 4 | GET /objects - List all available objects |
| 5 | """ |
| 6 | import json |
| 7 | from pathlib import Path |
| 8 | from dbbasic_web.responses import json as json_response, json_error |
| 9 | |
| 10 | |
| 11 | def GET(request): |
| 12 | """List all available objects""" |
| 13 | # Find all .py files in examples directory |
| 14 | examples_dir = Path('examples') |
| 15 | |
| 16 | if not examples_dir.exists(): |
| 17 | return json_response(json.dumps({ |
| 18 | 'status': 'ok', |
| 19 | 'objects': [], |
| 20 | 'count': 0, |
| 21 | 'message': 'No objects found (examples directory does not exist)', |
| 22 | })) |
| 23 | |
| 24 | objects = [] |
| 25 | |
| 26 | # Scan for .py files |
| 27 | for py_file in examples_dir.rglob('*.py'): |
| 28 | # Skip __init__.py and __pycache__ |
| 29 | if py_file.name == '__init__.py' or '__pycache__' in str(py_file): |
| 30 | continue |
| 31 | |
| 32 | # Get relative path from examples/ |
| 33 | rel_path = py_file.relative_to(examples_dir) |
| 34 | object_id = str(rel_path).replace('.py', '').replace('/', '_') |
| 35 | |
| 36 | objects.append({ |
| 37 | 'object_id': object_id, |
| 38 | 'path': str(rel_path), |
| 39 | 'file': str(py_file), |
| 40 | }) |
| 41 | |
| 42 | # Sort by object_id |
| 43 | objects.sort(key=lambda x: x['object_id']) |
| 44 | |
| 45 | return json_response(json.dumps({ |
| 46 | 'status': 'ok', |
| 47 | 'objects': objects, |
| 48 | 'count': len(objects), |
| 49 | })) |