| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import base64 |
| 4 |
import json |
| 5 |
from typing import TYPE_CHECKING, Callable |
| 6 |
|
| 7 |
from consul.exceptions import ACLDisabled, ACLPermissionDenied, BadRequest, ClientError, ConsulException, NotFound |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from consul.base import Response |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class CB: |
| 17 |
@classmethod |
| 18 |
def _status(cls, response: Response, allow_404: bool = True) -> None: |
| 19 |
|
| 20 |
if 400 <= response.code < 500: |
| 21 |
if response.code == 400: |
| 22 |
raise BadRequest(f"{response.code} {response.body}") |
| 23 |
if response.code == 401: |
| 24 |
raise ACLDisabled(response.body) |
| 25 |
if response.code == 403: |
| 26 |
raise ACLPermissionDenied(response.body) |
| 27 |
if response.code == 404: |
| 28 |
if not allow_404: |
| 29 |
raise NotFound(response.body) |
| 30 |
else: |
| 31 |
raise ClientError(f"{response.code} {response.body}") |
| 32 |
elif 500 <= response.code < 600: |
| 33 |
raise ConsulException(f"{response.code} {response.body}") |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def boolean(cls) -> Callable[[Response], bool]: |
| 37 |
|
| 38 |
def cb(response): |
| 39 |
CB._status(response) |
| 40 |
return response.code == 200 |
| 41 |
|
| 42 |
return cb |
| 43 |
|
| 44 |
@classmethod |
| 45 |
def json( |
| 46 |
cls, |
| 47 |
postprocess=None, |
| 48 |
allow_404: bool = True, |
| 49 |
one: bool = False, |
| 50 |
decode: bool | str = False, |
| 51 |
is_id: bool = False, |
| 52 |
index: bool = False, |
| 53 |
): |
| 54 |
""" |
| 55 |
*postprocess* is a function to apply to the final result. |
| 56 |
|
| 57 |
*allow_404* if set, None will be returned on 404, instead of raising |
| 58 |
NotFound. |
| 59 |
|
| 60 |
*index* if set, a tuple of index, data will be returned. |
| 61 |
|
| 62 |
*one* returns only the first item of the list of items. empty lists are |
| 63 |
coerced to None. |
| 64 |
|
| 65 |
*decode* if specified this key will be base64 decoded. |
| 66 |
|
| 67 |
*is_id* only the 'ID' field of the json object will be returned. |
| 68 |
""" |
| 69 |
|
| 70 |
def cb(response): |
| 71 |
CB._status(response, allow_404=allow_404) |
| 72 |
if response.code == 404: |
| 73 |
data = None |
| 74 |
else: |
| 75 |
try: |
| 76 |
data = json.loads(response.body) |
| 77 |
if decode: |
| 78 |
for item in data: |
| 79 |
if item.get(decode) is not None: |
| 80 |
item[decode] = base64.b64decode(item[decode]) |
| 81 |
if is_id: |
| 82 |
data = data["ID"] |
| 83 |
if one and isinstance(data, list): |
| 84 |
data = data[0] if data else None |
| 85 |
if postprocess: |
| 86 |
data = postprocess(data) |
| 87 |
except (json.JSONDecodeError, TypeError, KeyError) as e: |
| 88 |
raise ConsulException(f"Failed to decode JSON: {response.body} {e}") from e |
| 89 |
if index: |
| 90 |
if "X-Consul-Index" not in response.headers: |
| 91 |
raise ConsulException(f"Missing index header: {response.headers}") |
| 92 |
return response.headers["X-Consul-Index"], data |
| 93 |
return data |
| 94 |
|
| 95 |
return cb |