Skip to content

Commit

Permalink
Remove getattr() usage in _request_wrapper(). (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Jul 1, 2024
1 parent 632bcca commit cbeb35e
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions mreg_cli/utilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import os
import re
import sys
from typing import Any, Literal, NoReturn, TypeVar, cast, get_origin, overload
from typing import Any, Literal, NoReturn, TypeVar, get_origin, overload
from urllib.parse import urljoin
from uuid import uuid4

Expand Down Expand Up @@ -225,7 +225,7 @@ def _strip_none(data: dict[str, Any]) -> dict[str, Any]:


def _request_wrapper(
operation_type: str,
operation_type: Literal["get", "post", "patch", "delete"],
path: str,
params: QueryParams | None = None,
ok404: bool = False,
Expand Down Expand Up @@ -253,13 +253,23 @@ def _request_wrapper(
if data:
data = _strip_none(data)

result = getattr(session, operation_type)(
if operation_type == "get":
func = session.get
elif operation_type == "post":
func = session.post
elif operation_type == "patch":
func = session.patch
elif operation_type == "delete":
func = session.delete
else:
raise ValueError(f"Unknown operation type: {operation_type}")

result = func(
url,
params=params,
json=data or None,
timeout=HTTP_TIMEOUT,
)
result = cast(requests.Response, result) # convince mypy that result is a Response

request_id = result.headers.get("X-Request-Id", "?")
correlation_id = result.headers.get("X-Correlation-ID", "?")
Expand All @@ -278,8 +288,7 @@ def _request_wrapper(
and params == {}
and data is not None
):
result = getattr(session, operation_type)(url, params={}, timeout=HTTP_TIMEOUT, data=data)
result = cast(requests.Response, result)
result = func(url, params={}, timeout=HTTP_TIMEOUT, data=data)

OutputManager().recording_request(operation_type, url, params, data, result)

Expand Down

0 comments on commit cbeb35e

Please sign in to comment.