-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_operations.py
More file actions
101 lines (75 loc) · 3.13 KB
/
Copy pathabstract_operations.py
File metadata and controls
101 lines (75 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Abstract base classes and protocols for operations."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
from ..models import BulkCreateError, BulkCreateResult
class AbstractOperations:
"""Abstract base class for shared logic between sync and async operations."""
def __init__(self, client: Any) -> None:
"""Initialize the operations class.
Args:
client: The HTTP client to use for API requests.
"""
self._client = client
self._user_id: int | None = None
def _prepare_params(self, **kwargs: Any) -> dict[str, Any]:
"""Prepare request parameters by removing None values.
Args:
**kwargs: The parameters to prepare.
Returns:
A dictionary with None values removed.
"""
return {k: v for k, v in kwargs.items() if v is not None}
def _validate_mutual_exclusion(
self, param1: Any | None, param2: Any | None, param1_name: str, param2_name: str
) -> None:
"""Validate that two parameters are mutually exclusive.
Args:
param1: The first parameter value.
param2: The second parameter value.
param1_name: The name of the first parameter.
param2_name: The name of the second parameter.
Raises:
ValueError: If both parameters are provided.
"""
if param1 is not None and param2 is not None:
raise ValueError(f"Cannot specify both {param1_name} and {param2_name}")
def _validate_bulk_item(
self, item_data: dict[str, Any], required_fields: list[str]
) -> None:
"""Validate that required fields are present in bulk item data.
Args:
item_data: The item data dictionary to validate.
required_fields: List of required field names.
Raises:
ValueError: If any required field is missing.
"""
for field in required_fields:
if item_data.get(field) is None:
raise ValueError(f"{field} is required")
def _process_bulk_sync[T](
self,
items: list[dict[str, Any]],
create_func: Callable[[dict[str, Any]], T],
required_fields: list[str],
) -> BulkCreateResult[T]:
"""Process bulk creation synchronously.
Args:
items: List of item data dictionaries.
create_func: Function to create a single item from data dict.
required_fields: List of required field names.
Returns:
BulkCreateResult with successful and failed items.
"""
successful: list[T] = []
failed: list[BulkCreateError] = []
for index, item_data in enumerate(items):
try:
self._validate_bulk_item(item_data, required_fields)
created = create_func(item_data)
successful.append(created)
except Exception as e:
failed.append(
BulkCreateError(index=index, input_data=item_data, error=str(e))
)
return BulkCreateResult(successful=successful, failed=failed)