Skip to content

Commit 5d3f2b2

Browse files
committed
--wip-- [skip ci]
1 parent 0e9cf02 commit 5d3f2b2

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

src/posit/connect/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from requests import Response, Session
88

9+
from posit.connect.vanity import Vanities
10+
911
from . import hooks, me
1012
from .auth import Auth
1113
from .config import Config
@@ -271,6 +273,10 @@ def oauth(self) -> OAuth:
271273
"""
272274
return OAuth(self.resource_params, self.cfg.api_key)
273275

276+
@property
277+
def vanities(self) -> Vanities:
278+
return Vanities(self.ctx)
279+
274280
def __del__(self):
275281
"""Close the session when the Client instance is deleted."""
276282
if hasattr(self, "session") and self.session is not None:

src/posit/connect/content.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
from posixpath import dirname
88
from typing import Any, List, Literal, Optional, overload
99

10-
from posit.connect.oauth.associations import ContentItemAssociations
11-
1210
from . import tasks
1311
from .bundles import Bundles
1412
from .env import EnvVars
13+
from .oauth.associations import ContentItemAssociations
1514
from .permissions import Permissions
1615
from .resources import Resource, ResourceParameters, Resources
1716
from .tasks import Task
17+
from .vanity import VanityContentMixin
1818
from .variants import Variants
1919

2020

src/posit/connect/context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
from typing import Optional, Protocol
33

4+
import requests
45
from packaging.version import Version
56

67

@@ -21,7 +22,7 @@ def wrapper(instance: ContextManager, *args, **kwargs):
2122

2223

2324
class Context(dict):
24-
def __init__(self, session, url):
25+
def __init__(self, session: requests.Session, url: str):
2526
self.session = session
2627
self.url = url
2728

src/posit/connect/vanity.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from typing import Callable, Optional
2+
3+
from .context import Context
4+
5+
AfterDestroyCallback = Callable[[], None]
6+
7+
8+
class Vanity(dict):
9+
def __init__(
10+
self, ctx: Context, *, after_destroy: AfterDestroyCallback = lambda: None, **kwargs
11+
):
12+
super().__init__(**kwargs)
13+
self._ctx = ctx
14+
self._after_destroy = after_destroy
15+
16+
def destroy(self):
17+
url = self._ctx.url + f"v1/content/{self['content_guid']}/vanity"
18+
self._ctx.session.delete(url)
19+
self._after_destroy()
20+
21+
22+
class Vanities:
23+
def __init__(self, ctx: Context) -> None:
24+
self._ctx = ctx
25+
26+
def all(self) -> list[Vanity]:
27+
url = self._ctx.url + f"v1/vanities"
28+
response = self._ctx.session.get(url)
29+
results = response.json()
30+
return [Vanity(self._ctx, **result) for result in results]
31+
32+
33+
class VanityContentMixin(dict):
34+
def __init__(self, ctx: Context, **kwargs):
35+
super().__init__(**kwargs)
36+
self._ctx = ctx
37+
self._vanity: Optional[Vanity] = None
38+
39+
@property
40+
def vanity(self) -> Vanity:
41+
if self._vanity is None:
42+
url = self._ctx.url + f"v1/content/{self['guid']}/vanity"
43+
response = self._ctx.session.get(url)
44+
vanity_data = response.json()
45+
# Set the after_destroy callback to reset _vanity to None when destroyed
46+
after_destroy = lambda: setattr(self, "_vanity", None)
47+
self._vanity = Vanity(self._ctx, after_destroy=after_destroy, **vanity_data)
48+
return self._vanity
49+
50+
@vanity.setter
51+
def vanity(self, value: dict):
52+
url = self._ctx.url + f"v1/content/{self['guid']}/vanity"
53+
self._ctx.session.put(url, json=value)
54+
# Refresh the vanity property to reflect the updated value
55+
self._vanity = self.vanity
56+
57+
@vanity.deleter
58+
def vanity(self):
59+
if self._vanity:
60+
self._vanity.destroy()

0 commit comments

Comments
 (0)