77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
import json
|
|
import logging
|
|
from aiohttp import web, ClientSession
|
|
from router import Router
|
|
|
|
|
|
async def handle_rpc_request(request: web.Request) -> web.Response:
|
|
router: Router = request.app['router']
|
|
logger = logging.getLogger(__name__)
|
|
|
|
try:
|
|
body = await request.json()
|
|
|
|
if not isinstance(body, dict):
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": body.get("id", 1) if isinstance(body, dict) else 1,
|
|
"error": {
|
|
"code": -32600,
|
|
"message": "Invalid Request"
|
|
}
|
|
}, status=400)
|
|
|
|
method = body.get("method")
|
|
params = body.get("params", [])
|
|
request_id = body.get("id", 1)
|
|
|
|
if not method:
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": request_id,
|
|
"error": {
|
|
"code": -32600,
|
|
"message": "Missing method"
|
|
}
|
|
}, status=400)
|
|
|
|
logger.info(f"Handling RPC request: {method}")
|
|
|
|
# Special logging for signature status requests
|
|
if method == "getSignatureStatuses":
|
|
signatures = params if isinstance(params, list) else []
|
|
logger.info(f"GET_SIGNATURE_STATUSES: Checking signatures: {signatures}")
|
|
|
|
response = await router.route_request(method, params)
|
|
|
|
if method == "getSignatureStatuses":
|
|
logger.info(f"GET_SIGNATURE_STATUSES: Response: {response}")
|
|
response["id"] = request_id
|
|
|
|
return web.json_response(response)
|
|
|
|
except json.JSONDecodeError:
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"error": {
|
|
"code": -32700,
|
|
"message": "Parse error"
|
|
}
|
|
}, status=400)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Unexpected error: {e}")
|
|
return web.json_response({
|
|
"jsonrpc": "2.0",
|
|
"id": 1,
|
|
"error": {
|
|
"code": -32603,
|
|
"message": "Internal error"
|
|
}
|
|
}, status=500)
|
|
|
|
|
|
def setup_routes(app: web.Application) -> None:
|
|
app.router.add_post('/', handle_rpc_request)
|