Skip to content

Commit

Permalink
Add in ResourceDict class
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Nov 12, 2024
1 parent 1b1f20e commit 69bfa82
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
39 changes: 35 additions & 4 deletions src/posit/connect/_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
overload,
)

from ._api_call import ApiCallMixin, get_api
from ._api_call import ApiCallMixin, ContextP, get_api
from ._json import Jsonifiable, JsonifiableDict, ResponseAttrs

if TYPE_CHECKING:
Expand Down Expand Up @@ -105,7 +105,39 @@ def items(self):
return self._dict.items()


class ActiveDict(ApiCallMixin, ReadOnlyDict):
class ResourceDict(ReadOnlyDict, ContextP):
"""An abstraction to contain the context and read-only information."""

_ctx: Context
"""The context object containing the session and URL for API interactions."""

def __init__(
self,
ctx: Context,
/,
**kwargs: Any,
) -> None:
"""
A dict abstraction for any HTTP endpoint that returns a singular resource.
Adds helper methods to interact with the API with reduced boilerplate.
Parameters
----------
ctx : Context
The context object containing the session and URL for API interactions.
path : str
The HTTP path component for the resource endpoint
**kwargs : Any
Values to be stored
"""
super().__init__(**kwargs)
self._ctx = ctx


class ActiveDict(ApiCallMixin, ResourceDict):
"""A dict abstraction for any HTTP endpoint that returns a singular resource."""

_ctx: Context
"""The context object containing the session and URL for API interactions."""
_path: str
Expand Down Expand Up @@ -152,8 +184,7 @@ def __init__(
init_attrs_dict.update(attrs)
attrs = init_attrs_dict

super().__init__(attrs)
self._ctx = ctx
super().__init__(ctx, **attrs)
self._path = path


Expand Down
15 changes: 14 additions & 1 deletion src/posit/connect/_api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@
from .context import Context


class ApiCallProtocol(Protocol):
# Just the same as `.context.py` ContextManager but with `._ctx` attribute, not `.ctx`
class ContextP(Protocol):
_ctx: Context


class ContextCls(ContextP):
"""Class that contains the client context."""

_ctx: Context

def __init__(self, ctx: Context):
self._ctx = ctx


class ApiCallProtocol(ContextP, Protocol):
_path: str

def _endpoint(self, *path) -> str: ...
Expand Down

0 comments on commit 69bfa82

Please sign in to comment.