feat: sum

This commit is contained in:
superobk
2024-03-20 10:39:53 +08:00
parent f14c36d77a
commit e914aeee32
3 changed files with 27 additions and 0 deletions

23
src/blackbox/sum.py Normal file
View File

@ -0,0 +1,23 @@
from typing import Any, Coroutine
from fastapi import Request, Response, status
from fastapi.responses import JSONResponse
from .blackbox import Blackbox
class SUM(Blackbox):
def valid(self, data: any) -> bool:
return isinstance(data, list)
def processing(self, data: list) -> Coroutine[Any, Any, Any]:
return sum(data)
async def fast_api_handler(self, request: Request) -> Response:
try:
data = await request.json()
except:
return JSONResponse(content={"error": "json parse error"}, status_code=status.HTTP_400_BAD_REQUEST)
if not self.valid(data):
return JSONResponse(content={"error": "format error"}, status_code=status.HTTP_400_BAD_REQUEST)
return JSONResponse(content={"result": self.processing(data)}, status_code=status.HTTP_200_OK)