From 9c002886d2e7d3034ec648bea4bdf5297ea920f1 Mon Sep 17 00:00:00 2001 From: Cesar Date: Fri, 12 Jul 2024 01:13:38 +0200 Subject: [PATCH] feat: serverless function `get_user_tasks` init --- .github/workflows/cd.dev-functions.yaml | 64 +++++++ autotx/serverless_functions/get_user_tasks.py | 42 +++++ poetry.lock | 164 +++++++++++++++++- pyproject.toml | 1 + 4 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/cd.dev-functions.yaml create mode 100644 autotx/serverless_functions/get_user_tasks.py diff --git a/.github/workflows/cd.dev-functions.yaml b/.github/workflows/cd.dev-functions.yaml new file mode 100644 index 0000000..38784aa --- /dev/null +++ b/.github/workflows/cd.dev-functions.yaml @@ -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 }} diff --git a/autotx/serverless_functions/get_user_tasks.py b/autotx/serverless_functions/get_user_tasks.py new file mode 100644 index 0000000..dc4fb3f --- /dev/null +++ b/autotx/serverless_functions/get_user_tasks.py @@ -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"} diff --git a/poetry.lock b/poetry.lock index 7831d5a..653ace9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -304,6 +304,17 @@ files = [ {file = "bitarray-2.9.2.tar.gz", hash = "sha256:a8f286a51a32323715d77755ed959f94bef13972e9a2fe71b609e40e6d27957e"}, ] +[[package]] +name = "blinker" +version = "1.8.2" +description = "Fast, simple object-to-object and broadcast signaling" +optional = false +python-versions = ">=3.8" +files = [ + {file = "blinker-1.8.2-py3-none-any.whl", hash = "sha256:1779309f71bf239144b9399d06ae925637cf6634cf6bd131104184531bf67c01"}, + {file = "blinker-1.8.2.tar.gz", hash = "sha256:8f77b09d3bf7c795e969e9486f39c2c5e9c39d4ee07424be2bc594ece9642d83"}, +] + [[package]] name = "cached-property" version = "1.5.2" @@ -533,6 +544,23 @@ files = [ [package.dependencies] colorama = {version = "*", markers = "platform_system == \"Windows\""} +[[package]] +name = "cloudevents" +version = "1.11.0" +description = "CloudEvents Python SDK" +optional = false +python-versions = "*" +files = [ + {file = "cloudevents-1.11.0-py3-none-any.whl", hash = "sha256:77edb4f2b01f405c44ea77120c3213418dbc63d8859f98e9e85de875502b8a76"}, + {file = "cloudevents-1.11.0.tar.gz", hash = "sha256:5be990583e99f3b08af5a709460e20b25cb169270227957a20b47a6ec8635e66"}, +] + +[package.dependencies] +deprecation = ">=2.0,<3.0" + +[package.extras] +pydantic = ["pydantic (>=1.0.0,<3.0)"] + [[package]] name = "coingecko" version = "0.13" @@ -1062,6 +1090,28 @@ test = ["catboost (>=0.26,<1.2)", "coverage (>=5.3)", "dataclasses", "datasets", ts-forecast = ["hcrystalball (==0.1.10)", "holidays (<0.14)", "prophet (>=1.0.1)", "statsmodels (>=0.12.2)"] vw = ["scikit-learn", "vowpalwabbit (>=8.10.0,<9.0.0)"] +[[package]] +name = "flask" +version = "3.0.3" +description = "A simple framework for building complex web applications." +optional = false +python-versions = ">=3.8" +files = [ + {file = "flask-3.0.3-py3-none-any.whl", hash = "sha256:34e815dfaa43340d1d15a5c3a02b8476004037eb4840b34910c6e21679d288f3"}, + {file = "flask-3.0.3.tar.gz", hash = "sha256:ceb27b0af3823ea2737928a4d99d125a06175b8512c445cbd9a9ce200ef76842"}, +] + +[package.dependencies] +blinker = ">=1.6.2" +click = ">=8.1.3" +itsdangerous = ">=2.1.2" +Jinja2 = ">=3.1.2" +Werkzeug = ">=3.0.0" + +[package.extras] +async = ["asgiref (>=3.2)"] +dotenv = ["python-dotenv"] + [[package]] name = "frozenlist" version = "1.4.1" @@ -1148,6 +1198,25 @@ files = [ {file = "frozenlist-1.4.1.tar.gz", hash = "sha256:c037a86e8513059a2613aaba4d817bb90b9d9b6b69aace3ce9c877e8c8ed402b"}, ] +[[package]] +name = "functions-framework" +version = "3.8.0" +description = "An open source FaaS (Function as a service) framework for writing portable Python functions -- brought to you by the Google Cloud Functions team." +optional = false +python-versions = "<4,>=3.5" +files = [ + {file = "functions_framework-3.8.0-py3-none-any.whl", hash = "sha256:fdad66496c7fd3030a8170a8dfad1672f4713532e788e345d132e755634a07b7"}, + {file = "functions_framework-3.8.0.tar.gz", hash = "sha256:c92c0e5e9b6ca1968edd54bf07da9aa4f8a9525f21ef761365ef35b352a73d2d"}, +] + +[package.dependencies] +click = ">=7.0,<9.0" +cloudevents = ">=1.2.0,<2.0.0" +flask = ">=1.0,<4.0" +gunicorn = {version = ">=19.2.0", markers = "platform_system != \"Windows\""} +watchdog = ">=1.0.0" +Werkzeug = ">=0.14,<4.0.0" + [[package]] name = "gotrue" version = "2.5.4" @@ -1163,6 +1232,27 @@ files = [ httpx = ">=0.23,<0.28" pydantic = ">=1.10,<3" +[[package]] +name = "gunicorn" +version = "22.0.0" +description = "WSGI HTTP Server for UNIX" +optional = false +python-versions = ">=3.7" +files = [ + {file = "gunicorn-22.0.0-py3-none-any.whl", hash = "sha256:350679f91b24062c86e386e198a15438d53a7a8207235a78ba1b53df4c4378d9"}, + {file = "gunicorn-22.0.0.tar.gz", hash = "sha256:4a0b436239ff76fb33f11c07a16482c521a7e09c1ce3cc293c2330afe01bec63"}, +] + +[package.dependencies] +packaging = "*" + +[package.extras] +eventlet = ["eventlet (>=0.24.1,!=0.36.0)"] +gevent = ["gevent (>=1.4.0)"] +setproctitle = ["setproctitle"] +testing = ["coverage", "eventlet", "gevent", "pytest", "pytest-cov"] +tornado = ["tornado (>=0.2)"] + [[package]] name = "h11" version = "0.14.0" @@ -1306,6 +1396,17 @@ files = [ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, ] +[[package]] +name = "itsdangerous" +version = "2.2.0" +description = "Safely pass data to untrusted environments and back." +optional = false +python-versions = ">=3.8" +files = [ + {file = "itsdangerous-2.2.0-py3-none-any.whl", hash = "sha256:c6242fc49e35958c8b15141343aa660db5fc54d4f13a1db01a3f5891b98700ef"}, + {file = "itsdangerous-2.2.0.tar.gz", hash = "sha256:e0050c0b7da1eea53ffaf149c0cfbb5c6e2e2b69c4bef22c81fa6eb73e5f6173"}, +] + [[package]] name = "jinja2" version = "3.1.4" @@ -3205,6 +3306,50 @@ PyYAML = "*" wrapt = "*" yarl = "*" +[[package]] +name = "watchdog" +version = "4.0.1" +description = "Filesystem events monitoring" +optional = false +python-versions = ">=3.8" +files = [ + {file = "watchdog-4.0.1-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:da2dfdaa8006eb6a71051795856bedd97e5b03e57da96f98e375682c48850645"}, + {file = "watchdog-4.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:e93f451f2dfa433d97765ca2634628b789b49ba8b504fdde5837cdcf25fdb53b"}, + {file = "watchdog-4.0.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:ef0107bbb6a55f5be727cfc2ef945d5676b97bffb8425650dadbb184be9f9a2b"}, + {file = "watchdog-4.0.1-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:17e32f147d8bf9657e0922c0940bcde863b894cd871dbb694beb6704cfbd2fb5"}, + {file = "watchdog-4.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:03e70d2df2258fb6cb0e95bbdbe06c16e608af94a3ffbd2b90c3f1e83eb10767"}, + {file = "watchdog-4.0.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:123587af84260c991dc5f62a6e7ef3d1c57dfddc99faacee508c71d287248459"}, + {file = "watchdog-4.0.1-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:093b23e6906a8b97051191a4a0c73a77ecc958121d42346274c6af6520dec175"}, + {file = "watchdog-4.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:611be3904f9843f0529c35a3ff3fd617449463cb4b73b1633950b3d97fa4bfb7"}, + {file = "watchdog-4.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:62c613ad689ddcb11707f030e722fa929f322ef7e4f18f5335d2b73c61a85c28"}, + {file = "watchdog-4.0.1-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:d4925e4bf7b9bddd1c3de13c9b8a2cdb89a468f640e66fbfabaf735bd85b3e35"}, + {file = "watchdog-4.0.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:cad0bbd66cd59fc474b4a4376bc5ac3fc698723510cbb64091c2a793b18654db"}, + {file = "watchdog-4.0.1-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:a3c2c317a8fb53e5b3d25790553796105501a235343f5d2bf23bb8649c2c8709"}, + {file = "watchdog-4.0.1-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:c9904904b6564d4ee8a1ed820db76185a3c96e05560c776c79a6ce5ab71888ba"}, + {file = "watchdog-4.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:667f3c579e813fcbad1b784db7a1aaa96524bed53437e119f6a2f5de4db04235"}, + {file = "watchdog-4.0.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:d10a681c9a1d5a77e75c48a3b8e1a9f2ae2928eda463e8d33660437705659682"}, + {file = "watchdog-4.0.1-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:0144c0ea9997b92615af1d94afc0c217e07ce2c14912c7b1a5731776329fcfc7"}, + {file = "watchdog-4.0.1-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:998d2be6976a0ee3a81fb8e2777900c28641fb5bfbd0c84717d89bca0addcdc5"}, + {file = "watchdog-4.0.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl", hash = "sha256:e7921319fe4430b11278d924ef66d4daa469fafb1da679a2e48c935fa27af193"}, + {file = "watchdog-4.0.1-pp38-pypy38_pp73-macosx_11_0_arm64.whl", hash = "sha256:f0de0f284248ab40188f23380b03b59126d1479cd59940f2a34f8852db710625"}, + {file = "watchdog-4.0.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl", hash = "sha256:bca36be5707e81b9e6ce3208d92d95540d4ca244c006b61511753583c81c70dd"}, + {file = "watchdog-4.0.1-pp39-pypy39_pp73-macosx_11_0_arm64.whl", hash = "sha256:ab998f567ebdf6b1da7dc1e5accfaa7c6992244629c0fdaef062f43249bd8dee"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_aarch64.whl", hash = "sha256:dddba7ca1c807045323b6af4ff80f5ddc4d654c8bce8317dde1bd96b128ed253"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_armv7l.whl", hash = "sha256:4513ec234c68b14d4161440e07f995f231be21a09329051e67a2118a7a612d2d"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_i686.whl", hash = "sha256:4107ac5ab936a63952dea2a46a734a23230aa2f6f9db1291bf171dac3ebd53c6"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_ppc64.whl", hash = "sha256:6e8c70d2cd745daec2a08734d9f63092b793ad97612470a0ee4cbb8f5f705c57"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_ppc64le.whl", hash = "sha256:f27279d060e2ab24c0aa98363ff906d2386aa6c4dc2f1a374655d4e02a6c5e5e"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_s390x.whl", hash = "sha256:f8affdf3c0f0466e69f5b3917cdd042f89c8c63aebdb9f7c078996f607cdb0f5"}, + {file = "watchdog-4.0.1-py3-none-manylinux2014_x86_64.whl", hash = "sha256:ac7041b385f04c047fcc2951dc001671dee1b7e0615cde772e84b01fbf68ee84"}, + {file = "watchdog-4.0.1-py3-none-win32.whl", hash = "sha256:206afc3d964f9a233e6ad34618ec60b9837d0582b500b63687e34011e15bb429"}, + {file = "watchdog-4.0.1-py3-none-win_amd64.whl", hash = "sha256:7577b3c43e5909623149f76b099ac49a1a01ca4e167d1785c76eb52fa585745a"}, + {file = "watchdog-4.0.1-py3-none-win_ia64.whl", hash = "sha256:d7b9f5f3299e8dd230880b6c55504a1f69cf1e4316275d1b215ebdd8187ec88d"}, + {file = "watchdog-4.0.1.tar.gz", hash = "sha256:eebaacf674fa25511e8867028d281e602ee6500045b57f43b08778082f7f8b44"}, +] + +[package.extras] +watchmedo = ["PyYAML (>=3.10)"] + [[package]] name = "watchfiles" version = "0.22.0" @@ -3407,6 +3552,23 @@ files = [ {file = "websockets-12.0.tar.gz", hash = "sha256:81df9cbcbb6c260de1e007e58c011bfebe2dafc8435107b0537f393dd38c8b1b"}, ] +[[package]] +name = "werkzeug" +version = "3.0.3" +description = "The comprehensive WSGI web application library." +optional = false +python-versions = ">=3.8" +files = [ + {file = "werkzeug-3.0.3-py3-none-any.whl", hash = "sha256:fc9645dc43e03e4d630d23143a04a7f947a9a3b5727cd535fdfe155a17cc48c8"}, + {file = "werkzeug-3.0.3.tar.gz", hash = "sha256:097e5bfda9f0aba8da6b8545146def481d06aa7d3266e7448e2cccf67dd8bd18"}, +] + +[package.dependencies] +MarkupSafe = ">=2.1.1" + +[package.extras] +watchdog = ["watchdog (>=2.3)"] + [[package]] name = "wrapt" version = "1.16.0" @@ -3592,4 +3754,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.13" -content-hash = "c358c59f6f591afe7264818076545c27f31d51e67663bd4b67c8227bfe8689ab" +content-hash = "ef23ba7605f46ddced0500a44fb912f6c853f8ad696b25e1e0a9abd6f31a3f65" diff --git a/pyproject.toml b/pyproject.toml index df271af..fb63ffe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"