-
Notifications
You must be signed in to change notification settings - Fork 0
🛠️ Refactor: Core - Extract PathGetOperation #722
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
Merged
fderuiter
merged 1 commit into
main
from
refactor-extract-pathgetoperation-7851411157722976817
Mar 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from .get import PathGetOperation | ||
| from .list import ListOperation | ||
| from .record_create import RecordCreateOperation | ||
|
|
||
| __all__ = ["ListOperation", "RecordCreateOperation"] | ||
| __all__ = ["ListOperation", "PathGetOperation", "RecordCreateOperation"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """ | ||
| Operation for executing get requests via direct path. | ||
|
|
||
| This module encapsulates the logic for fetching and parsing a single resource | ||
| from the API using its ID. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, Callable, Generic, TypeVar | ||
|
|
||
| from imednet.core.protocols import AsyncRequestorProtocol, RequestorProtocol | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class PathGetOperation(Generic[T]): | ||
| """ | ||
| Operation for executing get requests via direct path. | ||
|
|
||
| Encapsulates the logic for making the HTTP request, handling empty | ||
| responses (not found), and parsing the result. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| path: str, | ||
| parse_func: Callable[[Any], T], | ||
| not_found_func: Callable[[], None], | ||
| ) -> None: | ||
| """ | ||
| Initialize the path get operation. | ||
|
|
||
| Args: | ||
| path: The API endpoint path. | ||
| parse_func: A function to parse a raw JSON item into the model T. | ||
| not_found_func: A callback to raise the appropriate not found error. | ||
| """ | ||
| self.path = path | ||
| self.parse_func = parse_func | ||
| self.not_found_func = not_found_func | ||
|
|
||
| def _process_response(self, response: Any) -> T: | ||
| """Process the raw HTTP response.""" | ||
| data = response.json() | ||
| if not data: | ||
| self.not_found_func() | ||
| return self.parse_func(data) | ||
|
|
||
| def execute_sync(self, client: RequestorProtocol) -> T: | ||
| """ | ||
| Execute synchronous get request. | ||
|
|
||
| Args: | ||
| client: The synchronous HTTP client. | ||
|
|
||
| Returns: | ||
| The parsed item. | ||
| """ | ||
| response = client.get(self.path) | ||
| return self._process_response(response) | ||
|
|
||
| async def execute_async(self, client: AsyncRequestorProtocol) -> T: | ||
| """ | ||
| Execute asynchronous get request. | ||
|
|
||
| Args: | ||
| client: The asynchronous HTTP client. | ||
|
|
||
| Returns: | ||
| The parsed item. | ||
| """ | ||
| response = await client.get(self.path) | ||
| return self._process_response(response) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not_found_funcis typed asCallable[[], None]and_process_response()continues to callparse_func(data)after invoking it. This makes the operation’s contract ambiguous/unsafe if a caller provides a callback that returns normally (it would then attempt to parse an empty payload). Consider typingnot_found_funcasCallable[[], NoReturn](and/or explicitly stopping execution after calling it) so both type-checkers and runtime behavior enforce the intended “must raise” contract.