|
23 | 23 |
|
24 | 24 | from ._api_call import ApiCallMixin, get_api |
25 | 25 | from ._json import Jsonifiable, JsonifiableDict, ResponseAttrs |
26 | | -from .resources import Resource, ResourceParameters |
27 | 26 |
|
28 | 27 | if TYPE_CHECKING: |
29 | 28 | from .context import Context |
@@ -106,28 +105,59 @@ def items(self): |
106 | 105 | return self._attrs.items() |
107 | 106 |
|
108 | 107 |
|
109 | | -class Active(ABC, Resource): |
110 | | - def __init__(self, ctx: Context, path: str, /, **attributes): |
111 | | - """A dict abstraction for any HTTP endpoint that returns a singular resource. |
| 108 | +class ActiveDict(ApiCallMixin, ReadOnlyDict): |
| 109 | + _ctx: Context |
| 110 | + """The context object containing the session and URL for API interactions.""" |
| 111 | + _path: str |
| 112 | + """The HTTP path component for the resource endpoint.""" |
| 113 | + |
| 114 | + def _get_api(self, *path) -> JsonifiableDict | None: |
| 115 | + super()._get_api(*path) |
| 116 | + |
| 117 | + def __init__( |
| 118 | + self, |
| 119 | + ctx: Context, |
| 120 | + path: str, |
| 121 | + get_data: Optional[bool] = None, |
| 122 | + /, |
| 123 | + **attrs: Jsonifiable, |
| 124 | + ) -> None: |
| 125 | + """ |
| 126 | + A dict abstraction for any HTTP endpoint that returns a singular resource. |
112 | 127 |
|
113 | | - Extends the `Resource` class and provides additional functionality for via the session context and an optional parent resource. |
| 128 | + Adds helper methods to interact with the API with reduced boilerplate. |
114 | 129 |
|
115 | 130 | Parameters |
116 | 131 | ---------- |
117 | 132 | ctx : Context |
118 | 133 | The context object containing the session and URL for API interactions. |
119 | 134 | path : str |
120 | 135 | The HTTP path component for the resource endpoint |
121 | | - **attributes : dict |
| 136 | + get_data : Optional[bool] |
| 137 | + If `True`, fetch the API and set the attributes from the response. If `False`, only set |
| 138 | + the provided attributes. If `None` [default], fetch the API if no attributes are |
| 139 | + provided. |
| 140 | + attrs : dict |
122 | 141 | Resource attributes passed |
123 | 142 | """ |
124 | | - params = ResourceParameters(ctx.session, ctx.url) |
125 | | - super().__init__(params, **attributes) |
| 143 | + # If no attributes are provided, fetch the API and set the attributes from the response |
| 144 | + if get_data is None: |
| 145 | + get_data = len(attrs) == 0 |
| 146 | + |
| 147 | + # If we should get data, fetch the API and set the attributes from the response |
| 148 | + if get_data: |
| 149 | + init_attrs: Jsonifiable = get_api(ctx, path) |
| 150 | + init_attrs_dict = cast(ResponseAttrs, init_attrs) |
| 151 | + # Overwrite the initial attributes with `attrs`: e.g. {'key': value} | {'content_guid': '123'} |
| 152 | + init_attrs_dict.update(attrs) |
| 153 | + attrs = init_attrs_dict |
| 154 | + |
| 155 | + super().__init__(attrs) |
126 | 156 | self._ctx = ctx |
127 | 157 | self._path = path |
128 | 158 |
|
129 | 159 |
|
130 | | -T = TypeVar("T", bound="Active") |
| 160 | +T = TypeVar("T", bound="ActiveDict") |
131 | 161 | """A type variable that is bound to the `Active` class""" |
132 | 162 |
|
133 | 163 |
|
@@ -275,58 +305,6 @@ def find_by(self, **conditions: Any) -> T | None: |
275 | 305 | return next((v for v in collection if v.items() >= conditions.items()), None) |
276 | 306 |
|
277 | 307 |
|
278 | | -class ApiDictEndpoint(ApiCallMixin, ReadOnlyDict): |
279 | | - _ctx: Context |
280 | | - """The context object containing the session and URL for API interactions.""" |
281 | | - _path: str |
282 | | - """The HTTP path component for the resource endpoint.""" |
283 | | - |
284 | | - def _get_api(self, *path) -> JsonifiableDict | None: |
285 | | - super()._get_api(*path) |
286 | | - |
287 | | - def __init__( |
288 | | - self, |
289 | | - ctx: Context, |
290 | | - path: str, |
291 | | - get_data: Optional[bool] = None, |
292 | | - /, |
293 | | - **attrs: Jsonifiable, |
294 | | - ) -> None: |
295 | | - """ |
296 | | - A dict abstraction for any HTTP endpoint that returns a singular resource. |
297 | | -
|
298 | | - Adds helper methods to interact with the API with reduced boilerplate. |
299 | | -
|
300 | | - Parameters |
301 | | - ---------- |
302 | | - ctx : Context |
303 | | - The context object containing the session and URL for API interactions. |
304 | | - path : str |
305 | | - The HTTP path component for the resource endpoint |
306 | | - get_data : Optional[bool] |
307 | | - If `True`, fetch the API and set the attributes from the response. If `False`, only set |
308 | | - the provided attributes. If `None` [default], fetch the API if no attributes are |
309 | | - provided. |
310 | | - attrs : dict |
311 | | - Resource attributes passed |
312 | | - """ |
313 | | - # If no attributes are provided, fetch the API and set the attributes from the response |
314 | | - if get_data is None: |
315 | | - get_data = len(attrs) == 0 |
316 | | - |
317 | | - # If we should get data, fetch the API and set the attributes from the response |
318 | | - if get_data: |
319 | | - init_attrs: Jsonifiable = get_api(ctx, path) |
320 | | - init_attrs_dict = cast(ResponseAttrs, init_attrs) |
321 | | - # Overwrite the initial attributes with `attrs`: e.g. {'key': value} | {'content_guid': '123'} |
322 | | - init_attrs_dict.update(attrs) |
323 | | - attrs = init_attrs_dict |
324 | | - |
325 | | - super().__init__(attrs) |
326 | | - self._ctx = ctx |
327 | | - self._path = path |
328 | | - |
329 | | - |
330 | 308 | ReadOnlyDictT = TypeVar("ReadOnlyDictT", bound="ReadOnlyDict") |
331 | 309 | """A type variable that is bound to the `Active` class""" |
332 | 310 |
|
|
0 commit comments