diff --git a/.cspell.jsonc b/.cspell.jsonc index 301e2bf..01c2e79 100644 --- a/.cspell.jsonc +++ b/.cspell.jsonc @@ -9,6 +9,7 @@ "ecies", "eciespy", "fastapi", + "pydantic", "secp256k1", "Spacefile", "uvicorn", diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index c01713c..2733f52 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -24,4 +24,4 @@ jobs: with: access_token: ${{ secrets.ACCESS_TOKEN }} project_id: ${{ secrets.PROJECT_ID }} - space_release: true + space_push: true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dfa1901..db26302 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,10 +19,4 @@ jobs: cache: "poetry" - run: poetry export -f requirements.txt -o requirements.txt --without=dev - - run: poetry install --only main - - - uses: neobrains/space-pipe@v0.5 - with: - access_token: ${{ secrets.ACCESS_TOKEN }} - project_id: ${{ secrets.PROJECT_ID }} - space_push: true + - run: poetry install diff --git a/main.py b/main.py index 3d0fba0..973af6f 100644 --- a/main.py +++ b/main.py @@ -1,17 +1,18 @@ from typing import Optional from ecies import decrypt, encrypt -from fastapi import FastAPI, Form, HTTPException -from fastapi.responses import Response +from fastapi import FastAPI, Form, Response +from fastapi.responses import JSONResponse +from pydantic import BaseModel app = FastAPI() -def resp_string(msg): - return Response(content=msg, media_type="plain/text") +class Error(BaseModel): + detail: str -@app.post("/") +@app.post("/", responses={400: {"model": Error}}) async def encrypt_decrypt( prv: Optional[str] = Form(None), pub: Optional[str] = Form(None), @@ -22,12 +23,20 @@ async def encrypt_decrypt( decrypted = decrypt(prv, bytes.fromhex(data)) return resp_string(decrypted) except ValueError: - raise HTTPException(status_code=400, detail="Invalid private key") + return resp_error("Invalid private key or data") elif pub and data: try: encrypted = encrypt(pub, data.encode()) return resp_string(encrypted.hex()) except ValueError: - raise HTTPException(status_code=400, detail="Invalid public key") + return resp_error("Invalid public key or data") else: - raise HTTPException(status_code=400, detail="Invalid request") + return resp_error("Invalid request") + + +def resp_string(msg): + return Response(content=msg, media_type="plain/text") + + +def resp_error(msg): + return JSONResponse(content={"detail": msg}, status_code=400) diff --git a/pyproject.toml b/pyproject.toml index 261ade2..2e98e34 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,7 @@ authors = ["Weiliang Li "] description = "" license = "MIT" name = "eciespy-demo" +package-mode = false version = "0.1.0" [tool.poetry.dependencies]