Skip to content

Commit

Permalink
refactor: use static duck typing for jobs and packages (#357)
Browse files Browse the repository at this point in the history
Co-authored-by: Barret Schloerke <[email protected]>
  • Loading branch information
tdstein and schloerke authored Dec 13, 2024
1 parent 7efac29 commit f709cc1
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 554 deletions.
4 changes: 3 additions & 1 deletion integration/tests/posit/connect/test_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TestEnvironments:
def setup_class(cls):
cls.client = connect.Client()
cls.environment = cls.client.environments.create(
title="title", name="name", cluster_name="Kubernetes"
title="title",
name="name",
cluster_name="Kubernetes",
)

@classmethod
Expand Down
4 changes: 4 additions & 0 deletions integration/tests/posit/connect/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ def test_find_by(self):

jobs = content.jobs
assert len(jobs) != 0

job = jobs[0]
key = job["key"]
assert content.jobs.find_by(key=key) == job
10 changes: 5 additions & 5 deletions src/posit/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
from .groups import Groups
from .metrics import Metrics
from .oauth import OAuth
from .packages import Packages
from .resources import ResourceParameters, _ResourceSequence
from .resources import ResourceParameters, _PaginatedResourceSequence, _ResourceSequence
from .tags import Tags
from .tasks import Tasks
from .users import User, Users
from .vanities import Vanities

if TYPE_CHECKING:
from .environments import Environments
from .packages import _Packages


class Client(ContextManager):
Expand Down Expand Up @@ -297,9 +297,9 @@ def oauth(self) -> OAuth:
return OAuth(self.resource_params, self.cfg.api_key)

@property
@requires(version="2024.10.0-dev")
def packages(self) -> Packages:
return Packages(self._ctx, "v1/packages")
@requires(version="2024.11.0")
def packages(self) -> _Packages:
return _PaginatedResourceSequence(self._ctx, "v1/packages", uid="name")

@property
def vanities(self) -> Vanities:
Expand Down
20 changes: 16 additions & 4 deletions src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
from . import tasks
from ._api import ApiDictEndpoint, JsonifiableDict
from .bundles import Bundles
from .context import requires
from .env import EnvVars
from .errors import ClientError
from .jobs import JobsMixin
from .oauth.associations import ContentItemAssociations
from .packages import ContentPackagesMixin as PackagesMixin
from .permissions import Permissions
from .resources import Resource, ResourceParameters, Resources
from .resources import Active, Resource, ResourceParameters, Resources, _ResourceSequence
from .tags import ContentItemTags
from .vanities import VanityMixin
from .variants import Variants

if TYPE_CHECKING:
from .context import Context
from .jobs import Jobs
from .packages import _ContentPackages
from .tasks import Task


Expand Down Expand Up @@ -174,7 +175,7 @@ class ContentItemOwner(Resource):
pass


class ContentItem(JobsMixin, PackagesMixin, VanityMixin, Resource):
class ContentItem(Active, VanityMixin, Resource):
class _AttrsBase(TypedDict, total=False):
# # `name` will be set by other _Attrs classes
# name: str
Expand Down Expand Up @@ -511,6 +512,17 @@ def tags(self) -> ContentItemTags:
content_guid=self["guid"],
)

@property
def jobs(self) -> Jobs:
path = posixpath.join(self._path, "jobs")
return _ResourceSequence(self._ctx, path, uid="key")

@property
@requires(version="2024.11.0")
def packages(self) -> _ContentPackages:
path = posixpath.join(self._path, "packages")
return _ResourceSequence(self._ctx, path, uid="name")


class Content(Resources):
"""Content resource.
Expand Down
11 changes: 10 additions & 1 deletion src/posit/connect/environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from abc import abstractmethod
from collections.abc import Mapping, Sized
from typing import (

from typing_extensions import (
Any,
List,
Literal,
Protocol,
SupportsIndex,
TypedDict,
overload,
runtime_checkable,
)

Expand Down Expand Up @@ -93,6 +96,12 @@ def update(

@runtime_checkable
class Environments(Sized, Protocol):
@overload
def __getitem__(self, index: SupportsIndex) -> Environment: ...

@overload
def __getitem__(self, index: slice) -> List[Environment]: ...

def create(
self,
*,
Expand Down
Loading

0 comments on commit f709cc1

Please sign in to comment.