From 1543f8d7c51eb1f160669ef970c0212c62df8666 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Fri, 17 Dec 2021 20:17:52 -0700 Subject: [PATCH] restructure api routes/tasks --- hyperglass/api/routes.py | 38 +------------------------------ hyperglass/api/tasks.py | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/hyperglass/api/routes.py b/hyperglass/api/routes.py index 4320509..f1bb877 100644 --- a/hyperglass/api/routes.py +++ b/hyperglass/api/routes.py @@ -13,8 +13,6 @@ from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html # Project from hyperglass.log import log from hyperglass.state import HyperglassState, use_state -from hyperglass.external import Webhook, bgptools -from hyperglass.api.tasks import process_headers from hyperglass.constants import __version__ from hyperglass.models.ui import UIParameters from hyperglass.exceptions import HyperglassError @@ -26,6 +24,7 @@ from hyperglass.models.config.params import Params from hyperglass.models.config.devices import Devices # Local +from .tasks import send_webhook from .fake_output import fake_output @@ -49,41 +48,6 @@ def get_ui_params(): return use_state("ui_params") -async def send_webhook( - query_data: Query, - request: Request, - timestamp: datetime, -): - """If webhooks are enabled, get request info and send a webhook.""" - params = use_state("params") - try: - if params.logging.http is not None: - headers = await process_headers(headers=request.headers) - - if headers.get("x-real-ip") is not None: - host = headers["x-real-ip"] - elif headers.get("x-forwarded-for") is not None: - host = headers["x-forwarded-for"] - else: - host = request.client.host - - network_info = await bgptools.network_info(host) - - async with Webhook(params.logging.http) as hook: - - await hook.send( - query={ - **query_data.dict(), - "headers": headers, - "source": host, - "network": network_info.get(host, {}), - "timestamp": timestamp, - } - ) - except Exception as err: - log.error("Error sending webhook to {}: {}", params.logging.http.provider, str(err)) - - async def query( query_data: Query, request: Request, diff --git a/hyperglass/api/tasks.py b/hyperglass/api/tasks.py index 3f60778..b9d93fd 100644 --- a/hyperglass/api/tasks.py +++ b/hyperglass/api/tasks.py @@ -3,9 +3,23 @@ # Standard Library from typing import Dict, Union from pathlib import Path +from datetime import datetime # Third Party from httpx import Headers +from starlette.requests import Request + +# Project +from hyperglass.log import log +from hyperglass.state import use_state +from hyperglass.external import Webhook, bgptools +from hyperglass.models.api import Query + +__all__ = ( + "import_public_key", + "process_headers", + "send_webhook", +) def import_public_key(app_path: Union[Path, str], device_name: str, keystring: str) -> bool: @@ -47,3 +61,38 @@ async def process_headers(headers: Headers) -> Dict: "x-forwarded-for", ) return {k: headers.get(k) for k in header_keys} + + +async def send_webhook( + query_data: Query, + request: Request, + timestamp: datetime, +): + """If webhooks are enabled, get request info and send a webhook.""" + params = use_state("params") + try: + if params.logging.http is not None: + headers = await process_headers(headers=request.headers) + + if headers.get("x-real-ip") is not None: + host = headers["x-real-ip"] + elif headers.get("x-forwarded-for") is not None: + host = headers["x-forwarded-for"] + else: + host = request.client.host + + network_info = await bgptools.network_info(host) + + async with Webhook(params.logging.http) as hook: + + await hook.send( + query={ + **query_data.dict(), + "headers": headers, + "source": host, + "network": network_info.get(host, {}), + "timestamp": timestamp, + } + ) + except Exception as err: + log.error("Error sending webhook to {}: {}", params.logging.http.provider, str(err))