Skip to content

Commit d63d1a7

Browse files
Refactor ListEndpointMixin to use ListOperation
- Extracted list execution logic (iteration and parsing) into a new `ListOperation` class in `imednet/core/endpoint/operations/list.py`. - Updated `ListEndpointMixin` to use `ListOperation` via composition. - This adheres to SRP and prepares the codebase for better modularity and testing. - No public API changes. - Tests passed. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 7443612 commit d63d1a7

3 files changed

Lines changed: 62 additions & 2 deletions

File tree

imednet/core/endpoint/mixins/list.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from imednet.constants import DEFAULT_PAGE_SIZE
66
from imednet.core.endpoint.abc import EndpointABC
7+
from imednet.core.endpoint.operations.list import ListOperation
78
from imednet.core.endpoint.structs import ListRequestState
89
from imednet.core.paginator import AsyncPaginator, Paginator
910
from imednet.core.parsing import get_model_parser
@@ -73,7 +74,8 @@ async def _execute_async_list(
7374
has_filters: bool,
7475
cache: Any,
7576
) -> List[T]:
76-
result = [parse_func(item) async for item in paginator]
77+
operation = ListOperation[T]()
78+
result = await operation.execute_async(paginator, parse_func)
7779
return self._process_list_result(result, study, has_filters, cache)
7880

7981
def _execute_sync_list(
@@ -84,7 +86,8 @@ def _execute_sync_list(
8486
has_filters: bool,
8587
cache: Any,
8688
) -> List[T]:
87-
result = [parse_func(item) for item in paginator]
89+
operation = ListOperation[T]()
90+
result = operation.execute_sync(paginator, parse_func)
8891
return self._process_list_result(result, study, has_filters, cache)
8992

9093
def _prepare_list_request(

imednet/core/endpoint/operations/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Operation for listing resources.
3+
4+
Decouples the logic of iterating and parsing from the endpoint definition.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
from typing import Any, Callable, Generic, List, TypeVar
10+
11+
from imednet.core.paginator import AsyncPaginator, Paginator
12+
from imednet.models.json_base import JsonModel
13+
14+
T = TypeVar("T", bound=JsonModel)
15+
16+
17+
class ListOperation(Generic[T]):
18+
"""
19+
Encapsulates the logic for listing resources.
20+
21+
This class handles the iteration over paginated results and parsing of items.
22+
It is designed to be used via composition within endpoint implementations.
23+
"""
24+
25+
def execute_sync(
26+
self,
27+
paginator: Paginator,
28+
parse_func: Callable[[Any], T],
29+
) -> List[T]:
30+
"""
31+
Execute a synchronous list operation.
32+
33+
Args:
34+
paginator: The paginator instance to iterate over.
35+
parse_func: A function to parse each raw item into a model.
36+
37+
Returns:
38+
A list of parsed model instances.
39+
"""
40+
return [parse_func(item) for item in paginator]
41+
42+
async def execute_async(
43+
self,
44+
paginator: AsyncPaginator,
45+
parse_func: Callable[[Any], T],
46+
) -> List[T]:
47+
"""
48+
Execute an asynchronous list operation.
49+
50+
Args:
51+
paginator: The async paginator instance to iterate over.
52+
parse_func: A function to parse each raw item into a model.
53+
54+
Returns:
55+
A list of parsed model instances.
56+
"""
57+
return [parse_func(item) async for item in paginator]

0 commit comments

Comments
 (0)