Skip to content

Commit 194f07b

Browse files
committed
merge Active and ApiDict class into ActiveDict
1 parent 9bc8701 commit 194f07b

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

src/posit/connect/_active.py

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
from ._api_call import ApiCallMixin, get_api
2525
from ._json import Jsonifiable, JsonifiableDict, ResponseAttrs
26-
from .resources import Resource, ResourceParameters
2726

2827
if TYPE_CHECKING:
2928
from .context import Context
@@ -106,28 +105,59 @@ def items(self):
106105
return self._attrs.items()
107106

108107

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.
112127
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.
114129
115130
Parameters
116131
----------
117132
ctx : Context
118133
The context object containing the session and URL for API interactions.
119134
path : str
120135
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
122141
Resource attributes passed
123142
"""
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)
126156
self._ctx = ctx
127157
self._path = path
128158

129159

130-
T = TypeVar("T", bound="Active")
160+
T = TypeVar("T", bound="ActiveDict")
131161
"""A type variable that is bound to the `Active` class"""
132162

133163

@@ -275,58 +305,6 @@ def find_by(self, **conditions: Any) -> T | None:
275305
return next((v for v in collection if v.items() >= conditions.items()), None)
276306

277307

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-
330308
ReadOnlyDictT = TypeVar("ReadOnlyDictT", bound="ReadOnlyDict")
331309
"""A type variable that is bound to the `Active` class"""
332310

0 commit comments

Comments
 (0)