Skip to content

Commit d672892

Browse files
committed
Add functionality for excluded methods
1 parent b485360 commit d672892

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

toggl_python/repository.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@
1717
Workspace,
1818
WorkspaceUser,
1919
)
20-
from .exceptions import NotSupported
20+
from .exceptions import MethodNotAllowed, NotSupported
2121

2222

2323
class BaseRepository(Api):
2424
LIST_URL = ""
2525
DETAIL_URL = None
2626
ENTITY_CLASS = BaseEntity
2727
ADDITIONAL_METHODS = {}
28+
EXCLUDED_METHODS = ()
2829

2930
def __init__(self, base_url=None, auth=None):
3031
super().__init__(base_url=base_url, auth=auth)
3132
if not self.DETAIL_URL:
3233
self.DETAIL_URL = self.LIST_URL + "/{id}"
3334

3435
def __getattr__(self, method: str):
36+
if method in self.EXCLUDED_METHODS:
37+
raise MethodNotAllowed
3538
try:
3639
method = super().__getattr__(method)
3740
except AttributeError:
@@ -94,20 +97,28 @@ def _list(self, _url, entity_class, **kwargs):
9497
return [entity_class(**entity) for entity in response.json()]
9598

9699
def list(self, **kwargs):
100+
if "list" in self.EXCLUDED_METHODS:
101+
raise MethodNotAllowed
97102
full_url = self.BASE_URL.join(self.LIST_URL)
98103
return self._list(full_url, self.ENTITY_CLASS)
99104

100105
def create(self, entity: ENTITY_CLASS):
106+
if "create" in self.EXCLUDED_METHODS:
107+
raise MethodNotAllowed
101108
full_url = self.BASE_URL.join(self.LIST_URL)
102109
response = self.post(full_url, data=entity.dict())
103110
return self.ENTITY_CLASS(**response.json())
104111

105112
def update(self, entity: ENTITY_CLASS):
113+
if "update" in self.EXCLUDED_METHODS:
114+
raise MethodNotAllowed
106115
full_url = self.BASE_URL.join(self.DETAIL_URL.format(id=entity.id))
107116
response = self.put(full_url, data=entity.dict())
108117
return self.ENTITY_CLASS(**response.json())
109118

110119
def partial_update(self, entity: ENTITY_CLASS):
120+
if "partial_update" in self.EXCLUDED_METHODS:
121+
raise MethodNotAllowed
111122
full_url = self.BASE_URL.join(self.DETAIL_URL.format(id=entity.id))
112123
response = self.patch(full_url, data=entity.dict())
113124
return self.ENTITY_CLASS(**response.json())

0 commit comments

Comments
 (0)