Skip to content

Commit d390f45

Browse files
committed
refactor: correct method names and reduce fluff.
1 parent ab69342 commit d390f45

File tree

2 files changed

+24
-75
lines changed

2 files changed

+24
-75
lines changed

src/posit/connect/vanities.py

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Callable, List, Optional, Union, overload
1+
from typing import Callable, List, Optional, TypedDict, Union
22

3-
from posit.connect.errors import ClientError
3+
from typing_extensions import NotRequired, Required, Unpack
44

5+
from .errors import ClientError
56
from .resources import Resource, ResourceParameters, Resources
67

78
AfterDestroyCallback = Callable[[], None]
@@ -51,6 +52,7 @@ def __init__(
5152
/,
5253
params: ResourceParameters,
5354
*,
55+
content_guid: str,
5456
after_destroy: Optional[AfterDestroyCallback] = None,
5557
**kwargs,
5658
):
@@ -62,7 +64,8 @@ def __init__(
6264
after_destroy : AfterDestroyCallback, optional
6365
Called after the Vanity is successfully destroyed, by default None
6466
"""
65-
super().__init__(params, **kwargs)
67+
super().__init__(params, content_guid=content_guid, **kwargs)
68+
self._endpoint = self.params.url + f"v1/content/{content_guid}/vanity"
6669
self._after_destroy = after_destroy
6770

6871
def destroy(self) -> None:
@@ -81,11 +84,7 @@ def destroy(self) -> None:
8184
----
8285
This action requires administrator privileges.
8386
"""
84-
fuid = self.get("content_guid")
85-
if fuid is None:
86-
raise ValueError("Missing value for required field: 'content_guid'.")
87-
endpoint = self.params.url + f"v1/content/{fuid}/vanity"
88-
self.params.session.delete(endpoint)
87+
self.params.session.delete(self._endpoint)
8988

9089
if self._after_destroy:
9190
self._after_destroy()
@@ -142,7 +141,7 @@ def vanity(self) -> Optional[Vanity]:
142141
raise e
143142

144143
@vanity.setter
145-
def vanity(self, value: Union[str, dict]) -> None:
144+
def vanity(self, value: Union[str, "CreateVanityRequest"]) -> None:
146145
"""Set the vanity.
147146
148147
Parameters
@@ -151,9 +150,9 @@ def vanity(self, value: Union[str, dict]) -> None:
151150
The value can be a string or a dictionary. If provided as a string, it represents the vanity path. If provided as a dictionary, it contains key-value pairs with detailed information about the object.
152151
"""
153152
if isinstance(value, str):
154-
self.set_vanity(path=value)
153+
self.create_vanity(path=value)
155154
elif isinstance(value, dict):
156-
self.set_vanity(**value)
155+
self.create_vanity(**value)
157156
self.reset_vanity()
158157

159158
@vanity.deleter
@@ -183,71 +182,32 @@ def reset_vanity(self) -> None:
183182
"""
184183
self._vanity = None
185184

186-
@overload
187-
def set_vanity(self, *, path: str) -> None:
188-
"""Set the vanity.
189-
190-
Parameters
191-
----------
192-
path : str
193-
The vanity path.
194-
195-
Raises
196-
------
197-
ValueError
198-
If the unique identifier field is missing or the value is None.
199-
"""
200-
...
201-
202-
@overload
203-
def set_vanity(self, *, path: str, force: bool) -> None:
204-
"""Set the vanity.
185+
class CreateVanityRequest(TypedDict, total=False):
186+
"""A request schema for creating a vanity.
205187
206-
Parameters
188+
Attributes
207189
----------
208190
path : str
209-
The vanity path.
191+
The path for the vanity.
210192
force : bool
211-
If `True`, overwrite the ownership of this vanity to this resource, default `False`
212-
213-
Raises
214-
------
215-
ValueError
216-
If the unique identifier field is missing or the value is None.
193+
Whether to force the creation of the vanity.
217194
"""
218-
...
219-
220-
@overload
221-
def set_vanity(self, **attributes) -> None:
222-
"""Set the vanity.
223195

224-
Parameters
225-
----------
226-
**attributes : dict, optional
227-
Arbitrary attributes. All attributes are passed as the request body to POST 'v1/content/:guid/vanity'
228-
229-
Raises
230-
------
231-
ValueError
232-
If the unique identifier field is missing or the value is None.
233-
"""
234-
...
196+
path: Required[str]
197+
force: NotRequired[bool]
235198

236-
def set_vanity(self, **attributes) -> None:
237-
"""Set the vanity.
199+
def create_vanity(self, **kwargs: Unpack[CreateVanityRequest]) -> None:
200+
"""Create a vanity.
238201
239202
Parameters
240203
----------
241-
**attributes : dict, optional
242-
Arbitrary attributes. All attributes are passed as the request body to POST 'v1/content/:guid/vanity'
243-
244-
Raises
245-
------
246-
ValueError
247-
If the unique identifier field is missing or the value is None.
204+
path : str, required
205+
The path for the vanity.
206+
force : bool, not required
207+
Whether to force the creation of the vanity, default False
248208
"""
249209
v = self.get(self._uid)
250210
if v is None:
251211
raise ValueError(f"Missing value for required field: '{self._uid}'.")
252212
endpoint = self.params.url + f"v1/content/{v}/vanity"
253-
self.params.session.put(endpoint, json=attributes)
213+
self.params.session.put(endpoint, json=kwargs)

tests/posit/connect/test_vanities.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from unittest.mock import Mock
22

3-
import pytest
43
import requests
54
import responses
65
from responses.matchers import json_params_matcher
@@ -27,16 +26,6 @@ def test_destroy_sends_delete_request(self):
2726

2827
assert mock_delete.call_count == 1
2928

30-
def test_destroy_without_content_guid_raises_value_error(self):
31-
vanity = Vanity(params=Mock())
32-
with pytest.raises(ValueError):
33-
vanity.destroy()
34-
35-
def test_destroy_with_none_content_guid_raises_value_error(self):
36-
vanity = Vanity(params=Mock(), content_guid=None)
37-
with pytest.raises(ValueError):
38-
vanity.destroy()
39-
4029
@responses.activate
4130
def test_destroy_calls_after_destroy_callback(self):
4231
content_guid = "8ce6eaca-60af-4c2f-93a0-f5f3cddf5ee5"

0 commit comments

Comments
 (0)