cleanup
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import logging
|
||||
from typing import Optional
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi.responses import JSONResponse
|
||||
from aiohttp import ClientSession, ClientTimeout
|
||||
from .constructors import ValidXCRequest
|
||||
# pylint: disable=invalid-name
|
||||
@@ -11,7 +11,7 @@ from .constructors import ValidXCRequest
|
||||
class XC(FastAPI):
|
||||
"""XC (CrossComm) Endpoints"""
|
||||
def __init__(self, app: FastAPI, util, constants) -> None: # pylint: disable=super-init-not-called
|
||||
self.app = app
|
||||
self.app: FastAPI = app
|
||||
self.util = util
|
||||
self.constants = constants
|
||||
|
||||
@@ -23,12 +23,13 @@ class XC(FastAPI):
|
||||
app.add_api_route(f"/{endpoint}", handler, methods=["POST"],
|
||||
include_in_schema=False)
|
||||
|
||||
async def xc_handler(self, data: ValidXCRequest, request: Request) -> dict:
|
||||
async def xc_handler(self, data: ValidXCRequest,
|
||||
request: Request) -> JSONResponse:
|
||||
"""Handle XC Commands"""
|
||||
|
||||
try:
|
||||
key: str = data.key
|
||||
bid: int = data.bid
|
||||
bid: int = int(data.bid)
|
||||
cmd: str = data.cmd
|
||||
cmd_data: Optional[dict] = data.data
|
||||
if not self.util.check_key(path=request.url.path, req_type=0, key=key):
|
||||
@@ -40,10 +41,10 @@ class XC(FastAPI):
|
||||
}
|
||||
|
||||
if not bid in BID_ADDR_MAP:
|
||||
return {
|
||||
return JSONResponse(status_code=500, content={
|
||||
'err': True,
|
||||
'errorText': 'Invalid bot id'
|
||||
}
|
||||
})
|
||||
|
||||
bot_api_url: str = f'http://{BID_ADDR_MAP[bid]}/'
|
||||
async with ClientSession() as session:
|
||||
@@ -51,13 +52,13 @@ class XC(FastAPI):
|
||||
'Content-Type': 'application/json; charset=utf-8'
|
||||
}, timeout=ClientTimeout(connect=5, sock_read=5)) as aiohttp_request:
|
||||
response: dict = await aiohttp_request.json()
|
||||
return {
|
||||
return JSONResponse(content={
|
||||
'success': True,
|
||||
'response': response
|
||||
}
|
||||
})
|
||||
except Exception as e:
|
||||
logging.debug("Error: %s", str(e))
|
||||
return {
|
||||
return JSONResponse(status_code=500, content={
|
||||
'err': True,
|
||||
'errorText': 'General error.',
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user