-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlayers.py
More file actions
293 lines (250 loc) · 8.26 KB
/
layers.py
File metadata and controls
293 lines (250 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from __future__ import annotations
from typing import Any, List, TypedDict, cast
from typing_extensions import NotRequired, Optional
from .apps import EnvVar, EnvVarInput, EnvVarUpdate, LayerRef
from .bridge import AsyncBridge
from .console import (
AsyncConsoleClient,
AsyncPaginatedList,
PaginatedList,
)
from .utils import convert_to_snake_case
class Layer(TypedDict):
id: str
"""Unique layer identifier."""
slug: str
"""Human-readable layer slug."""
description: NotRequired[str]
"""Optional description of the layer's purpose."""
layers: List[LayerRef]
"""Base layers included by this layer, in priority order."""
env_vars: List[EnvVar]
"""Environment variables defined in this layer."""
app_count: int
"""Number of apps that reference this layer."""
created_at: str
"""ISO 8601 timestamp of creation."""
updated_at: str
"""ISO 8601 timestamp of last modification."""
class LayerAppRef(TypedDict):
id: str
"""Unique app identifier (UUID)."""
slug: str
"""Human-readable app slug."""
layer_position: int
"""Index of this layer in the app's layers array."""
class AsyncLayers:
def __init__(self, client: AsyncConsoleClient):
self._client = client
async def get(self, id_or_slug: str) -> Layer | None:
"""Get a layer by ID or slug.
Args:
id_or_slug: Layer ID or slug.
"""
result = await self._client.get_or_none(f"/api/v2/layers/{id_or_slug}")
if result is None:
return None
return cast(Layer, convert_to_snake_case(result))
async def list(
self,
*,
search: Optional[str] = None,
cursor: Optional[str] = None,
limit: Optional[int] = None,
) -> AsyncPaginatedList[Layer]:
"""List all layers in the organization.
Args:
search: Search query for filtering.
cursor: Pagination cursor.
limit: Maximum number of items to return.
"""
params: dict[str, Any] = {}
if search is not None:
params["search"] = search
if cursor is not None:
params["cursor"] = cursor
if limit is not None:
params["limit"] = limit
return await self._client.get_paginated(
"/api/v2/layers",
cursor=None,
params=params if params else None,
)
async def create(
self,
slug: str,
*,
description: Optional[str] = None,
layers: Optional[List[str]] = None,
env_vars: Optional[List[EnvVarInput]] = None,
) -> Layer:
"""Create a new layer.
Args:
slug: Human-readable layer slug.
description: Optional description of the layer's purpose.
layers: Base layer IDs or slugs to include.
env_vars: Environment variables for this layer.
"""
body: dict[str, Any] = {"slug": slug}
if description is not None:
body["description"] = description
if layers is not None:
body["layers"] = layers
if env_vars is not None:
body["env_vars"] = env_vars
result = await self._client.post("/api/v2/layers", body)
return cast(Layer, convert_to_snake_case(result))
async def update(
self,
layer: str,
*,
slug: Optional[str] = None,
description: Optional[str] = None,
layers: Optional[List[str]] = None,
env_vars: Optional[List[EnvVarUpdate]] = None,
) -> Layer:
"""Update a layer.
Args:
layer: Layer ID or slug.
slug: New layer slug.
description: New description.
layers: Replace all included layers.
env_vars: Deep merge with existing environment variables.
"""
body: dict[str, Any] = {}
if slug is not None:
body["slug"] = slug
if description is not None:
body["description"] = description
if layers is not None:
body["layers"] = layers
if env_vars is not None:
body["env_vars"] = env_vars
result = await self._client.patch(f"/api/v2/layers/{layer}", body)
return cast(Layer, convert_to_snake_case(result))
async def delete(self, layer: str) -> None:
"""Delete a layer.
Args:
layer: Layer ID or slug.
"""
await self._client.delete(f"/api/v2/layers/{layer}")
async def apps(
self,
layer: str,
*,
cursor: Optional[str] = None,
limit: Optional[int] = None,
) -> AsyncPaginatedList[LayerAppRef]:
"""List apps that reference this layer.
Args:
layer: Layer ID or slug.
cursor: Pagination cursor.
limit: Maximum number of items to return.
"""
params: dict[str, Any] = {}
if cursor is not None:
params["cursor"] = cursor
if limit is not None:
params["limit"] = limit
return await self._client.get_paginated(
f"/api/v2/layers/{layer}/apps",
cursor=None,
params=params if params else None,
)
class Layers:
def __init__(self, client: AsyncConsoleClient, bridge: AsyncBridge):
self._client = client
self._bridge = bridge
self._async = AsyncLayers(client)
def get(self, id_or_slug: str) -> Layer | None:
"""Get a layer by ID or slug.
Args:
id_or_slug: Layer ID or slug.
"""
return self._bridge.run(self._async.get(id_or_slug))
def list(
self,
*,
search: Optional[str] = None,
cursor: Optional[str] = None,
limit: Optional[int] = None,
) -> PaginatedList[Layer]:
"""List all layers in the organization.
Args:
search: Search query for filtering.
cursor: Pagination cursor.
limit: Maximum number of items to return.
"""
paginated = self._bridge.run(
self._async.list(search=search, cursor=cursor, limit=limit)
)
return PaginatedList(self._bridge, paginated)
def create(
self,
slug: str,
*,
description: Optional[str] = None,
layers: Optional[List[str]] = None,
env_vars: Optional[List[EnvVarInput]] = None,
) -> Layer:
"""Create a new layer.
Args:
slug: Human-readable layer slug.
description: Optional description of the layer's purpose.
layers: Base layer IDs or slugs to include.
env_vars: Environment variables for this layer.
"""
return self._bridge.run(
self._async.create(
slug, description=description, layers=layers, env_vars=env_vars
)
)
def update(
self,
layer: str,
*,
slug: Optional[str] = None,
description: Optional[str] = None,
layers: Optional[List[str]] = None,
env_vars: Optional[List[EnvVarUpdate]] = None,
) -> Layer:
"""Update a layer.
Args:
layer: Layer ID or slug.
slug: New layer slug.
description: New description.
layers: Replace all included layers.
env_vars: Deep merge with existing environment variables.
"""
return self._bridge.run(
self._async.update(
layer,
slug=slug,
description=description,
layers=layers,
env_vars=env_vars,
)
)
def delete(self, layer: str) -> None:
"""Delete a layer.
Args:
layer: Layer ID or slug.
"""
self._bridge.run(self._async.delete(layer))
def apps(
self,
layer: str,
*,
cursor: Optional[str] = None,
limit: Optional[int] = None,
) -> PaginatedList[LayerAppRef]:
"""List apps that reference this layer.
Args:
layer: Layer ID or slug.
cursor: Pagination cursor.
limit: Maximum number of items to return.
"""
paginated = self._bridge.run(
self._async.apps(layer, cursor=cursor, limit=limit)
)
return PaginatedList(self._bridge, paginated)