refactor: added async await to ONTAP call
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
# contains the router for the aggregates endpoint
|
# contains the router for the aggregates endpoint
|
||||||
from fastapi import APIRouter, Query
|
from fastapi import APIRouter, Query, Request
|
||||||
from typing import List
|
from typing import List
|
||||||
from .aggregate_schema import AggregateSchema, MetricEnum
|
from .aggregate_schema import AggregateSchema, MetricEnum
|
||||||
from .aggregate_service import get_aggregates
|
from .aggregate_service import get_aggregates
|
||||||
@@ -10,6 +10,7 @@ router = APIRouter(tags=["aggregates"])
|
|||||||
|
|
||||||
@router.get("/aggregates", response_model=List[AggregateSchema])
|
@router.get("/aggregates", response_model=List[AggregateSchema])
|
||||||
async def aggregates_endpoint(
|
async def aggregates_endpoint(
|
||||||
|
request: Request,
|
||||||
metric: MetricEnum = Query(MetricEnum.relative, description="Metric type"),
|
metric: MetricEnum = Query(MetricEnum.relative, description="Metric type"),
|
||||||
):
|
):
|
||||||
return await get_aggregates(metric)
|
return await get_aggregates(request, metric)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
# contains the business logic for aggregates
|
# contains the business logic for aggregates
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
from fastapi import Request
|
||||||
from .aggregate_schema import AggregateSchema, MetricEnum
|
from .aggregate_schema import AggregateSchema, MetricEnum
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from ..utils import round_bytes, get_data_from_ontap
|
from ..utils import round_bytes, get_data_from_ontap
|
||||||
@@ -9,13 +11,13 @@ logger = getLogger("uvicorn")
|
|||||||
logger.setLevel("DEBUG")
|
logger.setLevel("DEBUG")
|
||||||
|
|
||||||
|
|
||||||
async def get_aggregates(metric: str = "relative") -> List[AggregateSchema]:
|
async def get_aggregates(request: Request, metric: str = "relative") -> List[AggregateSchema]:
|
||||||
# Dummy data for demonstration
|
# Dummy data for demonstration
|
||||||
# You can use the metric parameter to filter or modify results as needed
|
# You can use the metric parameter to filter or modify results as needed
|
||||||
# For now, just return the same data and show metric usage
|
# For now, just return the same data and show metric usage
|
||||||
logger.debug(f"Metric used: {metric}")
|
logger.debug(f"Metric used: {metric}")
|
||||||
|
client = request.app.requests_client
|
||||||
__aggregates = get_data_from_ontap(logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node")
|
__aggregates = await get_data_from_ontap(client, logger, "172.16.57.2", "admin", "Netapp12", "storage/aggregates", "fields=name,uuid,space,node,home_node")
|
||||||
logger.debug(__aggregates)
|
logger.debug(__aggregates)
|
||||||
__aggregates = __aggregates.get("records")
|
__aggregates = __aggregates.get("records")
|
||||||
if metric == MetricEnum.relative:
|
if metric == MetricEnum.relative:
|
||||||
|
|||||||
18
src/main.py
18
src/main.py
@@ -1,14 +1,26 @@
|
|||||||
from src.service import load_config
|
|
||||||
from fastapi import FastAPI
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
import httpx
|
||||||
|
|
||||||
from src.aggregate import aggregate_router
|
from src.aggregate import aggregate_router
|
||||||
|
from src.service import load_config
|
||||||
|
|
||||||
logger = logging.getLogger("uvicorn")
|
logger = logging.getLogger("uvicorn")
|
||||||
|
|
||||||
logger.info("Starting application")
|
logger.info("Starting application")
|
||||||
config = load_config()
|
config = load_config()
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
app.requests_client = httpx.AsyncClient(verify=False)
|
||||||
|
yield
|
||||||
|
await app.requests_client.aclose()
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
app.include_router(aggregate_router)
|
app.include_router(aggregate_router)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
19
src/utils.py
19
src/utils.py
@@ -9,15 +9,16 @@ def round_bytes(size_in_bytes: int) -> str:
|
|||||||
return f"{size_in_bytes:.2f}EB"
|
return f"{size_in_bytes:.2f}EB"
|
||||||
|
|
||||||
|
|
||||||
def get_data_from_ontap(logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""):
|
async def get_data_from_ontap(client, logger, hostname: str, username: str, password: str, endpoint: str, query_string: str = ""):
|
||||||
url = f"https://{hostname}/api/{endpoint}"
|
url = f"https://{hostname}/api/{endpoint}"
|
||||||
if query_string:
|
if query_string:
|
||||||
url += f"?{query_string}"
|
url += f"?{query_string}"
|
||||||
try:
|
async with client as _client:
|
||||||
logger.debug(f"Fetching data from ONTAP: {url}")
|
try:
|
||||||
response = httpx.get(url, auth=(username, password), verify=False)
|
logger.debug(f"Fetching data from ONTAP: {url}")
|
||||||
response.raise_for_status()
|
response = await _client.get(url, auth=(username, password))
|
||||||
return response.json()
|
response.raise_for_status()
|
||||||
except httpx.HTTPError as e:
|
return response.json()
|
||||||
logger.error(f"HTTP error occurred: {e}")
|
except httpx.HTTPError as e:
|
||||||
return None
|
logger.error(f"HTTP error occurred: {e}")
|
||||||
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user