|
17 | 17 | Workspace, |
18 | 18 | WorkspaceUser, |
19 | 19 | ) |
20 | | -from .exceptions import NotSupported |
| 20 | +from .exceptions import MethodNotAllowed, NotSupported |
21 | 21 |
|
22 | 22 |
|
23 | 23 | class BaseRepository(Api): |
24 | 24 | LIST_URL = "" |
25 | 25 | DETAIL_URL = None |
26 | 26 | ENTITY_CLASS = BaseEntity |
27 | 27 | ADDITIONAL_METHODS = {} |
| 28 | + EXCLUDED_METHODS = () |
28 | 29 |
|
29 | 30 | def __init__(self, base_url=None, auth=None): |
30 | 31 | super().__init__(base_url=base_url, auth=auth) |
31 | 32 | if not self.DETAIL_URL: |
32 | 33 | self.DETAIL_URL = self.LIST_URL + "/{id}" |
33 | 34 |
|
34 | 35 | def __getattr__(self, method: str): |
| 36 | + if method in self.EXCLUDED_METHODS: |
| 37 | + raise MethodNotAllowed |
35 | 38 | try: |
36 | 39 | method = super().__getattr__(method) |
37 | 40 | except AttributeError: |
@@ -94,20 +97,28 @@ def _list(self, _url, entity_class, **kwargs): |
94 | 97 | return [entity_class(**entity) for entity in response.json()] |
95 | 98 |
|
96 | 99 | def list(self, **kwargs): |
| 100 | + if "list" in self.EXCLUDED_METHODS: |
| 101 | + raise MethodNotAllowed |
97 | 102 | full_url = self.BASE_URL.join(self.LIST_URL) |
98 | 103 | return self._list(full_url, self.ENTITY_CLASS) |
99 | 104 |
|
100 | 105 | def create(self, entity: ENTITY_CLASS): |
| 106 | + if "create" in self.EXCLUDED_METHODS: |
| 107 | + raise MethodNotAllowed |
101 | 108 | full_url = self.BASE_URL.join(self.LIST_URL) |
102 | 109 | response = self.post(full_url, data=entity.dict()) |
103 | 110 | return self.ENTITY_CLASS(**response.json()) |
104 | 111 |
|
105 | 112 | def update(self, entity: ENTITY_CLASS): |
| 113 | + if "update" in self.EXCLUDED_METHODS: |
| 114 | + raise MethodNotAllowed |
106 | 115 | full_url = self.BASE_URL.join(self.DETAIL_URL.format(id=entity.id)) |
107 | 116 | response = self.put(full_url, data=entity.dict()) |
108 | 117 | return self.ENTITY_CLASS(**response.json()) |
109 | 118 |
|
110 | 119 | def partial_update(self, entity: ENTITY_CLASS): |
| 120 | + if "partial_update" in self.EXCLUDED_METHODS: |
| 121 | + raise MethodNotAllowed |
111 | 122 | full_url = self.BASE_URL.join(self.DETAIL_URL.format(id=entity.id)) |
112 | 123 | response = self.patch(full_url, data=entity.dict()) |
113 | 124 | return self.ENTITY_CLASS(**response.json()) |
|
0 commit comments