Skip to content

Commit

Permalink
DEV-19025: codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
alexreznikoff committed Feb 6, 2025
1 parent 36253a2 commit c0d0150
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/huntflow_base_metrics/web_frameworks/_middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abc

from dataclasses import dataclass
from typing import Optional, Set
from typing import Any, Optional, Set

from huntflow_base_metrics._context import METRIC_CONTEXT

Expand All @@ -18,7 +17,7 @@ class PrometheusMiddleware(abc.ABC):

@staticmethod
@abc.abstractmethod
def get_path_template(request) -> PathTemplate:
def get_path_template(request: Any) -> PathTemplate:
pass

@classmethod
Expand Down
12 changes: 5 additions & 7 deletions src/huntflow_base_metrics/web_frameworks/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import time

from http import HTTPStatus
from typing import Callable, Iterable, Optional

from aiohttp.web import Application, Request, Response, middleware

from huntflow_base_metrics.base import apply_labels
from huntflow_base_metrics.export import export_to_http_response
from huntflow_base_metrics.web_frameworks._middleware import PrometheusMiddleware, PathTemplate
from huntflow_base_metrics.web_frameworks._middleware import PathTemplate, PrometheusMiddleware
from huntflow_base_metrics.web_frameworks._request_metrics import (
EXCEPTIONS,
REQUESTS,
Expand Down Expand Up @@ -66,11 +65,10 @@ async def dispatch(cls, request: Request, handler: Callable) -> Response:
@staticmethod
def get_path_template(request: Request) -> PathTemplate:
match_info = request.match_info
value = match_info.route.resource.canonical if match_info else request.rel_url.path
return PathTemplate(
value=value,
is_handled=match_info is not None
)
value = request.rel_url.path
if match_info and match_info.route.resource:
value = match_info.route.resource.canonical
return PathTemplate(value=value, is_handled=match_info is not None)


def add_middleware(
Expand Down
3 changes: 1 addition & 2 deletions src/huntflow_base_metrics/web_frameworks/fastapi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time

from typing import Iterable, Optional

from fastapi import FastAPI
Expand All @@ -11,7 +10,7 @@

from huntflow_base_metrics.base import apply_labels
from huntflow_base_metrics.export import export_to_http_response
from huntflow_base_metrics.web_frameworks._middleware import PrometheusMiddleware, PathTemplate
from huntflow_base_metrics.web_frameworks._middleware import PathTemplate, PrometheusMiddleware
from huntflow_base_metrics.web_frameworks._request_metrics import (
EXCEPTIONS,
REQUESTS,
Expand Down
6 changes: 4 additions & 2 deletions src/huntflow_base_metrics/web_frameworks/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from huntflow_base_metrics.base import apply_labels
from huntflow_base_metrics.export import export_to_http_response
from huntflow_base_metrics.web_frameworks._middleware import PrometheusMiddleware, PathTemplate
from huntflow_base_metrics.web_frameworks._middleware import PathTemplate, PrometheusMiddleware
from huntflow_base_metrics.web_frameworks._request_metrics import (
EXCEPTIONS,
REQUESTS,
Expand Down Expand Up @@ -80,7 +80,9 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
status_code=str(status_code),
).inc()
apply_labels(
REQUESTS_IN_PROGRESS, method=method, path_template=path_template.value,
REQUESTS_IN_PROGRESS,
method=method,
path_template=path_template.value,
).dec()
exception_type = exception_context.get()
if exception_type:
Expand Down
24 changes: 9 additions & 15 deletions tests/test_web_frameworks/test_frameworks.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from contextlib import suppress
from enum import Enum
from inspect import iscoroutine
from typing import Dict, Optional, TYPE_CHECKING, Union, Sequence
from typing import Dict, Optional, Sequence, Union

import pytest

from aiohttp import ClientResponse as AiohttpResponse
from aiohttp.test_utils import TestClient as AiohttpTestClient
from httpx import Response as HttpxResponse
from prometheus_client.exposition import CONTENT_TYPE_LATEST

from huntflow_base_metrics.base import COMMON_LABELS_VALUES, REGISTRY
from tests.test_web_frameworks.aiohttp import aiohttp_app
from tests.test_web_frameworks.fastapi import fastapi_app
from tests.test_web_frameworks.litestar import litestar_app

if TYPE_CHECKING:
from aiohttp import ClientResponse
from aiohttp.test_utils import TestClient
from httpx import Response


class Framework(str, Enum):
aiohttp = "aiohttp"
Expand All @@ -34,7 +30,7 @@ class Framework(str, Enum):
@pytest.fixture(params=[Framework.fastapi, Framework.aiohttp, Framework.litestar])
async def create_app(request):
factory = factories[request.param]
aiohttp_client: Optional["TestClient"] = None
aiohttp_client: Optional[AiohttpTestClient] = None

async def test_client(
include_routes: Optional[Sequence[str]] = None,
Expand All @@ -55,21 +51,19 @@ async def test_client(


async def check_response(
response: Union["Response", "ClientResponse"],
resp: Union[HttpxResponse, AiohttpResponse],
expected_json: Optional[Dict] = None,
status: int = 200,
) -> None:
"""
There might be a httpx or aiohttp response with different behavior.
"""
if expected_json is not None:
json = response.json()
if iscoroutine(json):
json = await json

json = await resp.json() if isinstance(resp, AiohttpResponse) else resp.json()
assert json == expected_json

status_code = getattr(response, "status_code", None) or getattr(response, "status")
status_code = resp.status if isinstance(resp, AiohttpResponse) else resp.status_code

assert status_code == status


Expand Down

0 comments on commit c0d0150

Please sign in to comment.