Skip to content

Commit 999755b

Browse files
committed
--wip-- [skip ci]
1 parent 72b62ac commit 999755b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
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.environments import Environments
10+
911
from . import hooks, me
1012
from .auth import Auth
1113
from .config import Config
@@ -184,6 +186,10 @@ def me(self) -> User:
184186
"""
185187
return me.get(self.resource_params)
186188

189+
@property
190+
def environments(self) -> Environments:
191+
return Environments(self.ctx, "v1")
192+
187193
@property
188194
def groups(self) -> Groups:
189195
"""The groups resource interface.

src/posit/connect/environments.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from .resources import (
2+
ActiveCreatorMethods,
3+
ActiveDestroyerMethods,
4+
ActiveFinderMethods,
5+
ActiveUpdaterMethods,
6+
)
7+
8+
9+
class Environment(ActiveUpdaterMethods, ActiveDestroyerMethods):
10+
pass
11+
12+
13+
class Environments(ActiveFinderMethods[Environment], ActiveCreatorMethods[Environment]):
14+
def __init__(self, ctx, path, pathinfo="environments", uid="guid"):
15+
super().__init__(ctx, path, pathinfo, uid)
16+
17+
def _create_instance(self, path, pathinfo, /, **attributes):
18+
return Environment(self._ctx, path, pathinfo, **attributes)

src/posit/connect/resources.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,30 @@ def find_by(self, **conditions: Any) -> Optional[T]:
227227
The first record matching the conditions, or `None` if no match is found.
228228
"""
229229
return next((v for v in self._data if v.items() >= conditions.items()), None)
230+
231+
232+
class ActiveCreatorMethods(ActiveSequence[T], ABC):
233+
def create(self, **attributes) -> T:
234+
endpoint = self._ctx.url + self._path
235+
response = self._ctx.session.post(endpoint, json=attributes)
236+
result = response.json()
237+
uid = result[self._uid]
238+
return self._create_instance(self._path, uid, **result)
239+
240+
241+
class ActiveDestroyerMethods(Active):
242+
def destroy(self):
243+
endpoint = self._ctx.url + self._path
244+
self._ctx.session.delete(endpoint)
245+
246+
247+
class ActiveUpdaterMethods(Active):
248+
def update(self, /, **attributes):
249+
super().update(**attributes)
250+
self.save()
251+
252+
def save(self):
253+
endpoint = self._ctx.url + self._path
254+
response = self._ctx.session.put(endpoint, json=self)
255+
result = response.json()
256+
super().update(**result)

0 commit comments

Comments
 (0)