Skip to content

Commit

Permalink
feat: serverless function get_user_tasks init
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrzn committed Jul 11, 2024
1 parent 45d5a5f commit 9c00288
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 1 deletion.
64 changes: 64 additions & 0 deletions .github/workflows/cd.dev-functions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Deploy Serverless Function

on:
push:
branches:
- chore/serverless-function
pull_request:
types: [closed]
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

permissions:
contents: "read"
id-token: "write"


steps:
- uses: "actions/checkout@v4"

- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: "3.10"

- name: Install and configure Poetry
uses: snok/install-poetry@v1
with:
version: 1.5.1
virtualenvs-create: true
virtualenvs-in-project: true

- name: Export requirements.txt from Poetry
run: poetry export --without-hashes --format=requirements.txt > requirements.txt

- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
workload_identity_provider: ${{ secrets.GOOGLE_WIP }}
service_account: ${{ secrets.GOOGLE_SA }}

- name: "Set up Cloud SDK"
uses: "google-github-actions/setup-gcloud@v1"
with:
version: ">= 474.0.0"

- name: "Use gcloud CLI"
run: "gcloud info"

- name: "Deploy get_user_tasks function"
run: |
gcloud functions deploy get_user_tasks \
--project=autotx-423210 \
--gen2 \
--source=autotx/serverless_functions/get_user_tasks.py
--runtime=python310 \
--region=us-central1 \
--trigger-http \
--allow-unauthenticated \
--entry-point=get_user_tasks \
--set-env-vars SUPABASE_URL="${{ secrets.DEV_SUPABASE_URL }}",SUPABASE_SERVICE_ROLE_KEY="${{ secrets.DEV_SUPABASE_SERVICE_ROLE_KEY }}
42 changes: 42 additions & 0 deletions autotx/serverless_functions/get_user_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from fastapi import HTTPException
from flask import Request
from flask.typing import ResponseReturnValue
import functions_framework
from autotx import db
from autotx.server import authorize_app_and_user


@functions_framework.http
def get_user_tasks(request: Request) -> ResponseReturnValue:
if request.method != "GET":
return "Method not allowed"

try:
(app, app_user) = authorize_app_and_user(
request.authorization, request.args["userDid"]
)
tasks = db.TasksRepository(app_id=app.id).get_from_user(app_user.id)
user_tasks = [
{
"id": task.id,
"prompt": task.prompt,
"address": task.address,
"chain_id": task.chain_id,
"intents": task.intents,
}
for task in tasks
]
user_submitted_transactions = [
batch
for batch in db.get_submitted_transactions_from_user(app.id, app_user.id)
]

return {
"tasks": user_tasks,
"submitted_transactions": user_submitted_transactions,
}
except HTTPException as e:
return {"status": e.status_code, "message": e.detail}
except Exception as e:
print(e)
return {"status": 500, "message": "Internal error"}
164 changes: 163 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ supabase = "^2.5.0"
llama-cpp-python = "^0.2.78"
aiohttp = "^3.9.5"
pytest-timeout = "^2.3.1"
functions-framework = "^3.8.0"

[tool.poetry.group.dev.dependencies]
mypy = "^1.8.0"
Expand Down

0 comments on commit 9c00288

Please sign in to comment.