Skip to content

Commit 6d25a31

Browse files
committed
Init before day break
1 parent e8392e0 commit 6d25a31

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

src/posit/connect/system.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"""System Information."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING, List
6+
7+
from typing_extensions import TypedDict, Unpack
8+
9+
from .context import ContextManager
10+
from .resources import Active
11+
12+
if TYPE_CHECKING:
13+
from posit.connect.context import Context
14+
15+
16+
# from posit import connect
17+
# from posit.connect.system import System, SystemCache
18+
19+
# client = connect.Client()
20+
# system: System = client.system
21+
# caches: List[SystemCache]: system.caches.find()
22+
# for cache in caches:
23+
# task = cache.destroy(dry_run=True)
24+
25+
26+
class System(ContextManager):
27+
"""System information."""
28+
29+
def __init__(self, ctx: Context, path: str) -> None:
30+
super().__init__()
31+
self._ctx: Context = ctx
32+
# v1/system
33+
self._path: str = path
34+
35+
@property
36+
def caches(self) -> SystemCaches:
37+
"""
38+
List all system caches.
39+
40+
Returns
41+
-------
42+
SystemCaches
43+
Helper class for system caches.
44+
45+
Examples
46+
--------
47+
```python
48+
TODO-barret!
49+
# List all content runtime caches
50+
caches = system.caches.find()
51+
for cache in caches:
52+
task = cache.destroy(dry_run=True)
53+
```
54+
55+
"""
56+
path = self._path + "/caches"
57+
return SystemCaches(self._ctx, path)
58+
59+
60+
class SystemRuntimeCacheAttrs(TypedDict, total=False):
61+
language: str # "Python"
62+
"""The runtime language of the cache."""
63+
version: str # "3.8.12"
64+
"""The language version of the cache."""
65+
image_name: str # "Local"
66+
"""The name of the cache's execution environment."""
67+
68+
69+
class SystemRuntimeCacheDestroyed(SystemRuntimeCacheAttrs, total=False):
70+
task_id: str
71+
"""The identifier for the created eployment task. Always `null` for dry-run requests."""
72+
73+
74+
class SystemRuntimeCache(Active):
75+
def __init__(
76+
self,
77+
ctx: Context,
78+
path: str,
79+
/,
80+
**kwargs: Unpack[SystemRuntimeCacheAttrs],
81+
) -> None:
82+
super().__init__(ctx, path, **kwargs)
83+
84+
class _DestroyAttrs(TypedDict, total=False):
85+
dry_run: bool
86+
"""If `True`, the cache will not be destroyed, only the operation will be simulated."""
87+
88+
def destroy(self, **kwargs: Unpack[SystemRuntimeCache._DestroyAttrs]) -> None:
89+
"""
90+
Delete a content runtime package cache.
91+
92+
This action is only available to administrators.
93+
94+
Parameters
95+
----------
96+
dry_run : bool, optional
97+
If `True`, the cache will not be destroyed, only the operation will be simulated.
98+
99+
Examples
100+
--------
101+
```python
102+
TODO-barret!
103+
# Destroy the runtime cache
104+
task = runtime_cache.destroy(dry_run=True)
105+
```
106+
"""
107+
url = self._ctx.url + self._path
108+
data = dict(self)
109+
response = self._ctx.session.delete(url, json={**data, **kwargs})
110+
return response.json()
111+
112+
113+
class SystemCaches(ContextManager):
114+
"""System caches."""
115+
116+
def __init__(self, ctx: Context, path: str) -> None:
117+
super().__init__()
118+
self._ctx: Context = ctx
119+
# v1/system/caches
120+
self._path: str = path
121+
122+
@property
123+
def runtime(self) -> SystemRuntimeCaches:
124+
"""
125+
System runtime caches.
126+
127+
Returns
128+
-------
129+
SystemRuntimeCaches
130+
Helper class to manage system runtime caches.
131+
132+
Examples
133+
--------
134+
```python
135+
TODO-barret!
136+
# List all content runtime caches
137+
caches = system.caches.find()
138+
for cache in caches:
139+
task = cache.destroy(dry_run=True)
140+
```
141+
"""
142+
path = self._path + "/runtime"
143+
return SystemRuntimeCaches(self._ctx, path)
144+
145+
146+
class SystemRuntimeCaches(ContextManager):
147+
"""
148+
System runtime caches.
149+
150+
List all content runtime caches. These include packrat and Python
151+
environment caches.
152+
153+
This information is available only to administrators.
154+
"""
155+
156+
def __init__(self, ctx: Context, path: str) -> None:
157+
super().__init__()
158+
self._ctx: Context = ctx
159+
# v1/system/caches/runtime
160+
self._path: str = path
161+
162+
def find(self) -> List[SystemRuntimeCache]:
163+
"""
164+
List all content runtime caches.
165+
166+
List all content runtime caches. These include packrat and Python
167+
environment caches.
168+
169+
This information is available only to administrators.
170+
171+
Returns
172+
-------
173+
List[SystemRuntimeCache]
174+
List of all content runtime caches.
175+
176+
Examples
177+
--------
178+
```python
179+
TODO-barret!
180+
# List all content runtime caches
181+
runtime_caches = system.caches.runtime.find()
182+
for runtime_cache in runtime_caches:
183+
task = runtime_cache.destroy(dry_run=True)
184+
```
185+
"""
186+
url = self._ctx.url + self._path
187+
response = self._ctx.session.get(url)
188+
results = response.json()
189+
return [SystemRuntimeCache(**result) for result in results]
190+
191+
# TODO-barret overloads
192+
def destroy(cache: SystemRuntimeCache | None , / , kwargs)
193+
TODO-barret implementations

0 commit comments

Comments
 (0)