From b1a33237fb9cdd4689b15974bba6ff6b4485c867 Mon Sep 17 00:00:00 2001 From: fderuiter <127706008+fderuiter@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:00:04 +0000 Subject: [PATCH] refactor: explicit sync/async calls in RecordsEndpoint - Removed `_create_impl` from `RecordsEndpoint` to eliminate boolean flag argument smell. - Updated `create` and `async_create` to call `_prepare_create_request` and the appropriate sync/async execution method directly. - Removed unused `_get_client` helper from `GenericEndpoint`. - Maintained strict typing and sync/async parity. - Verified with existing tests. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- imednet/core/endpoint/base.py | 14 --------- imednet/endpoints/records.py | 54 +++++++++++------------------------ 2 files changed, 17 insertions(+), 51 deletions(-) diff --git a/imednet/core/endpoint/base.py b/imednet/core/endpoint/base.py index dc829a45..d04abf9c 100644 --- a/imednet/core/endpoint/base.py +++ b/imednet/core/endpoint/base.py @@ -79,20 +79,6 @@ def _require_async_client(self) -> AsyncRequestorProtocol: raise RuntimeError("Async client not configured") return self._async_client - def _get_client(self, is_async: bool) -> RequestorProtocol | AsyncRequestorProtocol: - """ - Get the appropriate client for the execution context. - - Args: - is_async: Whether an async client is required. - - Returns: - The sync or async client instance. - """ - if is_async: - return self._require_async_client() - return self._require_sync_client() - class BaseEndpoint(EdcEndpointMixin, GenericEndpoint[T]): """ diff --git a/imednet/endpoints/records.py b/imednet/endpoints/records.py index d1c7c48e..0eda046a 100644 --- a/imednet/endpoints/records.py +++ b/imednet/endpoints/records.py @@ -1,10 +1,9 @@ """Endpoint for managing records (eCRF instances) in a study.""" -from typing import Any, Awaitable, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Optional, Union from imednet.constants import HEADER_EMAIL_NOTIFY from imednet.core.endpoint.mixins import CreateEndpointMixin, EdcListGetEndpoint -from imednet.core.protocols import AsyncRequestorProtocol, RequestorProtocol from imednet.models.jobs import Job from imednet.models.records import Record from imednet.validation.cache import SchemaCache, validate_record_data @@ -83,35 +82,6 @@ def _build_headers(self, email_notify: Union[bool, str, None]) -> Dict[str, str] headers[HEADER_EMAIL_NOTIFY] = str(email_notify).lower() return headers - def _create_impl( - self, - study_key: str, - records_data: List[Dict[str, Any]], - email_notify: Union[bool, str, None] = None, - *, - schema: Optional[SchemaCache] = None, - is_async: bool = False, - ) -> Job | Awaitable[Job]: - path, headers = self._prepare_create_request(study_key, records_data, email_notify, schema) - client = self._get_client(is_async) - - if is_async: - return self._create_async( - cast(AsyncRequestorProtocol, client), - path, - json=records_data, - headers=headers, - parse_func=Job.from_json, - ) - - return self._create_sync( - cast(RequestorProtocol, client), - path, - json=records_data, - headers=headers, - parse_func=Job.from_json, - ) - def create( self, study_key: str, @@ -137,9 +107,14 @@ def create( Raises: ValueError: If email_notify contains invalid characters """ - return cast( - Job, - self._create_impl(study_key, records_data, email_notify, schema=schema, is_async=False), + path, headers = self._prepare_create_request(study_key, records_data, email_notify, schema) + client = self._require_sync_client() + return self._create_sync( + client, + path, + json=records_data, + headers=headers, + parse_func=Job.from_json, ) async def async_create( @@ -169,7 +144,12 @@ async def async_create( Raises: ValueError: If email_notify contains invalid characters """ - return await cast( - Awaitable[Job], - self._create_impl(study_key, records_data, email_notify, schema=schema, is_async=True), + path, headers = self._prepare_create_request(study_key, records_data, email_notify, schema) + client = self._require_async_client() + return await self._create_async( + client, + path, + json=records_data, + headers=headers, + parse_func=Job.from_json, )