Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getattr() usage in _request_wrapper(). #274

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
terjekv marked this conversation as resolved.
Show resolved Hide resolved

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
Loading