Commit 5da834ab authored by Michael R's avatar Michael R Committed by Andrey Vertiprahov
Browse files

Switch service mib from tornado to FastAPI

parent c95b7fc7
# ----------------------------------------------------------------------
# Models for JSON-RPC API
# ----------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from typing import Any, List
# Third-party modules
from pydantic import BaseModel
class JSONRemoteProcedureCall(BaseModel):
method: str
params: List[Any]
id: int
# ----------------------------------------------------------------------
# mib JSON-RPC API endpoint
# ----------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
from fastapi import APIRouter
# NOC modules
from noc.core.debug import error_report
from noc.core.error import NOCError
from noc.core.service.loader import get_service
from noc.core.service.models.jsonrpc import JSONRemoteProcedureCall
from noc.services.mib.api.mib import MIBAPI
router = APIRouter()
@router.post("/api/mib/")
@router.post("/api/mib")
def api_mib(req: JSONRemoteProcedureCall):
if req.method not in MIBAPI.get_methods():
return {
"error": f"Invalid method: '{req.method}'",
"id": req.id
}
service = get_service()
api = MIBAPI(service, None, None)
api_method = getattr(api, req.method)
result = None
error = None
try:
result = api_method(*req.params)
except NOCError as e:
error = f"Failed: {e}"
except Exception as e:
error_report()
error = f"Failed: {e}"
return {
"result": result,
"error": error,
"id": req.id
}
......@@ -7,13 +7,12 @@
# ----------------------------------------------------------------------
# NOC modules
from noc.core.service.tornado import TornadoService
from noc.core.service.fastapi import FastAPIService
from noc.services.mib.api.mib import MIBAPI
class MIBService(TornadoService):
class MIBService(FastAPIService):
name = "mib"
api = [MIBAPI]
use_mongo = True
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment