PyFlumeNG 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyflume/__init__.py +97 -0
- pyflume/auth.py +390 -0
- pyflume/client.py +1385 -0
- pyflume/constants.py +31 -0
- pyflume/data.py +226 -0
- pyflume/devices.py +70 -0
- pyflume/errors.py +57 -0
- pyflume/leak.py +83 -0
- pyflume/models.py +681 -0
- pyflume/notifications.py +138 -0
- pyflume/py.typed +1 -0
- pyflume/rate_limit.py +52 -0
- pyflume/types.py +48 -0
- pyflume/usage.py +265 -0
- pyflume/utils.py +140 -0
- pyflumeng-0.9.0.dist-info/METADATA +149 -0
- pyflumeng-0.9.0.dist-info/RECORD +20 -0
- pyflumeng-0.9.0.dist-info/WHEEL +5 -0
- pyflumeng-0.9.0.dist-info/licenses/LICENSE +21 -0
- pyflumeng-0.9.0.dist-info/top_level.txt +1 -0
pyflume/client.py
ADDED
|
@@ -0,0 +1,1385 @@
|
|
|
1
|
+
"""Complete resource client for documented and portal-discovered Flume routes."""
|
|
2
|
+
|
|
3
|
+
from typing import (
|
|
4
|
+
Any,
|
|
5
|
+
Iterator,
|
|
6
|
+
List,
|
|
7
|
+
Optional,
|
|
8
|
+
Set,
|
|
9
|
+
Tuple,
|
|
10
|
+
Type,
|
|
11
|
+
TypeVar,
|
|
12
|
+
Union,
|
|
13
|
+
cast,
|
|
14
|
+
overload,
|
|
15
|
+
)
|
|
16
|
+
from urllib.parse import urljoin
|
|
17
|
+
|
|
18
|
+
from requests import Response, Session
|
|
19
|
+
|
|
20
|
+
from .auth import FlumeAuth, FlumePortalAuth
|
|
21
|
+
from .constants import API_BASE_URL
|
|
22
|
+
from .errors import FlumeCapabilityError, FlumeHTTPError, FlumeRateLimitError
|
|
23
|
+
from .models import (
|
|
24
|
+
AccuracyResult,
|
|
25
|
+
ApiClient,
|
|
26
|
+
Budget,
|
|
27
|
+
Contact,
|
|
28
|
+
CurrentFlow,
|
|
29
|
+
Device,
|
|
30
|
+
DoNotAlertSchedule,
|
|
31
|
+
FlumeModel,
|
|
32
|
+
FlumeResponse,
|
|
33
|
+
Insurer,
|
|
34
|
+
Integration,
|
|
35
|
+
Leak,
|
|
36
|
+
Location,
|
|
37
|
+
LocationAccess,
|
|
38
|
+
LocationProfiles,
|
|
39
|
+
Notification,
|
|
40
|
+
ProService,
|
|
41
|
+
PurchaseOption,
|
|
42
|
+
QueryResult,
|
|
43
|
+
Span,
|
|
44
|
+
SpanType,
|
|
45
|
+
Subscription,
|
|
46
|
+
UsageAlert,
|
|
47
|
+
UsageAlertRule,
|
|
48
|
+
User,
|
|
49
|
+
)
|
|
50
|
+
from .types import JSONDict, JSONValue, RequestParams, ResourceId
|
|
51
|
+
|
|
52
|
+
ModelT = TypeVar("ModelT", bound=FlumeModel)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class FlumeClient:
|
|
56
|
+
"""Call Flume endpoints with a PersonalAuth or PortalAuth object."""
|
|
57
|
+
|
|
58
|
+
def __init__(
|
|
59
|
+
self,
|
|
60
|
+
auth: Union[FlumeAuth, FlumePortalAuth],
|
|
61
|
+
http_session: Optional[Session] = None,
|
|
62
|
+
base_url: str = API_BASE_URL,
|
|
63
|
+
timeout: float = 30,
|
|
64
|
+
) -> None:
|
|
65
|
+
self.auth: Union[FlumeAuth, FlumePortalAuth] = auth
|
|
66
|
+
auth_session = getattr(auth, "_http_session", None)
|
|
67
|
+
self._http_session: Session = http_session or (
|
|
68
|
+
auth_session if isinstance(auth_session, Session) else Session()
|
|
69
|
+
)
|
|
70
|
+
self.base_url: str = base_url.rstrip("/")
|
|
71
|
+
self.timeout: float = timeout
|
|
72
|
+
self.last_response: Optional[Response] = None
|
|
73
|
+
self.rate_limit = auth.rate_limit
|
|
74
|
+
|
|
75
|
+
def request(
|
|
76
|
+
self,
|
|
77
|
+
method: str,
|
|
78
|
+
path: str,
|
|
79
|
+
params: Optional[RequestParams] = None,
|
|
80
|
+
json: Optional[JSONValue] = None,
|
|
81
|
+
data: Any = None,
|
|
82
|
+
**kwargs: Any,
|
|
83
|
+
) -> JSONDict:
|
|
84
|
+
"""Return the complete Flume response envelope.
|
|
85
|
+
|
|
86
|
+
A 401 is retried once after refreshing the auth object. All other
|
|
87
|
+
errors retain the response envelope and are raised as typed errors.
|
|
88
|
+
"""
|
|
89
|
+
url = (
|
|
90
|
+
path
|
|
91
|
+
if path.startswith("http")
|
|
92
|
+
else urljoin(self.base_url + "/", path.lstrip("/"))
|
|
93
|
+
)
|
|
94
|
+
retried = False
|
|
95
|
+
request_timeout = kwargs.pop("timeout", self.timeout)
|
|
96
|
+
while True:
|
|
97
|
+
self.auth.ensure_valid()
|
|
98
|
+
response = self._http_session.request(
|
|
99
|
+
method.upper(),
|
|
100
|
+
url,
|
|
101
|
+
headers=self.auth.authorization_header,
|
|
102
|
+
params=cast(Any, params),
|
|
103
|
+
json=json,
|
|
104
|
+
data=data,
|
|
105
|
+
timeout=request_timeout,
|
|
106
|
+
**kwargs,
|
|
107
|
+
)
|
|
108
|
+
self.last_response = response
|
|
109
|
+
if self.rate_limit is not None:
|
|
110
|
+
self.rate_limit.update_from_headers(response.headers)
|
|
111
|
+
try:
|
|
112
|
+
envelope = response.json()
|
|
113
|
+
except ValueError as exc:
|
|
114
|
+
envelope = {}
|
|
115
|
+
if response.status_code < 200 or response.status_code >= 300:
|
|
116
|
+
raise FlumeHTTPError(
|
|
117
|
+
"Flume returned a non-JSON error", response
|
|
118
|
+
) from exc
|
|
119
|
+
if response.status_code == 401 and not retried:
|
|
120
|
+
self.auth.refresh()
|
|
121
|
+
retried = True
|
|
122
|
+
continue
|
|
123
|
+
if response.status_code == 429:
|
|
124
|
+
raise FlumeRateLimitError(
|
|
125
|
+
"Flume API rate limit exceeded", response, envelope
|
|
126
|
+
)
|
|
127
|
+
if response.status_code < 200 or response.status_code >= 300:
|
|
128
|
+
raise FlumeHTTPError("Flume API request failed", response, envelope)
|
|
129
|
+
return envelope
|
|
130
|
+
|
|
131
|
+
@overload
|
|
132
|
+
def data(
|
|
133
|
+
self,
|
|
134
|
+
method: str,
|
|
135
|
+
path: str,
|
|
136
|
+
model: Type[ModelT],
|
|
137
|
+
**kwargs: Any,
|
|
138
|
+
) -> Union[ModelT, List[ModelT]]: ...
|
|
139
|
+
|
|
140
|
+
@overload
|
|
141
|
+
def data(
|
|
142
|
+
self,
|
|
143
|
+
method: str,
|
|
144
|
+
path: str,
|
|
145
|
+
model: None = None,
|
|
146
|
+
**kwargs: Any,
|
|
147
|
+
) -> JSONValue: ...
|
|
148
|
+
|
|
149
|
+
def data(
|
|
150
|
+
self,
|
|
151
|
+
method: str,
|
|
152
|
+
path: str,
|
|
153
|
+
model: Optional[Type[FlumeModel]] = None,
|
|
154
|
+
**kwargs: Any,
|
|
155
|
+
) -> Any:
|
|
156
|
+
"""Return the response data parsed into portal-shaped models."""
|
|
157
|
+
required_capability = kwargs.pop("requires_capability", None)
|
|
158
|
+
if required_capability is not None:
|
|
159
|
+
self._require_capability(required_capability)
|
|
160
|
+
return self.response(method, path, model=model, **kwargs).data
|
|
161
|
+
|
|
162
|
+
@overload
|
|
163
|
+
def response(
|
|
164
|
+
self,
|
|
165
|
+
method: str,
|
|
166
|
+
path: str,
|
|
167
|
+
model: Type[ModelT],
|
|
168
|
+
**kwargs: Any,
|
|
169
|
+
) -> FlumeResponse[ModelT]: ...
|
|
170
|
+
|
|
171
|
+
@overload
|
|
172
|
+
def response(
|
|
173
|
+
self,
|
|
174
|
+
method: str,
|
|
175
|
+
path: str,
|
|
176
|
+
model: None = None,
|
|
177
|
+
**kwargs: Any,
|
|
178
|
+
) -> FlumeResponse[JSONValue]: ...
|
|
179
|
+
|
|
180
|
+
def response(
|
|
181
|
+
self,
|
|
182
|
+
method: str,
|
|
183
|
+
path: str,
|
|
184
|
+
model: Optional[Type[FlumeModel]] = None,
|
|
185
|
+
**kwargs: Any,
|
|
186
|
+
) -> FlumeResponse[Any]:
|
|
187
|
+
"""Return a typed Flume response envelope."""
|
|
188
|
+
return FlumeResponse(self.request(method, path, **kwargs), model=model)
|
|
189
|
+
|
|
190
|
+
def data_one(
|
|
191
|
+
self,
|
|
192
|
+
method: str,
|
|
193
|
+
path: str,
|
|
194
|
+
model: Type[ModelT],
|
|
195
|
+
**kwargs: Any,
|
|
196
|
+
) -> Optional[ModelT]:
|
|
197
|
+
"""Return the first parsed resource, matching portal ``model()`` calls."""
|
|
198
|
+
value = self.data(method, path, model, **kwargs)
|
|
199
|
+
if isinstance(value, list):
|
|
200
|
+
return value[0] if value else None
|
|
201
|
+
return cast(Optional[ModelT], value)
|
|
202
|
+
|
|
203
|
+
@overload
|
|
204
|
+
def list_all(
|
|
205
|
+
self,
|
|
206
|
+
path: str,
|
|
207
|
+
params: Optional[RequestParams],
|
|
208
|
+
model: Type[ModelT],
|
|
209
|
+
) -> List[ModelT]: ...
|
|
210
|
+
|
|
211
|
+
@overload
|
|
212
|
+
def list_all(
|
|
213
|
+
self,
|
|
214
|
+
path: str,
|
|
215
|
+
params: Optional[RequestParams] = None,
|
|
216
|
+
) -> List[FlumeModel]: ...
|
|
217
|
+
|
|
218
|
+
def list_all(
|
|
219
|
+
self,
|
|
220
|
+
path: str,
|
|
221
|
+
params: Optional[RequestParams] = None,
|
|
222
|
+
model: Type[FlumeModel] = FlumeModel,
|
|
223
|
+
) -> Any:
|
|
224
|
+
"""Fetch every page from a paginated Flume list endpoint."""
|
|
225
|
+
results: List[FlumeModel] = []
|
|
226
|
+
for page in self.iter_pages(path, params=params, model=model):
|
|
227
|
+
page_data = page.data or []
|
|
228
|
+
if isinstance(page_data, list):
|
|
229
|
+
results.extend(cast(List[FlumeModel], page_data))
|
|
230
|
+
else:
|
|
231
|
+
results.append(cast(FlumeModel, page_data))
|
|
232
|
+
return results
|
|
233
|
+
|
|
234
|
+
@overload
|
|
235
|
+
def iter_pages(
|
|
236
|
+
self,
|
|
237
|
+
path: str,
|
|
238
|
+
params: Optional[RequestParams],
|
|
239
|
+
model: Type[ModelT],
|
|
240
|
+
) -> Iterator[FlumeResponse[ModelT]]: ...
|
|
241
|
+
|
|
242
|
+
@overload
|
|
243
|
+
def iter_pages(
|
|
244
|
+
self,
|
|
245
|
+
path: str,
|
|
246
|
+
params: Optional[RequestParams] = None,
|
|
247
|
+
) -> Iterator[FlumeResponse[FlumeModel]]: ...
|
|
248
|
+
|
|
249
|
+
def iter_pages(
|
|
250
|
+
self,
|
|
251
|
+
path: str,
|
|
252
|
+
params: Optional[RequestParams] = None,
|
|
253
|
+
model: Type[FlumeModel] = FlumeModel,
|
|
254
|
+
) -> Any:
|
|
255
|
+
"""Yield typed response envelopes for each page of a list endpoint.
|
|
256
|
+
|
|
257
|
+
A repeated pagination link is treated as a server-side pagination
|
|
258
|
+
failure and stops traversal instead of causing an infinite loop.
|
|
259
|
+
"""
|
|
260
|
+
request_params = dict(params or {})
|
|
261
|
+
request_params.setdefault("limit", 2000)
|
|
262
|
+
request_params.setdefault("offset", 0)
|
|
263
|
+
next_path: Optional[str] = path
|
|
264
|
+
seen: Set[Tuple[str, Tuple[Tuple[str, str], ...]]] = set()
|
|
265
|
+
while next_path:
|
|
266
|
+
marker = (
|
|
267
|
+
next_path,
|
|
268
|
+
tuple(
|
|
269
|
+
sorted((key, repr(value)) for key, value in request_params.items())
|
|
270
|
+
),
|
|
271
|
+
)
|
|
272
|
+
if marker in seen:
|
|
273
|
+
raise RuntimeError("Flume pagination returned a repeated next link")
|
|
274
|
+
seen.add(marker)
|
|
275
|
+
page = self.response("GET", next_path, model=model, params=request_params)
|
|
276
|
+
yield page
|
|
277
|
+
next_path = page.next_url
|
|
278
|
+
request_params = {}
|
|
279
|
+
|
|
280
|
+
def _user_path(self, suffix: str = "") -> str:
|
|
281
|
+
return "/users/{0}{1}".format(self.auth.user_id, suffix)
|
|
282
|
+
|
|
283
|
+
def _require_capability(self, capability: str) -> None:
|
|
284
|
+
"""Raise before transport when auth lacks a named capability."""
|
|
285
|
+
if capability not in getattr(self.auth, "capabilities", frozenset()):
|
|
286
|
+
raise FlumeCapabilityError(
|
|
287
|
+
"This operation requires '{0}'. Use PortalAuth instead of "
|
|
288
|
+
"PersonalAuth.".format(capability),
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
# Documented user, device, query, and flow routes.
|
|
292
|
+
def get_user(self) -> Optional[User]:
|
|
293
|
+
return self.data_one("GET", self._user_path(), User)
|
|
294
|
+
|
|
295
|
+
def list_devices(self, **params: JSONValue) -> List[Device]:
|
|
296
|
+
return self.list_all(self._user_path("/devices"), params, Device)
|
|
297
|
+
|
|
298
|
+
def list_portal_devices(self, **params: JSONValue) -> List[Device]:
|
|
299
|
+
"""List devices through the portal's root device route."""
|
|
300
|
+
return self.list_all("/devices", params, Device)
|
|
301
|
+
|
|
302
|
+
def get_device(
|
|
303
|
+
self, device_id: ResourceId, **params: JSONValue
|
|
304
|
+
) -> Optional[Device]:
|
|
305
|
+
return self.data_one(
|
|
306
|
+
"GET",
|
|
307
|
+
self._user_path("/devices/{0}".format(device_id)),
|
|
308
|
+
Device,
|
|
309
|
+
params=params,
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
def get_portal_device(
|
|
313
|
+
self, device_id: ResourceId, **params: JSONValue
|
|
314
|
+
) -> Optional[Device]:
|
|
315
|
+
"""Fetch one device through the portal's root device route."""
|
|
316
|
+
return self.data_one(
|
|
317
|
+
"GET", "/devices/{0}".format(device_id), Device, params=params
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
def query(self, device_id: ResourceId, payload: JSONDict) -> List[QueryResult]:
|
|
321
|
+
return cast(
|
|
322
|
+
List[QueryResult],
|
|
323
|
+
self.data(
|
|
324
|
+
"POST",
|
|
325
|
+
self._user_path("/devices/{0}/query".format(device_id)),
|
|
326
|
+
QueryResult,
|
|
327
|
+
json=payload,
|
|
328
|
+
),
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
def portal_query(
|
|
332
|
+
self, device_id: ResourceId, payload: JSONDict
|
|
333
|
+
) -> List[QueryResult]:
|
|
334
|
+
"""Submit a query through the portal's root device route."""
|
|
335
|
+
return cast(
|
|
336
|
+
List[QueryResult],
|
|
337
|
+
self.data(
|
|
338
|
+
"POST",
|
|
339
|
+
"/devices/{0}/query".format(device_id),
|
|
340
|
+
QueryResult,
|
|
341
|
+
json=payload,
|
|
342
|
+
),
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
def get_current_flow(self, device_id: ResourceId) -> Optional[CurrentFlow]:
|
|
346
|
+
return self.data_one(
|
|
347
|
+
"GET",
|
|
348
|
+
self._user_path("/devices/{0}/query/active".format(device_id)),
|
|
349
|
+
CurrentFlow,
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
def get_portal_current_flow(self, device_id: ResourceId) -> Optional[CurrentFlow]:
|
|
353
|
+
"""Read current flow through the portal's root device route."""
|
|
354
|
+
return self.data_one(
|
|
355
|
+
"GET", "/devices/{0}/query/active".format(device_id), CurrentFlow
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
# Locations and account mutations.
|
|
359
|
+
def list_locations(self, **params: JSONValue) -> List[Location]:
|
|
360
|
+
return self.list_all(self._user_path("/locations"), params, Location)
|
|
361
|
+
|
|
362
|
+
def list_portal_locations(self, **params: JSONValue) -> List[Location]:
|
|
363
|
+
"""List locations through the portal's root location route."""
|
|
364
|
+
return self.list_all("/locations", params, Location)
|
|
365
|
+
|
|
366
|
+
def get_location_profiles(self) -> Optional[LocationProfiles]:
|
|
367
|
+
"""Fetch the portal's appliance/profile metadata."""
|
|
368
|
+
return self.data_one("GET", "/location-profiles", LocationProfiles)
|
|
369
|
+
|
|
370
|
+
def get_location(self, location_id: ResourceId) -> Optional[Location]:
|
|
371
|
+
return self.data_one(
|
|
372
|
+
"GET", self._user_path("/locations/{0}".format(location_id)), Location
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
def get_portal_location(self, location_id: ResourceId) -> Optional[Location]:
|
|
376
|
+
"""Fetch one location through the portal's root location route."""
|
|
377
|
+
return self.data_one("GET", "/locations/{0}".format(location_id), Location)
|
|
378
|
+
|
|
379
|
+
def create_location(self, payload: JSONDict) -> JSONValue:
|
|
380
|
+
return self.data("POST", self._user_path("/locations"), json=payload)
|
|
381
|
+
|
|
382
|
+
def create_portal_location(self, payload: JSONDict) -> JSONValue:
|
|
383
|
+
"""Create a location through the portal's root location route."""
|
|
384
|
+
self._require_capability("portal_writes")
|
|
385
|
+
return self.data("POST", "/locations", json=payload)
|
|
386
|
+
|
|
387
|
+
def update_location(self, location_id: ResourceId, payload: JSONDict) -> JSONValue:
|
|
388
|
+
return self.data(
|
|
389
|
+
"PATCH", self._user_path("/locations/{0}".format(location_id)), json=payload
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
def update_portal_location(
|
|
393
|
+
self, location_id: ResourceId, payload: JSONDict
|
|
394
|
+
) -> JSONValue:
|
|
395
|
+
"""Update a location through the portal's root location route."""
|
|
396
|
+
self._require_capability("portal_writes")
|
|
397
|
+
return self.data("PATCH", "/locations/{0}".format(location_id), json=payload)
|
|
398
|
+
|
|
399
|
+
def update_user(self, payload: JSONDict) -> JSONValue:
|
|
400
|
+
return self.data("PATCH", self._user_path(), json=payload)
|
|
401
|
+
|
|
402
|
+
def update_portal_user(self, payload: JSONDict) -> JSONValue:
|
|
403
|
+
"""Update the current user through the portal's collection route."""
|
|
404
|
+
self._require_capability("portal_writes")
|
|
405
|
+
return self.data("PATCH", "/users/{0}".format(self.auth.user_id), json=payload)
|
|
406
|
+
|
|
407
|
+
def update_password(self, payload: JSONDict) -> JSONValue:
|
|
408
|
+
return self.data("PATCH", self._user_path("/password"), json=payload)
|
|
409
|
+
|
|
410
|
+
def update_email(self, payload: JSONDict) -> JSONValue:
|
|
411
|
+
return self.data("PATCH", self._user_path("/email"), json=payload)
|
|
412
|
+
|
|
413
|
+
# Notifications, alerts, and rules.
|
|
414
|
+
def list_notifications(self, **params: JSONValue) -> List[Notification]:
|
|
415
|
+
return self.list_all(self._user_path("/notifications"), params, Notification)
|
|
416
|
+
|
|
417
|
+
def list_portal_notifications(self, **params: JSONValue) -> List[Notification]:
|
|
418
|
+
"""List notifications through the portal's root notification route."""
|
|
419
|
+
return self.list_all("/notifications", params, Notification)
|
|
420
|
+
|
|
421
|
+
def get_notification(self, notification_id: ResourceId) -> Optional[Notification]:
|
|
422
|
+
return self.data_one(
|
|
423
|
+
"GET",
|
|
424
|
+
self._user_path("/notifications/{0}".format(notification_id)),
|
|
425
|
+
Notification,
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
def get_portal_notification(
|
|
429
|
+
self, notification_id: ResourceId
|
|
430
|
+
) -> Optional[Notification]:
|
|
431
|
+
"""Fetch one notification through the portal's root route."""
|
|
432
|
+
return self.data_one(
|
|
433
|
+
"GET", "/notifications/{0}".format(notification_id), Notification
|
|
434
|
+
)
|
|
435
|
+
|
|
436
|
+
def update_notification(
|
|
437
|
+
self, notification_id: ResourceId, payload: JSONDict
|
|
438
|
+
) -> JSONValue:
|
|
439
|
+
return self.data(
|
|
440
|
+
"PATCH",
|
|
441
|
+
self._user_path("/notifications/{0}".format(notification_id)),
|
|
442
|
+
json=payload,
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
def update_portal_notification(
|
|
446
|
+
self,
|
|
447
|
+
notification_id: ResourceId,
|
|
448
|
+
payload: JSONDict,
|
|
449
|
+
) -> JSONValue:
|
|
450
|
+
"""Update a notification through the portal's root route."""
|
|
451
|
+
self._require_capability("portal_writes")
|
|
452
|
+
return self.data(
|
|
453
|
+
"PATCH", "/notifications/{0}".format(notification_id), json=payload
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
def delete_notification(self, notification_id: ResourceId) -> JSONValue:
|
|
457
|
+
return self.data(
|
|
458
|
+
"DELETE", self._user_path("/notifications/{0}".format(notification_id))
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
def delete_portal_notification(self, notification_id: ResourceId) -> JSONValue:
|
|
462
|
+
"""Delete a notification through the portal's root route."""
|
|
463
|
+
self._require_capability("portal_writes")
|
|
464
|
+
return self.data("DELETE", "/notifications/{0}".format(notification_id))
|
|
465
|
+
|
|
466
|
+
def set_notification_read(
|
|
467
|
+
self,
|
|
468
|
+
notification_id: ResourceId,
|
|
469
|
+
read: bool = True,
|
|
470
|
+
portal: bool = True,
|
|
471
|
+
) -> JSONValue:
|
|
472
|
+
"""Set notification read state using the portal's frontend payload."""
|
|
473
|
+
if portal:
|
|
474
|
+
return self.update_portal_notification(
|
|
475
|
+
notification_id, {"read": bool(read)}
|
|
476
|
+
)
|
|
477
|
+
return self.update_notification(notification_id, {"read": bool(read)})
|
|
478
|
+
|
|
479
|
+
def list_usage_alerts(self, **params: JSONValue) -> List[UsageAlert]:
|
|
480
|
+
return self.list_all(self._user_path("/usage-alerts"), params, UsageAlert)
|
|
481
|
+
|
|
482
|
+
def list_event_rules(
|
|
483
|
+
self,
|
|
484
|
+
device_id: ResourceId,
|
|
485
|
+
**params: JSONValue,
|
|
486
|
+
) -> List[UsageAlertRule]:
|
|
487
|
+
return self.list_all(
|
|
488
|
+
self._user_path("/devices/{0}/rules".format(device_id)),
|
|
489
|
+
params,
|
|
490
|
+
UsageAlertRule,
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
def list_usage_alert_rules(
|
|
494
|
+
self,
|
|
495
|
+
device_id: ResourceId,
|
|
496
|
+
**params: JSONValue,
|
|
497
|
+
) -> List[UsageAlertRule]:
|
|
498
|
+
return self.list_all(
|
|
499
|
+
self._user_path("/devices/{0}/rules/usage-alerts".format(device_id)),
|
|
500
|
+
params,
|
|
501
|
+
UsageAlertRule,
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
def list_portal_usage_alert_rules(
|
|
505
|
+
self,
|
|
506
|
+
device_id: ResourceId,
|
|
507
|
+
**params: JSONValue,
|
|
508
|
+
) -> List[UsageAlertRule]:
|
|
509
|
+
"""List rules through the portal's root device route."""
|
|
510
|
+
return self.list_all(
|
|
511
|
+
"/devices/{0}/rules/usage-alerts".format(device_id), params, UsageAlertRule
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
def get_usage_alert_rule(
|
|
515
|
+
self,
|
|
516
|
+
device_id: ResourceId,
|
|
517
|
+
rule_id: ResourceId,
|
|
518
|
+
) -> Optional[UsageAlertRule]:
|
|
519
|
+
return self.data_one(
|
|
520
|
+
"GET",
|
|
521
|
+
self._user_path(
|
|
522
|
+
"/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id)
|
|
523
|
+
),
|
|
524
|
+
UsageAlertRule,
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
def get_portal_usage_alert_rule(
|
|
528
|
+
self,
|
|
529
|
+
device_id: ResourceId,
|
|
530
|
+
rule_id: ResourceId,
|
|
531
|
+
) -> Optional[UsageAlertRule]:
|
|
532
|
+
"""Fetch one rule through the portal's root device route."""
|
|
533
|
+
return self.data_one(
|
|
534
|
+
"GET",
|
|
535
|
+
"/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id),
|
|
536
|
+
UsageAlertRule,
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
def create_usage_alert_rule(
|
|
540
|
+
self, device_id: ResourceId, payload: JSONDict
|
|
541
|
+
) -> JSONValue:
|
|
542
|
+
return self.data(
|
|
543
|
+
"POST",
|
|
544
|
+
self._user_path("/devices/{0}/rules/usage-alerts".format(device_id)),
|
|
545
|
+
json=payload,
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
def create_portal_usage_alert_rule(
|
|
549
|
+
self,
|
|
550
|
+
device_id: ResourceId,
|
|
551
|
+
payload: JSONDict,
|
|
552
|
+
) -> JSONValue:
|
|
553
|
+
"""Create a rule through the portal's root device route."""
|
|
554
|
+
self._require_capability("portal_writes")
|
|
555
|
+
return self.data(
|
|
556
|
+
"POST", "/devices/{0}/rules/usage-alerts".format(device_id), json=payload
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
def update_usage_alert_rule(
|
|
560
|
+
self,
|
|
561
|
+
device_id: ResourceId,
|
|
562
|
+
rule_id: ResourceId,
|
|
563
|
+
payload: JSONDict,
|
|
564
|
+
) -> JSONValue:
|
|
565
|
+
self._require_capability("portal_writes")
|
|
566
|
+
return self.data(
|
|
567
|
+
"PATCH",
|
|
568
|
+
self._user_path(
|
|
569
|
+
"/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id)
|
|
570
|
+
),
|
|
571
|
+
json=payload,
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
def update_portal_usage_alert_rule(
|
|
575
|
+
self,
|
|
576
|
+
device_id: ResourceId,
|
|
577
|
+
rule_id: ResourceId,
|
|
578
|
+
payload: JSONDict,
|
|
579
|
+
) -> JSONValue:
|
|
580
|
+
"""Update a rule using the portal service's collection PATCH form."""
|
|
581
|
+
self._require_capability("portal_writes")
|
|
582
|
+
return self.data(
|
|
583
|
+
"PATCH",
|
|
584
|
+
"/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id),
|
|
585
|
+
json=payload,
|
|
586
|
+
)
|
|
587
|
+
|
|
588
|
+
def delete_usage_alert_rule(
|
|
589
|
+
self, device_id: ResourceId, rule_id: ResourceId
|
|
590
|
+
) -> JSONValue:
|
|
591
|
+
return self.data(
|
|
592
|
+
"DELETE",
|
|
593
|
+
self._user_path(
|
|
594
|
+
"/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id)
|
|
595
|
+
),
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
def delete_portal_usage_alert_rule(
|
|
599
|
+
self,
|
|
600
|
+
device_id: ResourceId,
|
|
601
|
+
rule_id: ResourceId,
|
|
602
|
+
) -> JSONValue:
|
|
603
|
+
"""Delete a rule through the portal service's collection route."""
|
|
604
|
+
self._require_capability("portal_writes")
|
|
605
|
+
return self.data(
|
|
606
|
+
"DELETE", "/devices/{0}/rules/usage-alerts/{1}".format(device_id, rule_id)
|
|
607
|
+
)
|
|
608
|
+
|
|
609
|
+
def set_usage_alert_rule_active(
|
|
610
|
+
self,
|
|
611
|
+
device_id: ResourceId,
|
|
612
|
+
rule_id: ResourceId,
|
|
613
|
+
active: bool,
|
|
614
|
+
) -> JSONValue:
|
|
615
|
+
return self.update_usage_alert_rule(
|
|
616
|
+
device_id, rule_id, {"active": bool(active)}
|
|
617
|
+
)
|
|
618
|
+
|
|
619
|
+
def set_portal_usage_alert_rule_active(
|
|
620
|
+
self,
|
|
621
|
+
device_id: ResourceId,
|
|
622
|
+
rule_id: ResourceId,
|
|
623
|
+
active: bool,
|
|
624
|
+
) -> JSONValue:
|
|
625
|
+
"""Toggle a rule using the portal service's collection PATCH form."""
|
|
626
|
+
self._require_capability("portal_writes")
|
|
627
|
+
return self.update_portal_usage_alert_rule(
|
|
628
|
+
device_id,
|
|
629
|
+
rule_id,
|
|
630
|
+
{"active": bool(active)},
|
|
631
|
+
)
|
|
632
|
+
|
|
633
|
+
def list_leaks(self, device_id: ResourceId) -> Optional[Leak]:
|
|
634
|
+
return self.data_one(
|
|
635
|
+
"GET", self._user_path("/devices/{0}/leaks/active".format(device_id)), Leak
|
|
636
|
+
)
|
|
637
|
+
|
|
638
|
+
def list_portal_leaks(self, device_id: ResourceId) -> List[Leak]:
|
|
639
|
+
"""Read active leaks through the portal's user-scoped route."""
|
|
640
|
+
return cast(
|
|
641
|
+
List[Leak],
|
|
642
|
+
self.data(
|
|
643
|
+
"GET",
|
|
644
|
+
self._user_path("/devices/{0}/leaks/active".format(device_id)),
|
|
645
|
+
Leak,
|
|
646
|
+
),
|
|
647
|
+
)
|
|
648
|
+
|
|
649
|
+
def get_leak(self, device_id: ResourceId, leak_id: ResourceId) -> Optional[Leak]:
|
|
650
|
+
return self.data_one(
|
|
651
|
+
"GET",
|
|
652
|
+
self._user_path("/devices/{0}/leaks/{1}".format(device_id, leak_id)),
|
|
653
|
+
Leak,
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
def get_portal_leak(
|
|
657
|
+
self, device_id: ResourceId, leak_id: ResourceId
|
|
658
|
+
) -> Optional[Leak]:
|
|
659
|
+
"""Read one active leak through the portal's user-scoped route."""
|
|
660
|
+
return self.data_one(
|
|
661
|
+
"GET",
|
|
662
|
+
self._user_path("/devices/{0}/leaks/{1}".format(device_id, leak_id)),
|
|
663
|
+
Leak,
|
|
664
|
+
)
|
|
665
|
+
|
|
666
|
+
# Budgets and subscriptions.
|
|
667
|
+
def list_budgets(self, device_id: ResourceId, **params: JSONValue) -> List[Budget]:
|
|
668
|
+
return self.list_all(
|
|
669
|
+
self._user_path("/devices/{0}/budgets".format(device_id)), params, Budget
|
|
670
|
+
)
|
|
671
|
+
|
|
672
|
+
def list_portal_budgets(
|
|
673
|
+
self, device_id: ResourceId, **params: JSONValue
|
|
674
|
+
) -> List[Budget]:
|
|
675
|
+
"""List budgets through the portal's root device route."""
|
|
676
|
+
return self.list_all("/devices/{0}/budgets".format(device_id), params, Budget)
|
|
677
|
+
|
|
678
|
+
def get_budget(
|
|
679
|
+
self, device_id: ResourceId, budget_id: ResourceId
|
|
680
|
+
) -> Optional[Budget]:
|
|
681
|
+
return self.data_one(
|
|
682
|
+
"GET",
|
|
683
|
+
self._user_path("/devices/{0}/budgets/{1}".format(device_id, budget_id)),
|
|
684
|
+
Budget,
|
|
685
|
+
)
|
|
686
|
+
|
|
687
|
+
def get_portal_budget(
|
|
688
|
+
self,
|
|
689
|
+
device_id: ResourceId,
|
|
690
|
+
budget_id: ResourceId,
|
|
691
|
+
) -> Optional[Budget]:
|
|
692
|
+
"""Fetch one budget using the portal service's collection GET form."""
|
|
693
|
+
return self.data_one(
|
|
694
|
+
"GET", "/devices/{0}/budgets/{1}".format(device_id, budget_id), Budget
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
def create_budget(self, device_id: ResourceId, payload: JSONDict) -> JSONValue:
|
|
698
|
+
return self.data(
|
|
699
|
+
"POST",
|
|
700
|
+
self._user_path("/devices/{0}/budgets".format(device_id)),
|
|
701
|
+
json=payload,
|
|
702
|
+
)
|
|
703
|
+
|
|
704
|
+
def create_portal_budget(
|
|
705
|
+
self, device_id: ResourceId, payload: JSONDict
|
|
706
|
+
) -> JSONValue:
|
|
707
|
+
"""Create a budget through the portal's root device route."""
|
|
708
|
+
return self.data("POST", "/devices/{0}/budgets".format(device_id), json=payload)
|
|
709
|
+
|
|
710
|
+
def update_budget(
|
|
711
|
+
self,
|
|
712
|
+
device_id: ResourceId,
|
|
713
|
+
budget_id: ResourceId,
|
|
714
|
+
payload: JSONDict,
|
|
715
|
+
) -> JSONValue:
|
|
716
|
+
return self.data(
|
|
717
|
+
"PATCH",
|
|
718
|
+
self._user_path("/devices/{0}/budgets/{1}".format(device_id, budget_id)),
|
|
719
|
+
json=payload,
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
def update_portal_budget(
|
|
723
|
+
self,
|
|
724
|
+
device_id: ResourceId,
|
|
725
|
+
budget_id: ResourceId,
|
|
726
|
+
payload: JSONDict,
|
|
727
|
+
) -> JSONValue:
|
|
728
|
+
"""Update a budget using the portal service's collection PATCH form."""
|
|
729
|
+
return self.data(
|
|
730
|
+
"PATCH",
|
|
731
|
+
"/devices/{0}/budgets/{1}".format(device_id, budget_id),
|
|
732
|
+
json=payload,
|
|
733
|
+
)
|
|
734
|
+
|
|
735
|
+
def delete_budget(self, device_id: ResourceId, budget_id: ResourceId) -> JSONValue:
|
|
736
|
+
return self.data(
|
|
737
|
+
"DELETE",
|
|
738
|
+
self._user_path("/devices/{0}/budgets/{1}".format(device_id, budget_id)),
|
|
739
|
+
)
|
|
740
|
+
|
|
741
|
+
def delete_portal_budget(
|
|
742
|
+
self, device_id: ResourceId, budget_id: ResourceId
|
|
743
|
+
) -> JSONValue:
|
|
744
|
+
"""Delete a budget using the portal service's collection route."""
|
|
745
|
+
return self.data(
|
|
746
|
+
"DELETE", "/devices/{0}/budgets/{1}".format(device_id, budget_id)
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
def list_subscriptions(self, **params: JSONValue) -> List[Subscription]:
|
|
750
|
+
return self.list_all(self._user_path("/subscriptions"), params, Subscription)
|
|
751
|
+
|
|
752
|
+
def list_portal_subscriptions(self, **params: JSONValue) -> List[Subscription]:
|
|
753
|
+
"""List subscriptions through the portal's root route."""
|
|
754
|
+
return self.list_all("/subscriptions", params, Subscription)
|
|
755
|
+
|
|
756
|
+
def get_subscription(self, subscription_id: ResourceId) -> Optional[Subscription]:
|
|
757
|
+
return self.data_one(
|
|
758
|
+
"GET",
|
|
759
|
+
self._user_path("/subscriptions/{0}".format(subscription_id)),
|
|
760
|
+
Subscription,
|
|
761
|
+
)
|
|
762
|
+
|
|
763
|
+
def get_portal_subscription(
|
|
764
|
+
self, subscription_id: ResourceId
|
|
765
|
+
) -> Optional[Subscription]:
|
|
766
|
+
"""Fetch a subscription through the portal's collection GET form."""
|
|
767
|
+
return self.data_one(
|
|
768
|
+
"GET", "/subscriptions/{0}".format(subscription_id), Subscription
|
|
769
|
+
)
|
|
770
|
+
|
|
771
|
+
def create_location_subscription(
|
|
772
|
+
self,
|
|
773
|
+
location_id: ResourceId,
|
|
774
|
+
payload: JSONDict,
|
|
775
|
+
) -> JSONValue:
|
|
776
|
+
return self.data(
|
|
777
|
+
"POST",
|
|
778
|
+
self._user_path("/locations/{0}/subscriptions".format(location_id)),
|
|
779
|
+
json=payload,
|
|
780
|
+
)
|
|
781
|
+
|
|
782
|
+
def create_portal_subscription(
|
|
783
|
+
self,
|
|
784
|
+
location_id: ResourceId,
|
|
785
|
+
payload: JSONDict,
|
|
786
|
+
) -> JSONValue:
|
|
787
|
+
"""Create a subscription through the portal's root location route."""
|
|
788
|
+
return self.data(
|
|
789
|
+
"POST", "/locations/{0}/subscriptions".format(location_id), json=payload
|
|
790
|
+
)
|
|
791
|
+
|
|
792
|
+
def update_subscription(
|
|
793
|
+
self, subscription_id: ResourceId, payload: JSONDict
|
|
794
|
+
) -> JSONValue:
|
|
795
|
+
return self.data(
|
|
796
|
+
"PATCH",
|
|
797
|
+
self._user_path("/subscriptions/{0}".format(subscription_id)),
|
|
798
|
+
json=payload,
|
|
799
|
+
)
|
|
800
|
+
|
|
801
|
+
def update_portal_subscription(
|
|
802
|
+
self,
|
|
803
|
+
subscription_id: ResourceId,
|
|
804
|
+
payload: JSONDict,
|
|
805
|
+
) -> JSONValue:
|
|
806
|
+
"""Update a subscription through the portal's collection PATCH form."""
|
|
807
|
+
return self.data(
|
|
808
|
+
"PATCH", "/subscriptions/{0}".format(subscription_id), json=payload
|
|
809
|
+
)
|
|
810
|
+
|
|
811
|
+
def delete_subscription(self, subscription_id: ResourceId) -> JSONValue:
|
|
812
|
+
return self.data(
|
|
813
|
+
"DELETE", self._user_path("/subscriptions/{0}".format(subscription_id))
|
|
814
|
+
)
|
|
815
|
+
|
|
816
|
+
def delete_portal_subscription(self, subscription_id: ResourceId) -> JSONValue:
|
|
817
|
+
"""Delete a subscription through the portal's collection route."""
|
|
818
|
+
return self.data("DELETE", "/subscriptions/{0}".format(subscription_id))
|
|
819
|
+
|
|
820
|
+
def create_stripe_portal(self, return_url: str) -> JSONValue:
|
|
821
|
+
return self.data(
|
|
822
|
+
"POST", self._user_path("/stripe-portal"), json={"return_url": return_url}
|
|
823
|
+
)
|
|
824
|
+
|
|
825
|
+
def subscribe(self, return_url: str) -> JSONValue:
|
|
826
|
+
return self.data(
|
|
827
|
+
"POST", self._user_path("/subscribe"), json={"return_url": return_url}
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
# Portal-only usage schedules and shutoff configuration.
|
|
831
|
+
def list_do_not_alert_schedules(
|
|
832
|
+
self,
|
|
833
|
+
device_id: ResourceId,
|
|
834
|
+
**params: JSONValue,
|
|
835
|
+
) -> List[DoNotAlertSchedule]:
|
|
836
|
+
return self.list_all(
|
|
837
|
+
self._user_path("/devices/{0}/do-not-alert-schedules".format(device_id)),
|
|
838
|
+
params,
|
|
839
|
+
DoNotAlertSchedule,
|
|
840
|
+
)
|
|
841
|
+
|
|
842
|
+
def list_portal_do_not_alert_schedules(
|
|
843
|
+
self,
|
|
844
|
+
device_id: ResourceId,
|
|
845
|
+
**params: JSONValue,
|
|
846
|
+
) -> List[DoNotAlertSchedule]:
|
|
847
|
+
"""List DNA schedules through the portal's user-scoped route."""
|
|
848
|
+
return self.list_all(
|
|
849
|
+
self._user_path("/devices/{0}/do-not-alert-schedules".format(device_id)),
|
|
850
|
+
params,
|
|
851
|
+
DoNotAlertSchedule,
|
|
852
|
+
)
|
|
853
|
+
|
|
854
|
+
def create_do_not_alert_schedule(
|
|
855
|
+
self,
|
|
856
|
+
device_id: ResourceId,
|
|
857
|
+
payload: JSONDict,
|
|
858
|
+
) -> JSONValue:
|
|
859
|
+
return self.data(
|
|
860
|
+
"POST",
|
|
861
|
+
self._user_path("/devices/{0}/do-not-alert-schedules".format(device_id)),
|
|
862
|
+
json=payload,
|
|
863
|
+
)
|
|
864
|
+
|
|
865
|
+
def create_portal_do_not_alert_schedule(
|
|
866
|
+
self,
|
|
867
|
+
device_id: ResourceId,
|
|
868
|
+
payload: JSONDict,
|
|
869
|
+
) -> JSONValue:
|
|
870
|
+
"""Create a DNA schedule using the portal payload."""
|
|
871
|
+
return self.data(
|
|
872
|
+
"POST",
|
|
873
|
+
self._user_path("/devices/{0}/do-not-alert-schedules".format(device_id)),
|
|
874
|
+
json=payload,
|
|
875
|
+
)
|
|
876
|
+
|
|
877
|
+
def update_do_not_alert_schedule(
|
|
878
|
+
self,
|
|
879
|
+
device_id: ResourceId,
|
|
880
|
+
schedule_id: ResourceId,
|
|
881
|
+
payload: JSONDict,
|
|
882
|
+
) -> JSONValue:
|
|
883
|
+
return self.data(
|
|
884
|
+
"PATCH",
|
|
885
|
+
self._user_path(
|
|
886
|
+
"/devices/{0}/do-not-alert-schedules/{1}".format(device_id, schedule_id)
|
|
887
|
+
),
|
|
888
|
+
json=payload,
|
|
889
|
+
)
|
|
890
|
+
|
|
891
|
+
def update_portal_do_not_alert_schedule(
|
|
892
|
+
self,
|
|
893
|
+
device_id: ResourceId,
|
|
894
|
+
schedule_id: ResourceId,
|
|
895
|
+
payload: JSONDict,
|
|
896
|
+
) -> JSONValue:
|
|
897
|
+
"""Update a DNA schedule using the portal collection PATCH form."""
|
|
898
|
+
return self.data(
|
|
899
|
+
"PATCH",
|
|
900
|
+
self._user_path(
|
|
901
|
+
"/devices/{0}/do-not-alert-schedules/{1}".format(device_id, schedule_id)
|
|
902
|
+
),
|
|
903
|
+
json=payload,
|
|
904
|
+
)
|
|
905
|
+
|
|
906
|
+
def delete_do_not_alert_schedule(
|
|
907
|
+
self,
|
|
908
|
+
device_id: ResourceId,
|
|
909
|
+
schedule_id: ResourceId,
|
|
910
|
+
) -> JSONValue:
|
|
911
|
+
return self.data(
|
|
912
|
+
"DELETE",
|
|
913
|
+
self._user_path(
|
|
914
|
+
"/devices/{0}/do-not-alert-schedules/{1}".format(device_id, schedule_id)
|
|
915
|
+
),
|
|
916
|
+
)
|
|
917
|
+
|
|
918
|
+
def delete_portal_do_not_alert_schedule(
|
|
919
|
+
self,
|
|
920
|
+
device_id: ResourceId,
|
|
921
|
+
schedule_id: ResourceId,
|
|
922
|
+
) -> JSONValue:
|
|
923
|
+
"""Delete a DNA schedule using the portal collection route."""
|
|
924
|
+
return self.data(
|
|
925
|
+
"DELETE",
|
|
926
|
+
self._user_path(
|
|
927
|
+
"/devices/{0}/do-not-alert-schedules/{1}".format(device_id, schedule_id)
|
|
928
|
+
),
|
|
929
|
+
)
|
|
930
|
+
|
|
931
|
+
def update_rule_schedules(
|
|
932
|
+
self,
|
|
933
|
+
device_id: ResourceId,
|
|
934
|
+
rule_id: ResourceId,
|
|
935
|
+
payload: JSONDict,
|
|
936
|
+
) -> JSONValue:
|
|
937
|
+
return self.data(
|
|
938
|
+
"PATCH",
|
|
939
|
+
self._user_path(
|
|
940
|
+
"/devices/{0}/rules/usage-alerts/{1}/do-not-alert-schedules".format(
|
|
941
|
+
device_id, rule_id
|
|
942
|
+
)
|
|
943
|
+
),
|
|
944
|
+
json=payload,
|
|
945
|
+
)
|
|
946
|
+
|
|
947
|
+
def toggle_usage_alert_schedule(
|
|
948
|
+
self,
|
|
949
|
+
device_id: ResourceId,
|
|
950
|
+
rule_id: ResourceId,
|
|
951
|
+
schedule_id: ResourceId,
|
|
952
|
+
active: bool,
|
|
953
|
+
) -> JSONValue:
|
|
954
|
+
"""Associate a DNA schedule with a rule and set its active state."""
|
|
955
|
+
path = self._user_path(
|
|
956
|
+
"/devices/{0}/rules/usage-alerts/{1}/do-not-alert-schedules".format(
|
|
957
|
+
device_id,
|
|
958
|
+
rule_id,
|
|
959
|
+
),
|
|
960
|
+
)
|
|
961
|
+
return self.data(
|
|
962
|
+
"PATCH",
|
|
963
|
+
path,
|
|
964
|
+
params={"id": schedule_id},
|
|
965
|
+
json={"active": bool(active)},
|
|
966
|
+
)
|
|
967
|
+
|
|
968
|
+
def update_rule_shutoff_config(
|
|
969
|
+
self,
|
|
970
|
+
device_id: ResourceId,
|
|
971
|
+
rule_id: ResourceId,
|
|
972
|
+
payload: JSONDict,
|
|
973
|
+
) -> JSONValue:
|
|
974
|
+
return self.data(
|
|
975
|
+
"PATCH",
|
|
976
|
+
self._user_path(
|
|
977
|
+
"/devices/{0}/rules/usage-alerts/{1}/shutoff-config".format(
|
|
978
|
+
device_id, rule_id
|
|
979
|
+
)
|
|
980
|
+
),
|
|
981
|
+
json=payload,
|
|
982
|
+
)
|
|
983
|
+
|
|
984
|
+
# Sharing, integrations, diagnostics, and support endpoints discovered in the portal.
|
|
985
|
+
def list_location_access(
|
|
986
|
+
self,
|
|
987
|
+
location_id: ResourceId,
|
|
988
|
+
**params: JSONValue,
|
|
989
|
+
) -> List[LocationAccess]:
|
|
990
|
+
return self.list_all(
|
|
991
|
+
self._user_path("/locations/{0}/access".format(location_id)),
|
|
992
|
+
params,
|
|
993
|
+
LocationAccess,
|
|
994
|
+
)
|
|
995
|
+
|
|
996
|
+
def list_portal_location_access(
|
|
997
|
+
self,
|
|
998
|
+
location_id: ResourceId,
|
|
999
|
+
**params: JSONValue,
|
|
1000
|
+
) -> List[LocationAccess]:
|
|
1001
|
+
"""List sharing records through the portal's root location route."""
|
|
1002
|
+
return self.list_all(
|
|
1003
|
+
"/locations/{0}/access".format(location_id), params, LocationAccess
|
|
1004
|
+
)
|
|
1005
|
+
|
|
1006
|
+
def get_location_access(
|
|
1007
|
+
self,
|
|
1008
|
+
location_id: ResourceId,
|
|
1009
|
+
access_id: ResourceId,
|
|
1010
|
+
) -> Optional[LocationAccess]:
|
|
1011
|
+
return self.data_one(
|
|
1012
|
+
"GET",
|
|
1013
|
+
self._user_path("/locations/{0}/access/{1}".format(location_id, access_id)),
|
|
1014
|
+
LocationAccess,
|
|
1015
|
+
)
|
|
1016
|
+
|
|
1017
|
+
def get_portal_location_access(
|
|
1018
|
+
self,
|
|
1019
|
+
location_id: ResourceId,
|
|
1020
|
+
access_id: ResourceId,
|
|
1021
|
+
) -> Optional[LocationAccess]:
|
|
1022
|
+
"""Fetch sharing records through the portal collection GET form."""
|
|
1023
|
+
return self.data_one(
|
|
1024
|
+
"GET",
|
|
1025
|
+
"/locations/{0}/access/{1}".format(location_id, access_id),
|
|
1026
|
+
LocationAccess,
|
|
1027
|
+
)
|
|
1028
|
+
|
|
1029
|
+
def grant_location_access(
|
|
1030
|
+
self, location_id: ResourceId, payload: JSONDict
|
|
1031
|
+
) -> JSONValue:
|
|
1032
|
+
return self.data(
|
|
1033
|
+
"POST",
|
|
1034
|
+
self._user_path("/locations/{0}/access".format(location_id)),
|
|
1035
|
+
json=payload,
|
|
1036
|
+
)
|
|
1037
|
+
|
|
1038
|
+
def grant_portal_location_access(
|
|
1039
|
+
self,
|
|
1040
|
+
location_id: ResourceId,
|
|
1041
|
+
payload: JSONDict,
|
|
1042
|
+
) -> JSONValue:
|
|
1043
|
+
"""Grant sharing access through the portal's root location route."""
|
|
1044
|
+
return self.data(
|
|
1045
|
+
"POST", "/locations/{0}/access".format(location_id), json=payload
|
|
1046
|
+
)
|
|
1047
|
+
|
|
1048
|
+
def revoke_location_access(
|
|
1049
|
+
self,
|
|
1050
|
+
location_id: ResourceId,
|
|
1051
|
+
access_id: ResourceId,
|
|
1052
|
+
) -> JSONValue:
|
|
1053
|
+
return self.data(
|
|
1054
|
+
"DELETE",
|
|
1055
|
+
self._user_path("/locations/{0}/access/{1}".format(location_id, access_id)),
|
|
1056
|
+
)
|
|
1057
|
+
|
|
1058
|
+
def revoke_portal_location_access(
|
|
1059
|
+
self,
|
|
1060
|
+
location_id: ResourceId,
|
|
1061
|
+
access_id: ResourceId,
|
|
1062
|
+
) -> JSONValue:
|
|
1063
|
+
"""Revoke sharing access through the portal collection route."""
|
|
1064
|
+
return self.data(
|
|
1065
|
+
"DELETE", "/locations/{0}/access/{1}".format(location_id, access_id)
|
|
1066
|
+
)
|
|
1067
|
+
|
|
1068
|
+
def list_integrations(
|
|
1069
|
+
self, device_id: ResourceId, **params: JSONValue
|
|
1070
|
+
) -> List[Integration]:
|
|
1071
|
+
return self.list_all(
|
|
1072
|
+
self._user_path("/devices/{0}/integrations".format(device_id)),
|
|
1073
|
+
params,
|
|
1074
|
+
Integration,
|
|
1075
|
+
)
|
|
1076
|
+
|
|
1077
|
+
def list_portal_integrations(
|
|
1078
|
+
self,
|
|
1079
|
+
device_id: ResourceId,
|
|
1080
|
+
**params: JSONValue,
|
|
1081
|
+
) -> List[Integration]:
|
|
1082
|
+
"""List integrations through the portal's user-scoped route."""
|
|
1083
|
+
return self.list_all(
|
|
1084
|
+
self._user_path("/devices/{0}/integrations".format(device_id)),
|
|
1085
|
+
params,
|
|
1086
|
+
Integration,
|
|
1087
|
+
)
|
|
1088
|
+
|
|
1089
|
+
def get_integration(
|
|
1090
|
+
self,
|
|
1091
|
+
device_id: ResourceId,
|
|
1092
|
+
integration_id: ResourceId,
|
|
1093
|
+
) -> Optional[Integration]:
|
|
1094
|
+
return self.data_one(
|
|
1095
|
+
"GET",
|
|
1096
|
+
self._user_path(
|
|
1097
|
+
"/devices/{0}/integrations/{1}".format(device_id, integration_id)
|
|
1098
|
+
),
|
|
1099
|
+
Integration,
|
|
1100
|
+
)
|
|
1101
|
+
|
|
1102
|
+
def get_portal_integration(
|
|
1103
|
+
self,
|
|
1104
|
+
device_id: ResourceId,
|
|
1105
|
+
integration_id: ResourceId,
|
|
1106
|
+
) -> Optional[Integration]:
|
|
1107
|
+
return self.data_one(
|
|
1108
|
+
"GET",
|
|
1109
|
+
self._user_path(
|
|
1110
|
+
"/devices/{0}/integrations/{1}".format(device_id, integration_id)
|
|
1111
|
+
),
|
|
1112
|
+
Integration,
|
|
1113
|
+
)
|
|
1114
|
+
|
|
1115
|
+
def command_integration(
|
|
1116
|
+
self,
|
|
1117
|
+
device_id: ResourceId,
|
|
1118
|
+
integration_id: ResourceId,
|
|
1119
|
+
state: JSONValue,
|
|
1120
|
+
) -> JSONValue:
|
|
1121
|
+
return self.data(
|
|
1122
|
+
"POST",
|
|
1123
|
+
self._user_path(
|
|
1124
|
+
"/devices/{0}/integrations/{1}/command".format(
|
|
1125
|
+
device_id, integration_id
|
|
1126
|
+
)
|
|
1127
|
+
),
|
|
1128
|
+
json={"state": state},
|
|
1129
|
+
)
|
|
1130
|
+
|
|
1131
|
+
def command_portal_integration(
|
|
1132
|
+
self,
|
|
1133
|
+
device_id: ResourceId,
|
|
1134
|
+
integration_id: ResourceId,
|
|
1135
|
+
state: JSONValue,
|
|
1136
|
+
) -> JSONValue:
|
|
1137
|
+
return self.data(
|
|
1138
|
+
"POST",
|
|
1139
|
+
self._user_path(
|
|
1140
|
+
"/devices/{0}/integrations/{1}/command".format(
|
|
1141
|
+
device_id, integration_id
|
|
1142
|
+
)
|
|
1143
|
+
),
|
|
1144
|
+
json={"state": state},
|
|
1145
|
+
)
|
|
1146
|
+
|
|
1147
|
+
def refresh_integration(
|
|
1148
|
+
self,
|
|
1149
|
+
device_id: ResourceId,
|
|
1150
|
+
integration_id: ResourceId,
|
|
1151
|
+
) -> JSONValue:
|
|
1152
|
+
return self.data(
|
|
1153
|
+
"POST",
|
|
1154
|
+
self._user_path(
|
|
1155
|
+
"/devices/{0}/integrations/{1}/refresh".format(
|
|
1156
|
+
device_id, integration_id
|
|
1157
|
+
)
|
|
1158
|
+
),
|
|
1159
|
+
json={},
|
|
1160
|
+
)
|
|
1161
|
+
|
|
1162
|
+
def refresh_portal_integration(
|
|
1163
|
+
self,
|
|
1164
|
+
device_id: ResourceId,
|
|
1165
|
+
integration_id: ResourceId,
|
|
1166
|
+
) -> JSONValue:
|
|
1167
|
+
return self.data(
|
|
1168
|
+
"POST",
|
|
1169
|
+
self._user_path(
|
|
1170
|
+
"/devices/{0}/integrations/{1}/refresh".format(
|
|
1171
|
+
device_id, integration_id
|
|
1172
|
+
)
|
|
1173
|
+
),
|
|
1174
|
+
json={},
|
|
1175
|
+
)
|
|
1176
|
+
|
|
1177
|
+
def delete_integration(
|
|
1178
|
+
self, device_id: ResourceId, integration_id: ResourceId
|
|
1179
|
+
) -> JSONValue:
|
|
1180
|
+
return self.data(
|
|
1181
|
+
"DELETE",
|
|
1182
|
+
self._user_path(
|
|
1183
|
+
"/devices/{0}/integrations/{1}".format(device_id, integration_id)
|
|
1184
|
+
),
|
|
1185
|
+
)
|
|
1186
|
+
|
|
1187
|
+
def unlink_portal_integration(
|
|
1188
|
+
self,
|
|
1189
|
+
device_id: ResourceId,
|
|
1190
|
+
integration_id: Optional[ResourceId] = None,
|
|
1191
|
+
payload: Optional[JSONDict] = None,
|
|
1192
|
+
) -> JSONValue:
|
|
1193
|
+
"""Unlink an integration using the portal's collection DELETE form."""
|
|
1194
|
+
path = self._user_path("/devices/{0}/integrations".format(device_id))
|
|
1195
|
+
if integration_id is not None:
|
|
1196
|
+
path += "/{0}".format(integration_id)
|
|
1197
|
+
return self.data("DELETE", path, json=payload)
|
|
1198
|
+
|
|
1199
|
+
def list_shutoff_integrations(self, device_id: ResourceId) -> List[Integration]:
|
|
1200
|
+
"""List portal shutoff-valve integrations."""
|
|
1201
|
+
return self.list_all(
|
|
1202
|
+
self._user_path("/devices/{0}/integrations".format(device_id)),
|
|
1203
|
+
{"type": "SHUTOFF_VALVE"},
|
|
1204
|
+
Integration,
|
|
1205
|
+
)
|
|
1206
|
+
|
|
1207
|
+
def list_spans(self, device_id: ResourceId, **params: JSONValue) -> List[Span]:
|
|
1208
|
+
return self.list_all(
|
|
1209
|
+
self._user_path("/devices/{0}/spans".format(device_id)), params, Span
|
|
1210
|
+
)
|
|
1211
|
+
|
|
1212
|
+
def update_span(
|
|
1213
|
+
self,
|
|
1214
|
+
device_id: ResourceId,
|
|
1215
|
+
span_id: ResourceId,
|
|
1216
|
+
payload: JSONDict,
|
|
1217
|
+
) -> JSONValue:
|
|
1218
|
+
return self.data(
|
|
1219
|
+
"PATCH",
|
|
1220
|
+
self._user_path("/devices/{0}/spans/{1}".format(device_id, span_id)),
|
|
1221
|
+
json=payload,
|
|
1222
|
+
)
|
|
1223
|
+
|
|
1224
|
+
def update_span_type(
|
|
1225
|
+
self,
|
|
1226
|
+
device_id: ResourceId,
|
|
1227
|
+
span_id: ResourceId,
|
|
1228
|
+
span_type: str,
|
|
1229
|
+
) -> JSONValue:
|
|
1230
|
+
"""Set a span's type using the portal's exact payload shape."""
|
|
1231
|
+
return self.update_span(device_id, span_id, {"type": span_type})
|
|
1232
|
+
|
|
1233
|
+
def list_span_types(
|
|
1234
|
+
self, location_id: ResourceId, **params: JSONValue
|
|
1235
|
+
) -> List[SpanType]:
|
|
1236
|
+
return self.list_all(
|
|
1237
|
+
self._user_path("/locations/{0}/span-types".format(location_id)),
|
|
1238
|
+
params,
|
|
1239
|
+
SpanType,
|
|
1240
|
+
)
|
|
1241
|
+
|
|
1242
|
+
def submit_feedback(self, device_id: ResourceId, payload: JSONDict) -> JSONValue:
|
|
1243
|
+
return self.data(
|
|
1244
|
+
"POST",
|
|
1245
|
+
self._user_path("/devices/{0}/feedback".format(device_id)),
|
|
1246
|
+
json=payload,
|
|
1247
|
+
)
|
|
1248
|
+
|
|
1249
|
+
def submit_device_feedback(
|
|
1250
|
+
self, device_id: ResourceId, payload: JSONDict
|
|
1251
|
+
) -> JSONValue:
|
|
1252
|
+
"""Submit device feedback through the portal route."""
|
|
1253
|
+
return self.submit_feedback(device_id, payload)
|
|
1254
|
+
|
|
1255
|
+
def get_meter_accuracy(self, device_id: ResourceId) -> List[AccuracyResult]:
|
|
1256
|
+
return cast(
|
|
1257
|
+
List[AccuracyResult],
|
|
1258
|
+
self.data(
|
|
1259
|
+
"GET",
|
|
1260
|
+
self._user_path("/devices/{0}/meters/accuracy".format(device_id)),
|
|
1261
|
+
AccuracyResult,
|
|
1262
|
+
),
|
|
1263
|
+
)
|
|
1264
|
+
|
|
1265
|
+
def submit_meter_accuracy(
|
|
1266
|
+
self, device_id: ResourceId, payload: JSONDict
|
|
1267
|
+
) -> JSONValue:
|
|
1268
|
+
return self.data(
|
|
1269
|
+
"POST",
|
|
1270
|
+
self._user_path("/devices/{0}/meters/accuracy".format(device_id)),
|
|
1271
|
+
json=payload,
|
|
1272
|
+
)
|
|
1273
|
+
|
|
1274
|
+
def submit_meter_accuracy_readings(
|
|
1275
|
+
self,
|
|
1276
|
+
device_id: ResourceId,
|
|
1277
|
+
since_datetime: str,
|
|
1278
|
+
until_datetime: str,
|
|
1279
|
+
since_reading: float,
|
|
1280
|
+
until_reading: float,
|
|
1281
|
+
since_image: str,
|
|
1282
|
+
until_image: str,
|
|
1283
|
+
units: str,
|
|
1284
|
+
) -> JSONValue:
|
|
1285
|
+
"""Submit the portal meter-accuracy form payload."""
|
|
1286
|
+
return self.submit_meter_accuracy(
|
|
1287
|
+
device_id,
|
|
1288
|
+
{
|
|
1289
|
+
"since_datetime": since_datetime,
|
|
1290
|
+
"until_datetime": until_datetime,
|
|
1291
|
+
"since_reading": since_reading,
|
|
1292
|
+
"until_reading": until_reading,
|
|
1293
|
+
"since_image": since_image,
|
|
1294
|
+
"until_image": until_image,
|
|
1295
|
+
"units": units,
|
|
1296
|
+
},
|
|
1297
|
+
)
|
|
1298
|
+
|
|
1299
|
+
def initiate_accuracy_conversation(self, payload: JSONDict) -> JSONValue:
|
|
1300
|
+
return self.data(
|
|
1301
|
+
"POST", self._user_path("/initiate-accuracy-conversation"), json=payload
|
|
1302
|
+
)
|
|
1303
|
+
|
|
1304
|
+
def start_accuracy_conversation(
|
|
1305
|
+
self,
|
|
1306
|
+
message: str,
|
|
1307
|
+
since_url: str,
|
|
1308
|
+
since_datetime: str,
|
|
1309
|
+
since_reading: float,
|
|
1310
|
+
until_url: str,
|
|
1311
|
+
until_datetime: str,
|
|
1312
|
+
until_reading: float,
|
|
1313
|
+
units: str,
|
|
1314
|
+
reading_diff: float,
|
|
1315
|
+
queried_diff: float,
|
|
1316
|
+
accuracy: float,
|
|
1317
|
+
) -> JSONValue:
|
|
1318
|
+
"""Start the portal's accuracy-support conversation payload."""
|
|
1319
|
+
return self.initiate_accuracy_conversation(
|
|
1320
|
+
{
|
|
1321
|
+
"message": message,
|
|
1322
|
+
"since_url": since_url,
|
|
1323
|
+
"since_datetime": since_datetime,
|
|
1324
|
+
"since_reading": since_reading,
|
|
1325
|
+
"until_url": until_url,
|
|
1326
|
+
"until_datetime": until_datetime,
|
|
1327
|
+
"until_reading": until_reading,
|
|
1328
|
+
"units": units,
|
|
1329
|
+
"reading_diff": reading_diff,
|
|
1330
|
+
"queried_diff": queried_diff,
|
|
1331
|
+
"accuracy": accuracy,
|
|
1332
|
+
},
|
|
1333
|
+
)
|
|
1334
|
+
|
|
1335
|
+
def list_purchase_options(
|
|
1336
|
+
self,
|
|
1337
|
+
device_id: ResourceId,
|
|
1338
|
+
**params: JSONValue,
|
|
1339
|
+
) -> List[PurchaseOption]:
|
|
1340
|
+
return self.list_all(
|
|
1341
|
+
self._user_path("/devices/{0}/purchase-options".format(device_id)),
|
|
1342
|
+
params,
|
|
1343
|
+
PurchaseOption,
|
|
1344
|
+
)
|
|
1345
|
+
|
|
1346
|
+
def get_purchase_options(self, device_id: ResourceId) -> List[PurchaseOption]:
|
|
1347
|
+
"""Fetch device purchase options through the portal route."""
|
|
1348
|
+
return self.list_purchase_options(device_id)
|
|
1349
|
+
|
|
1350
|
+
def list_pro_services(self, **params: JSONValue) -> List[ProService]:
|
|
1351
|
+
return self.list_all("/pro-services", params, ProService)
|
|
1352
|
+
|
|
1353
|
+
def list_insurers(self, **params: JSONValue) -> List[Insurer]:
|
|
1354
|
+
return self.list_all("/insurers", params, Insurer)
|
|
1355
|
+
|
|
1356
|
+
def list_portal_insurers(self, **params: JSONValue) -> List[Insurer]:
|
|
1357
|
+
"""List insurers through the portal root route."""
|
|
1358
|
+
return self.list_all("/insurers", params, Insurer)
|
|
1359
|
+
|
|
1360
|
+
def list_clients(self, **params: JSONValue) -> List[ApiClient]:
|
|
1361
|
+
return self.list_all("/clients", params, ApiClient)
|
|
1362
|
+
|
|
1363
|
+
def list_portal_clients(self, **params: JSONValue) -> List[ApiClient]:
|
|
1364
|
+
"""List API clients through the portal root route."""
|
|
1365
|
+
return self.list_all("/clients", params, ApiClient)
|
|
1366
|
+
|
|
1367
|
+
def create_client(self, payload: Optional[JSONDict] = None) -> JSONValue:
|
|
1368
|
+
return self.data("POST", "/clients", json=payload or {})
|
|
1369
|
+
|
|
1370
|
+
def generate_api_client(self) -> JSONValue:
|
|
1371
|
+
"""Generate a portal API client using the portal's empty payload."""
|
|
1372
|
+
return self.create_client({})
|
|
1373
|
+
|
|
1374
|
+
def get_contact_info(self, **params: JSONValue) -> List[Contact]:
|
|
1375
|
+
return self.list_all("/contacts", params, Contact)
|
|
1376
|
+
|
|
1377
|
+
def raw(self, method: str, path: str, **kwargs: Any) -> JSONDict:
|
|
1378
|
+
"""Call any current or future Flume endpoint without a new wrapper."""
|
|
1379
|
+
return self.request(method, path, **kwargs)
|
|
1380
|
+
|
|
1381
|
+
def raw_response(self, method: str, path: str, **kwargs: Any) -> Response:
|
|
1382
|
+
"""Call an endpoint and return the underlying Requests response."""
|
|
1383
|
+
self.request(method, path, **kwargs)
|
|
1384
|
+
assert self.last_response is not None
|
|
1385
|
+
return self.last_response
|