|
| 1 | +from typing import Callable, List, Optional, TypedDict |
| 2 | + |
| 3 | +from typing_extensions import NotRequired, Required, Unpack |
| 4 | + |
| 5 | +from .errors import ClientError |
| 6 | +from .resources import Resource, ResourceParameters, Resources |
| 7 | + |
| 8 | + |
| 9 | +class Vanity(Resource): |
| 10 | + """A vanity resource. |
| 11 | +
|
| 12 | + Vanities maintain custom URL paths assigned to content. |
| 13 | +
|
| 14 | + Warnings |
| 15 | + -------- |
| 16 | + Vanity paths may only contain alphanumeric characters, hyphens, underscores, and slashes. |
| 17 | +
|
| 18 | + Vanities cannot have children. For example, if the vanity path "/finance/" exists, the vanity path "/finance/budget/" cannot. But, if "/finance" does not exist, both "/finance/budget/" and "/finance/report" are allowed. |
| 19 | +
|
| 20 | + The following vanities are reserved by Connect: |
| 21 | + - `/__` |
| 22 | + - `/favicon.ico` |
| 23 | + - `/connect` |
| 24 | + - `/apps` |
| 25 | + - `/users` |
| 26 | + - `/groups` |
| 27 | + - `/setpassword` |
| 28 | + - `/user-completion` |
| 29 | + - `/confirm` |
| 30 | + - `/recent` |
| 31 | + - `/reports` |
| 32 | + - `/plots` |
| 33 | + - `/unpublished` |
| 34 | + - `/settings` |
| 35 | + - `/metrics` |
| 36 | + - `/tokens` |
| 37 | + - `/help` |
| 38 | + - `/login` |
| 39 | + - `/welcome` |
| 40 | + - `/register` |
| 41 | + - `/resetpassword` |
| 42 | + - `/content` |
| 43 | + """ |
| 44 | + |
| 45 | + AfterDestroyCallback = Callable[[], None] |
| 46 | + |
| 47 | + class VanityAttributes(TypedDict): |
| 48 | + """Vanity attributes.""" |
| 49 | + |
| 50 | + path: Required[str] |
| 51 | + content_guid: Required[str] |
| 52 | + created_time: Required[str] |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + /, |
| 57 | + params: ResourceParameters, |
| 58 | + *, |
| 59 | + after_destroy: Optional[AfterDestroyCallback] = None, |
| 60 | + **kwargs: Unpack[VanityAttributes], |
| 61 | + ): |
| 62 | + """Initialize a Vanity. |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + params : ResourceParameters |
| 67 | + after_destroy : AfterDestroyCallback, optional |
| 68 | + Called after the Vanity is successfully destroyed, by default None |
| 69 | + """ |
| 70 | + super().__init__(params, **kwargs) |
| 71 | + self._after_destroy = after_destroy |
| 72 | + self._content_guid = kwargs["content_guid"] |
| 73 | + |
| 74 | + @property |
| 75 | + def _endpoint(self): |
| 76 | + return self.params.url + f"v1/content/{self._content_guid}/vanity" |
| 77 | + |
| 78 | + def destroy(self) -> None: |
| 79 | + """Destroy the vanity. |
| 80 | +
|
| 81 | + Raises |
| 82 | + ------ |
| 83 | + ValueError |
| 84 | + If the foreign unique identifier is missing or its value is `None`. |
| 85 | +
|
| 86 | + Warnings |
| 87 | + -------- |
| 88 | + This operation is irreversible. |
| 89 | +
|
| 90 | + Note |
| 91 | + ---- |
| 92 | + This action requires administrator privileges. |
| 93 | + """ |
| 94 | + self.params.session.delete(self._endpoint) |
| 95 | + |
| 96 | + if self._after_destroy: |
| 97 | + self._after_destroy() |
| 98 | + |
| 99 | + |
| 100 | +class Vanities(Resources): |
| 101 | + """Manages a collection of vanities.""" |
| 102 | + |
| 103 | + def all(self) -> List[Vanity]: |
| 104 | + """Retrieve all vanities. |
| 105 | +
|
| 106 | + Returns |
| 107 | + ------- |
| 108 | + List[Vanity] |
| 109 | +
|
| 110 | + Notes |
| 111 | + ----- |
| 112 | + This action requires administrator privileges. |
| 113 | + """ |
| 114 | + endpoint = self.params.url + "v1/vanities" |
| 115 | + response = self.params.session.get(endpoint) |
| 116 | + results = response.json() |
| 117 | + return [Vanity(self.params, **result) for result in results] |
| 118 | + |
| 119 | + |
| 120 | +class VanityMixin(Resource): |
| 121 | + """Mixin class to add a vanity attribute to a resource.""" |
| 122 | + |
| 123 | + class HasGuid(TypedDict): |
| 124 | + """Has a guid.""" |
| 125 | + |
| 126 | + guid: Required[str] |
| 127 | + |
| 128 | + def __init__(self, params: ResourceParameters, **kwargs: Unpack[HasGuid]): |
| 129 | + super().__init__(params, **kwargs) |
| 130 | + self._content_guid = kwargs["guid"] |
| 131 | + self._vanity: Optional[Vanity] = None |
| 132 | + |
| 133 | + @property |
| 134 | + def _endpoint(self): |
| 135 | + return self.params.url + f"v1/content/{self._content_guid}/vanity" |
| 136 | + |
| 137 | + @property |
| 138 | + def vanity(self) -> Optional[str]: |
| 139 | + """Get the vanity.""" |
| 140 | + if self._vanity: |
| 141 | + return self._vanity["path"] |
| 142 | + |
| 143 | + try: |
| 144 | + self._vanity = self.find_vanity() |
| 145 | + self._vanity._after_destroy = self.reset_vanity |
| 146 | + return self._vanity["path"] |
| 147 | + except ClientError as e: |
| 148 | + if e.http_status == 404: |
| 149 | + return None |
| 150 | + raise e |
| 151 | + |
| 152 | + @vanity.setter |
| 153 | + def vanity(self, value: str) -> None: |
| 154 | + """Set the vanity. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + value : str |
| 159 | + The vanity path. |
| 160 | +
|
| 161 | + Note |
| 162 | + ---- |
| 163 | + This action requires owner or administrator privileges. |
| 164 | +
|
| 165 | + See Also |
| 166 | + -------- |
| 167 | + create_vanity |
| 168 | + """ |
| 169 | + self._vanity = self.create_vanity(path=value) |
| 170 | + self._vanity._after_destroy = self.reset_vanity |
| 171 | + |
| 172 | + @vanity.deleter |
| 173 | + def vanity(self) -> None: |
| 174 | + """Destroy the vanity. |
| 175 | +
|
| 176 | + Warnings |
| 177 | + -------- |
| 178 | + This operation is irreversible. |
| 179 | +
|
| 180 | + Note |
| 181 | + ---- |
| 182 | + This action requires owner or administrator privileges. |
| 183 | +
|
| 184 | + See Also |
| 185 | + -------- |
| 186 | + reset_vanity |
| 187 | + """ |
| 188 | + self.vanity |
| 189 | + if self._vanity: |
| 190 | + self._vanity.destroy() |
| 191 | + self.reset_vanity() |
| 192 | + |
| 193 | + def reset_vanity(self) -> None: |
| 194 | + """Unload the cached vanity. |
| 195 | +
|
| 196 | + Forces the next access, if any, to query the vanity from the Connect server. |
| 197 | + """ |
| 198 | + self._vanity = None |
| 199 | + |
| 200 | + class CreateVanityRequest(TypedDict, total=False): |
| 201 | + """A request schema for creating a vanity.""" |
| 202 | + |
| 203 | + path: Required[str] |
| 204 | + """The vanity path (.e.g, 'my-dashboard')""" |
| 205 | + |
| 206 | + force: NotRequired[bool] |
| 207 | + """Whether to force creation of the vanity""" |
| 208 | + |
| 209 | + def create_vanity(self, **kwargs: Unpack[CreateVanityRequest]) -> Vanity: |
| 210 | + """Create a vanity. |
| 211 | +
|
| 212 | + Parameters |
| 213 | + ---------- |
| 214 | + path : str, required |
| 215 | + The path for the vanity. |
| 216 | + force : bool, not required |
| 217 | + Whether to force the creation of the vanity. When True, any other vanity with the same path will be deleted. |
| 218 | +
|
| 219 | + Warnings |
| 220 | + -------- |
| 221 | + If setting force=True, the destroy operation performed on the other vanity is irreversible. |
| 222 | + """ |
| 223 | + response = self.params.session.put(self._endpoint, json=kwargs) |
| 224 | + result = response.json() |
| 225 | + return Vanity(self.params, **result) |
| 226 | + |
| 227 | + def find_vanity(self) -> Vanity: |
| 228 | + """Find the vanity. |
| 229 | +
|
| 230 | + Returns |
| 231 | + ------- |
| 232 | + Vanity |
| 233 | + """ |
| 234 | + response = self.params.session.get(self._endpoint) |
| 235 | + result = response.json() |
| 236 | + return Vanity(self.params, **result) |
0 commit comments