|
| 1 | +import chalice |
| 2 | +import chalice.app |
| 3 | +import chalicelib.template_manager as template_manager |
| 4 | +import chalicelib.util.type_util as type_util |
| 5 | + |
| 6 | +template_manager_api = chalice.app.Blueprint(__name__) |
| 7 | + |
| 8 | + |
| 9 | +@template_manager_api.route("/", methods=["GET"]) |
| 10 | +def list_template_manager_services() -> dict[str, list[str]]: |
| 11 | + return {"template_managers": list(template_manager.template_managers.keys())} |
| 12 | + |
| 13 | + |
| 14 | +@template_manager_api.route("/{service_name}", methods=["GET"]) |
| 15 | +def list_templates(service_name: str) -> dict[str, list[dict[str, str]]]: |
| 16 | + if service_name not in template_manager.template_managers: |
| 17 | + raise chalice.NotFoundError(f"Service {service_name} not found") |
| 18 | + return {"templates": [t.model_dump(mode="json") for t in template_manager.template_managers[service_name].list()]} |
| 19 | + |
| 20 | + |
| 21 | +@template_manager_api.route("/{service_name}/{code}", methods=["GET", "POST", "PUT", "DELETE"]) |
| 22 | +def crud_template(service_name: str, code: str) -> dict[str, str]: |
| 23 | + request: chalice.app.Request = template_manager_api.current_request |
| 24 | + method: type_util.HttpMethodType = request.method.upper() |
| 25 | + payload: dict[str, str] | None = request.json_body if method in {"POST", "PUT"} else None |
| 26 | + |
| 27 | + if not (template_mgr := template_manager.template_managers.get(service_name, None)): |
| 28 | + raise chalice.NotFoundError(f"Service {service_name} not found") |
| 29 | + |
| 30 | + if method == "GET": |
| 31 | + if template_info := template_mgr.retrieve(code): |
| 32 | + return template_info.model_dump(mode="json") |
| 33 | + raise chalice.NotFoundError(f"Template {code} not found") |
| 34 | + elif method == "POST": |
| 35 | + if not payload: |
| 36 | + raise chalice.BadRequestError("Payload is required") |
| 37 | + return template_mgr.create(code, payload).model_dump(mode="json") |
| 38 | + elif method == "PUT": |
| 39 | + if not payload: |
| 40 | + raise chalice.BadRequestError("Payload is required") |
| 41 | + return template_mgr.update(code, payload).model_dump(mode="json") |
| 42 | + elif method == "DELETE": |
| 43 | + template_mgr.delete(code) |
| 44 | + return {"code": code} |
| 45 | + |
| 46 | + raise chalice.NotFoundError(f"Method {method} is not allowed") |
| 47 | + |
| 48 | + |
| 49 | +blueprints: dict[str, chalice.app.Blueprint] = {"/template-manager": template_manager_api} |
0 commit comments