|
1 |
| -from typing import Any, Dict, Iterator, Optional, Tuple |
| 1 | +from typing import Any, Iterator, Optional, Tuple |
2 | 2 |
|
| 3 | +from arangoasync.typings import Json |
3 | 4 |
|
4 |
| -class Wrapper: |
5 |
| - """Wrapper over server response objects.""" |
6 | 5 |
|
7 |
| - def __init__(self, data: Dict[str, Any]) -> None: |
| 6 | +class JsonWrapper: |
| 7 | + """Wrapper over server request/response objects.""" |
| 8 | + |
| 9 | + def __init__(self, data: Json) -> None: |
8 | 10 | self._data = data
|
9 | 11 |
|
10 | 12 | def __getitem__(self, key: str) -> Any:
|
@@ -42,9 +44,88 @@ def items(self) -> Iterator[Tuple[str, Any]]:
|
42 | 44 | """Return an iterator over the dictionary’s key-value pairs."""
|
43 | 45 | return iter(self._data.items())
|
44 | 46 |
|
| 47 | + def to_dict(self) -> Json: |
| 48 | + """Return the dictionary.""" |
| 49 | + return self._data |
| 50 | + |
| 51 | + |
| 52 | +class KeyOptions(JsonWrapper): |
| 53 | + """Additional options for key generation, used on collections. |
| 54 | +
|
| 55 | + https://docs.arangodb.com/stable/develop/http-api/collections/#create-a-collection_body_keyOptions |
| 56 | +
|
| 57 | + Example: |
| 58 | + .. code-block:: json |
| 59 | +
|
| 60 | + "keyOptions": { |
| 61 | + "type": "autoincrement", |
| 62 | + "increment": 5, |
| 63 | + "allowUserKeys": true |
| 64 | + } |
45 | 65 |
|
46 |
| -class ServerStatusInformation(Wrapper): |
| 66 | + Args: |
| 67 | + data (dict | None): Key options. If this parameter is specified, the |
| 68 | + other parameters are ignored. |
| 69 | + allow_user_keys (bool): If set to `True`, then you are allowed to supply own |
| 70 | + key values in the `_key` attribute of documents. If set to `False`, then |
| 71 | + the key generator is solely responsible for generating keys and an error |
| 72 | + is raised if you supply own key values in the `_key` attribute of |
| 73 | + documents. |
| 74 | + generator_type (str): Specifies the type of the key generator. The currently |
| 75 | + available generators are "traditional", "autoincrement", "uuid" and |
| 76 | + "padded". |
| 77 | + increment (int | None): The increment value for the "autoincrement" key |
| 78 | + generator. Not allowed for other key generator types. |
| 79 | + offset (int | None): The initial offset value for the "autoincrement" key |
| 80 | + generator. Not allowed for other key generator types. |
47 | 81 | """
|
| 82 | + |
| 83 | + def __init__( |
| 84 | + self, |
| 85 | + data: Optional[Json] = None, |
| 86 | + allow_user_keys: bool = True, |
| 87 | + generator_type: str = "traditional", |
| 88 | + increment: Optional[int] = None, |
| 89 | + offset: Optional[int] = None, |
| 90 | + ) -> None: |
| 91 | + if data is None: |
| 92 | + data = { |
| 93 | + "allowUserKeys": allow_user_keys, |
| 94 | + "type": generator_type, |
| 95 | + } |
| 96 | + if increment is not None: |
| 97 | + data["increment"] = increment |
| 98 | + if offset is not None: |
| 99 | + data["offset"] = offset |
| 100 | + super().__init__(data) |
| 101 | + |
| 102 | + def validate(self) -> None: |
| 103 | + """Validate key options.""" |
| 104 | + if "type" not in self: |
| 105 | + raise ValueError('"type" value is required for key options') |
| 106 | + if "allowUserKeys" not in self: |
| 107 | + raise ValueError('"allowUserKeys" value is required for key options') |
| 108 | + |
| 109 | + allowed_types = {"autoincrement", "uuid", "padded", "traditional"} |
| 110 | + if self["type"] not in allowed_types: |
| 111 | + raise ValueError( |
| 112 | + f"Invalid key generator type '{self['type']}', " |
| 113 | + f"expected one of {allowed_types}" |
| 114 | + ) |
| 115 | + |
| 116 | + if self.get("increment") is not None and self["type"] != "autoincrement": |
| 117 | + raise ValueError( |
| 118 | + '"increment" value is only allowed for "autoincrement" ' "key generator" |
| 119 | + ) |
| 120 | + if self.get("offset") is not None and self["type"] != "autoincrement": |
| 121 | + raise ValueError( |
| 122 | + '"offset" value is only allowed for "autoincrement" ' "key generator" |
| 123 | + ) |
| 124 | + |
| 125 | + |
| 126 | +class ServerStatusInformation(JsonWrapper): |
| 127 | + """Status information about the server. |
| 128 | +
|
48 | 129 | https://docs.arangodb.com/stable/develop/http-api/administration/#get-server-status-information
|
49 | 130 |
|
50 | 131 | Example:
|
@@ -92,7 +173,7 @@ class ServerStatusInformation(Wrapper):
|
92 | 173 | }
|
93 | 174 | """
|
94 | 175 |
|
95 |
| - def __init__(self, data: Dict[str, Any]) -> None: |
| 176 | + def __init__(self, data: Json) -> None: |
96 | 177 | super().__init__(data)
|
97 | 178 |
|
98 | 179 | @property
|
@@ -132,13 +213,13 @@ def hostname(self) -> Optional[str]:
|
132 | 213 | return self._data.get("hostname")
|
133 | 214 |
|
134 | 215 | @property
|
135 |
| - def server_info(self) -> Optional[Dict[str, Any]]: |
| 216 | + def server_info(self) -> Optional[Json]: |
136 | 217 | return self._data.get("serverInfo")
|
137 | 218 |
|
138 | 219 | @property
|
139 |
| - def coordinator(self) -> Optional[Dict[str, Any]]: |
| 220 | + def coordinator(self) -> Optional[Json]: |
140 | 221 | return self._data.get("coordinator")
|
141 | 222 |
|
142 | 223 | @property
|
143 |
| - def agency(self) -> Optional[Dict[str, Any]]: |
| 224 | + def agency(self) -> Optional[Json]: |
144 | 225 | return self._data.get("agency")
|
0 commit comments