Skip to content

Commit

Permalink
Fix class duck typing using inspiration from #364
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Dec 16, 2024
1 parent 666a186 commit 15f854a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 38 deletions.
31 changes: 16 additions & 15 deletions src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import posixpath
import time
from abc import abstractmethod
from collections.abc import Mapping
from posixpath import dirname

from typing_extensions import (
TYPE_CHECKING,
Any,
Hashable,
List,
Literal,
NotRequired,
Expand All @@ -29,14 +28,7 @@
from .errors import ClientError
from .oauth.associations import ContentItemAssociations
from .permissions import Permissions
from .resources import (
Active,
Resource,
Resources,
_Resource,
_ResourceSequence,
_ResourceUpdatePatchMixin,
)
from .resources import Active, Resource, Resources, _Resource, _ResourceSequence
from .tags import ContentItemTags
from .vanities import VanityMixin
from .variants import Variants
Expand All @@ -48,16 +40,25 @@
from .tasks import Task


# TODO-barret: Replace with Resource class from https://github.com/posit-dev/posit-sdk-py/pull/364/files#diff-94b7dc3c7d7d7c7b1a5f25e06c37df5fc53e1921cb10d41d4f04b18a715fae55R72
class ResourceP(Protocol):
def __getitem__(self, key: Hashable, /) -> Any: ...


def _assert_guid(guid: str):
assert isinstance(guid, str), "Expected 'guid' to be a string"
assert len(guid) > 0, "Expected 'guid' to be non-empty"


# ContentItem Repository uses a PATCH method, not a PUT for updating.
class _ContentItemRepositoryResource(_ResourceUpdatePatchMixin, _Resource): ...
class _ContentItemRepository(_Resource):
def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride]
response = self._ctx.client.patch(self._path, json=attributes)
result = response.json()
super().update(**result)


class ContentItemRepository(Mapping[str, Any]):
class ContentItemRepository(ResourceP):
"""
Content items GitHub repository information.
Expand Down Expand Up @@ -216,7 +217,7 @@ def oauth(self) -> ContentItemOAuth:
@property
def repository(self) -> ContentItemRepository | None:
try:
return _Resource(
return _ContentItemRepository(
self._ctx,
f"v1/content/{self['guid']}/repository",
)
Expand Down Expand Up @@ -256,10 +257,10 @@ def create_repository(self, /, **attributes) -> ContentItemRepository:
ContentItemRepository
"""
path = f"v1/content/{self['guid']}/repository"
response = self._ctx.session.put(path, json=attributes)
response = self._ctx.client.session.put(path, json=attributes)
result = response.json()

return _ContentItemRepositoryResource(
return _ContentItemRepository(
self._ctx,
path,
**result,
Expand Down
23 changes: 0 additions & 23 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ def __init__(self, ctx: Context, path: str, /, **attributes):
self._path = path


class _ResourceP(Protocol):
_ctx: Context
_path: str

def destroy(self) -> None: ...

def update(self, **attributes): ...


class _Resource(dict):
def __init__(self, ctx: Context, path: str, /, **attributes):
self._ctx = ctx
Expand All @@ -87,20 +78,6 @@ def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride
super().update(**result)


class _ResourceUpdatePatchMixin:
def update(self: _ResourceP, **attributes): # type: ignore[reportIncompatibleMethodOverride]
response = self._ctx.client.patch(self._path, json=attributes)
result = response.json()
super().update(**result)


class _ResourcePatch(_Resource):
def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride]
response = self._ctx.client.patch(self._path, json=attributes)
result = response.json()
super().update(**result)


class _ResourceSequence(Sequence):
def __init__(self, ctx: Context, path: str, *, uid: str = "guid"):
self._ctx = ctx
Expand Down

0 comments on commit 15f854a

Please sign in to comment.