From 3805e5a9de8fc27d1a54111d03c4988afe24c466 Mon Sep 17 00:00:00 2001 From: pomdtr Date: Mon, 5 Jun 2023 18:38:05 +0200 Subject: [PATCH 1/3] update python starter with actions --- python-app/README.md | 6 ++++++ python-app/Spacefile | 3 +++ python-app/main.py | 30 ++++++++++++++++++++++++++++++ python-app/requirements.txt | 3 ++- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/python-app/README.md b/python-app/README.md index 99fa51b..dab0772 100644 --- a/python-app/README.md +++ b/python-app/README.md @@ -1,3 +1,9 @@ # Python app on Space An example Space app built with [Python](https://python.org) and [FastAPI](https://fastapi.tiangolo.com). + +## Triggering actions + +``` +space dev trigger -x list +``` diff --git a/python-app/Spacefile b/python-app/Spacefile index d2f147d..29a84d9 100644 --- a/python-app/Spacefile +++ b/python-app/Spacefile @@ -5,3 +5,6 @@ micros: src: . engine: python3.9 primary: true + provide_actions: true + run: uvicorn main:app + dev: .venv/bin/uvicorn main:app --reload diff --git a/python-app/main.py b/python-app/main.py index 9ca94bb..16ecba6 100644 --- a/python-app/main.py +++ b/python-app/main.py @@ -32,3 +32,33 @@ async def add_todo(item: TodoItem): resp = todos_base.put(item.dict()) # Return the response as JSON. return resp + +@app.post("/actions/list") +async def list_action(): + todos = todos_base.fetch() + # Return the items as JSON. + return todos.items + + +@app.get("/__space/actions") +async def actions(): + return { + "actions": [ + { + "name": "list", + "description": "List all todos", + "path": "/actions/list" + }, + { + "name": "add", + "path": "/api/todos", + "description": "Add a todo", + "input": [ + { + "name": "text", + "type": "string" + } + ] + } + ] + } diff --git a/python-app/requirements.txt b/python-app/requirements.txt index 962cf93..3f38d01 100644 --- a/python-app/requirements.txt +++ b/python-app/requirements.txt @@ -1,2 +1,3 @@ deta~=1.1.0 -fastapi~=0.92.0 +fastapi~=0.96.0 +uvicorn~=0.22.0 From 9f44918ac2b781a172258d6e375e48447df8abdc Mon Sep 17 00:00:00 2001 From: pomdtr Date: Mon, 5 Jun 2023 18:46:26 +0200 Subject: [PATCH 2/3] add action support to the nodejs example --- node-app/Spacefile | 2 ++ node-app/index.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/node-app/Spacefile b/node-app/Spacefile index 6ee512e..dd0cca9 100644 --- a/node-app/Spacefile +++ b/node-app/Spacefile @@ -6,3 +6,5 @@ micros: engine: nodejs16 primary: true run: node index.js + dev: node index.js + provide_actions: true diff --git a/node-app/index.js b/node-app/index.js index 9d051e6..ad4c4fb 100644 --- a/node-app/index.js +++ b/node-app/index.js @@ -30,6 +30,36 @@ app.post("/api/todos", async (request, response) => { response.send(resp); }); +app.post("/actions/list", async (request, response) => { + const todos = await todos_base.fetch(); + response.json({ + items: todos.items, + }); +}); + +app.get("/__space/actions", async (request, response) => { + response.json({ + actions: [ + { + name: "list", + title: "List todos", + path: "/actions/list", + }, + { + name: "add", + title: "Add todo", + path: "/api/todos", + input: [ + { + name: "text", + type: "string", + }, + ], + }, + ], + }); +}); + app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); }); From 1dcbd40c60641826198b7d8bc23da0cc99c9fc4a Mon Sep 17 00:00:00 2001 From: pomdtr Date: Mon, 12 Jun 2023 12:53:28 +0200 Subject: [PATCH 3/3] fix python example --- python-app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-app/main.py b/python-app/main.py index 16ecba6..166e6f8 100644 --- a/python-app/main.py +++ b/python-app/main.py @@ -46,13 +46,13 @@ async def actions(): "actions": [ { "name": "list", - "description": "List all todos", + "title": "List all todos", "path": "/actions/list" }, { "name": "add", "path": "/api/todos", - "description": "Add a todo", + "title": "Add a todo", "input": [ { "name": "text",