Skip to content

Commit

Permalink
feat: adds base class definition for resources (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein authored Mar 13, 2024
1 parent d4b68d8 commit 6657186
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from . import urls

from .config import Config
from .resources import Resources


class ContentItem(TypedDict, total=False):
# TODO: specify types
pass


class Content:
class Content(Resources[ContentItem]):
def __init__(self, config: Config, session: Session) -> None:
self.url = urls.append_path(config.url, "v1/content")
self.config = config
Expand All @@ -40,3 +41,12 @@ def get(self, id: str) -> ContentItem:
url = urls.append_path(self.url, id)
response = self.session.get(url)
return ContentItem(**response.json())

def create(self) -> ContentItem:
raise NotImplementedError()

def update(self) -> ContentItem:
raise NotImplementedError()

def delete(self) -> None:
raise NotImplementedError()
31 changes: 31 additions & 0 deletions src/posit/connect/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from abc import ABC, abstractmethod
from typing import Generic, List, Optional, TypeVar


T = TypeVar("T")


class Resources(ABC, Generic[T]):
@abstractmethod
def create(self, *args, **kwargs) -> T:
raise NotImplementedError()

@abstractmethod
def delete(self, *args, **kwargs) -> None:
raise NotImplementedError()

@abstractmethod
def find(self, *args, **kwargs) -> List[T]:
raise NotImplementedError()

@abstractmethod
def find_one(self, *args, **kwargs) -> Optional[T]:
raise NotImplementedError()

@abstractmethod
def get(self, *args, **kwargs) -> T:
raise NotImplementedError()

@abstractmethod
def update(self, *args, **kwargs) -> T:
raise NotImplementedError()
13 changes: 12 additions & 1 deletion src/posit/connect/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

from requests import Session


from . import urls

from .config import Config
from .paginator import _MAX_PAGE_SIZE, Paginator
from .resources import Resources


class User(TypedDict, total=False):
Expand All @@ -25,7 +27,7 @@ class User(TypedDict, total=False):
locked: bool


class Users:
class Users(Resources[User]):
def __init__(self, config: Config, session: Session) -> None:
self.url = urls.append_path(config.url, "v1/users")
self.config = config
Expand Down Expand Up @@ -53,3 +55,12 @@ def get(self, id: str) -> User:
url = urls.append_path(self.url, id)
response = self.session.get(url)
return User(**response.json())

def create(self) -> User:
raise NotImplementedError()

def update(self) -> User:
raise NotImplementedError()

def delete(self) -> None:
raise NotImplementedError()
49 changes: 49 additions & 0 deletions tests/posit/connect/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest

from typing import Any, List, Optional

from posit.connect.resources import Resources


class TestResources(Resources[Any]):
def create(self) -> Any:
return super().create() # type: ignore [safe-super]

def delete(self) -> None:
return super().delete() # type: ignore [safe-super]

def find(self) -> List[Any]:
return super().find() # type: ignore [safe-super]

def find_one(self) -> Optional[Any]:
return super().find_one() # type: ignore [safe-super]

def get(self) -> Any:
return super().get() # type: ignore [safe-super]

def update(self) -> Any:
return super().update() # type: ignore [safe-super]

def test_create(self):
with pytest.raises(NotImplementedError):
self.create()

def test_delete(self):
with pytest.raises(NotImplementedError):
self.delete()

def test_find(self):
with pytest.raises(NotImplementedError):
self.find()

def test_find_one(self):
with pytest.raises(NotImplementedError):
self.find_one()

def test_get(self):
with pytest.raises(NotImplementedError):
self.get()

def test_update(self):
with pytest.raises(NotImplementedError):
self.update()

0 comments on commit 6657186

Please sign in to comment.