Skip to content

Commit 2fde9b2

Browse files
hf-kkleinKonstantin
and
Konstantin
authored
feat: add method to call PATCH Event/.../replay endpoint (#137)
* feat: add method to call PATCH`Event/.../replay` endpoint * pylint --------- Co-authored-by: Konstantin <[email protected]>
1 parent 6dd887f commit 2fde9b2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/bssclient/client/bssclient.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import logging
55
import uuid
66
from abc import ABC
7-
from typing import Awaitable, Optional
7+
from typing import Awaitable, Literal, Optional, TypeAlias
88

9-
from aiohttp import BasicAuth, ClientSession, ClientTimeout
9+
from aiohttp import BasicAuth, ClientResponseError, ClientSession, ClientTimeout
1010
from more_itertools import chunked
1111
from yarl import URL
1212

@@ -17,6 +17,8 @@
1717

1818
_logger = logging.getLogger(__name__)
1919

20+
DomainModelType: TypeAlias = Literal["Prozess", "Aufgabe", "Zeitlimit"]
21+
2022

2123
class BssClient(ABC):
2224
"""
@@ -154,6 +156,29 @@ async def get_all_ermittlungsauftraege(self, package_size: int = 100) -> list[Er
154156
_logger.info("Downloaded %i Ermittlungsautraege", len(result))
155157
return result
156158

159+
async def replay_event(self, model_type: DomainModelType, model_id: uuid.UUID, event_number: int) -> bool:
160+
"""calls the re-apply endpoint"""
161+
session = await self._get_session()
162+
request_url = (
163+
self._config.server_url
164+
/ "api"
165+
/ "Event"
166+
/ "replay"
167+
/ model_type
168+
/ str(model_id)
169+
/ str(event_number)
170+
/ "false" # is temporal
171+
)
172+
request_uuid = uuid.uuid4()
173+
_logger.debug("[%s] requesting %s", str(request_uuid), request_url)
174+
try:
175+
async with session.patch(request_url) as response:
176+
_logger.debug("[%s] response status: %s", str(request_uuid), response.status)
177+
return response.status == 200
178+
except ClientResponseError as cre:
179+
_logger.debug("[%s] response status: %s", str(request_uuid), cre.status)
180+
return False
181+
157182

158183
class BasicAuthBssClient(BssClient):
159184
"""BSS client with basic auth"""

unittests/test_ermittlungsauftraege.py

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import uuid
23
from datetime import datetime, timezone
34
from pathlib import Path
45
from unittest.mock import AsyncMock, Mock
@@ -122,3 +123,20 @@ async def test_get_stats_with_oauth(self, bss_client_with_oauth):
122123
# https://github.com/Hochfrequenz/bssclient.py/issues/25
123124
assert isinstance(actual, AufgabeStats)
124125
assert actual.stats["Ermittlungsauftrag"]["status"]["Beendet"] == 2692
126+
127+
@pytest.mark.parametrize(
128+
"status_code, expected",
129+
[
130+
pytest.param(200, True),
131+
pytest.param(400, False),
132+
pytest.param(404, False),
133+
],
134+
)
135+
async def test_replay_event(self, bss_client_with_basic_auth, status_code: int, expected: bool) -> None:
136+
client, bss_config = bss_client_with_basic_auth
137+
random_uuid = uuid.uuid4()
138+
with aioresponses() as mocked_bss:
139+
mocked_patch_url = f"{bss_config.server_url}api/Event/replay/Prozess/{random_uuid}/17/false"
140+
mocked_bss.patch(mocked_patch_url, status=status_code)
141+
actual = await client.replay_event("Prozess", random_uuid, 17)
142+
assert actual is expected

0 commit comments

Comments
 (0)