hexdb 2.3.2__tar.gz → 2.3.3__tar.gz
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.
- hexdb-2.3.3/PKG-INFO +36 -0
- hexdb-2.3.3/README.md +23 -0
- {hexdb-2.3.2 → hexdb-2.3.3}/hexdb/__init__.py +26 -5
- hexdb-2.3.3/hexdb/connection_uri.py +174 -0
- hexdb-2.3.3/hexdb.egg-info/PKG-INFO +36 -0
- {hexdb-2.3.2 → hexdb-2.3.3}/hexdb.egg-info/SOURCES.txt +3 -1
- {hexdb-2.3.2 → hexdb-2.3.3}/pyproject.toml +1 -1
- {hexdb-2.3.2 → hexdb-2.3.3}/tests/test_client.py +3 -2
- hexdb-2.3.3/tests/test_connection_uri.py +113 -0
- hexdb-2.3.2/PKG-INFO +0 -16
- hexdb-2.3.2/README.md +0 -3
- hexdb-2.3.2/hexdb.egg-info/PKG-INFO +0 -16
- {hexdb-2.3.2 → hexdb-2.3.3}/hexdb.egg-info/dependency_links.txt +0 -0
- {hexdb-2.3.2 → hexdb-2.3.3}/hexdb.egg-info/top_level.txt +0 -0
- {hexdb-2.3.2 → hexdb-2.3.3}/setup.cfg +0 -0
hexdb-2.3.3/PKG-INFO
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hexdb
|
|
3
|
+
Version: 2.3.3
|
|
4
|
+
Summary: Client for HexDB, the hexagonal document database.
|
|
5
|
+
Author: Dream In Hex
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://hexdb.app
|
|
8
|
+
Project-URL: Source, https://github.com/dreaminhex/hexdb
|
|
9
|
+
Project-URL: Issues, https://github.com/dreaminhex/hexdb/issues
|
|
10
|
+
Keywords: hexdb,database,document,nosql,client
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# hexdb (Python)
|
|
15
|
+
|
|
16
|
+
Client for HexDB. Python 3.9+, standard library only. See ../README.md and the HexDB manual.
|
|
17
|
+
|
|
18
|
+
## Connection URIs
|
|
19
|
+
|
|
20
|
+
`HexDB` takes a connection URI wherever it takes a URL:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from hexdb import HexDB, parse_connection_uri
|
|
24
|
+
|
|
25
|
+
db = HexDB("hexdbs://analyst:s3cret%21@db.example.com") # signs in with POST /auth/login
|
|
26
|
+
db = HexDB("hexdb://127.0.0.1:7700?apiKey=hxk_...&timeout=10")
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- `hexdb://` is HTTP, port 7700 by default. `hexdbs://` is HTTPS, port 443 by default.
|
|
30
|
+
- A login in the URI signs in when `HexDB` is created. Percent-encode `@`, `:` and `/` in the login and password.
|
|
31
|
+
- The `apiKey`, `timeout` and `caFile` options set the API key, the request timeout in seconds and the CA file. The `api_key`, `timeout` and `ca_file` arguments win over them; an explicit `api_key` also skips the sign-in. `pageSize` is parsed but not used by this driver.
|
|
32
|
+
- A path is kept as the base of every request (`hexdbs://example.com/hexdb`).
|
|
33
|
+
- An invalid URI raises `ValueError` naming the problem.
|
|
34
|
+
- Plain `http://` and `https://` URLs work as before.
|
|
35
|
+
|
|
36
|
+
`parse_connection_uri(uri)` returns a `ConnectionUri` with `url`, `login`, `password`, `api_key`, `timeout_seconds`, `page_size` and `ca_file` (None when absent). See "Connection URIs" in the HexDB manual for the full format.
|
hexdb-2.3.3/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# hexdb (Python)
|
|
2
|
+
|
|
3
|
+
Client for HexDB. Python 3.9+, standard library only. See ../README.md and the HexDB manual.
|
|
4
|
+
|
|
5
|
+
## Connection URIs
|
|
6
|
+
|
|
7
|
+
`HexDB` takes a connection URI wherever it takes a URL:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from hexdb import HexDB, parse_connection_uri
|
|
11
|
+
|
|
12
|
+
db = HexDB("hexdbs://analyst:s3cret%21@db.example.com") # signs in with POST /auth/login
|
|
13
|
+
db = HexDB("hexdb://127.0.0.1:7700?apiKey=hxk_...&timeout=10")
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
- `hexdb://` is HTTP, port 7700 by default. `hexdbs://` is HTTPS, port 443 by default.
|
|
17
|
+
- A login in the URI signs in when `HexDB` is created. Percent-encode `@`, `:` and `/` in the login and password.
|
|
18
|
+
- The `apiKey`, `timeout` and `caFile` options set the API key, the request timeout in seconds and the CA file. The `api_key`, `timeout` and `ca_file` arguments win over them; an explicit `api_key` also skips the sign-in. `pageSize` is parsed but not used by this driver.
|
|
19
|
+
- A path is kept as the base of every request (`hexdbs://example.com/hexdb`).
|
|
20
|
+
- An invalid URI raises `ValueError` naming the problem.
|
|
21
|
+
- Plain `http://` and `https://` URLs work as before.
|
|
22
|
+
|
|
23
|
+
`parse_connection_uri(uri)` returns a `ConnectionUri` with `url`, `login`, `password`, `api_key`, `timeout_seconds`, `page_size` and `ca_file` (None when absent). See "Connection URIs" in the HexDB manual for the full format.
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from hexdb import HexDB
|
|
4
4
|
|
|
5
5
|
db = HexDB("http://127.0.0.1:7700", api_key=os.environ["HEXDB_API_KEY"])
|
|
6
|
+
db = HexDB("hexdbs://ada:s3cret@db.example.com") # a connection URI; signs in
|
|
6
7
|
orders = db.tessellation("orders")
|
|
7
8
|
doc = orders.insert({"customer": "ada", "total": 12})
|
|
8
9
|
page = orders.query(filter={"total": {"$gt": 10}}, sort="-total")
|
|
@@ -25,8 +26,10 @@ import urllib.parse
|
|
|
25
26
|
import urllib.request
|
|
26
27
|
from typing import Any, BinaryIO, Callable, Dict, Iterator, List, Optional, Sequence, Tuple, Union
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
from .connection_uri import ConnectionUri, is_connection_uri, parse_connection_uri
|
|
30
|
+
|
|
31
|
+
__all__ = ["HexDB", "HexDBError", "Tessellation", "Stream", "Files", "ConnectionUri", "parse_connection_uri"]
|
|
32
|
+
__version__ = "2.3.3"
|
|
30
33
|
|
|
31
34
|
Json = Any
|
|
32
35
|
Document = Dict[str, Any]
|
|
@@ -67,7 +70,12 @@ def _quote(text: str) -> str:
|
|
|
67
70
|
|
|
68
71
|
|
|
69
72
|
class HexDB:
|
|
70
|
-
"""A connection to a HexDB server (any hex; replicas forward writes).
|
|
73
|
+
"""A connection to a HexDB server (any hex; replicas forward writes).
|
|
74
|
+
|
|
75
|
+
`url` is an http(s) URL or a hexdb:// / hexdbs:// connection URI. A URI with a login
|
|
76
|
+
signs in here (POST /auth/login); its apiKey, timeout and caFile options apply unless
|
|
77
|
+
the matching argument is given, which wins. An explicit `api_key` also skips the sign-in.
|
|
78
|
+
"""
|
|
71
79
|
|
|
72
80
|
def __init__(
|
|
73
81
|
self,
|
|
@@ -75,18 +83,31 @@ class HexDB:
|
|
|
75
83
|
api_key: Optional[str] = None,
|
|
76
84
|
*,
|
|
77
85
|
retries: int = 3,
|
|
78
|
-
timeout: float =
|
|
86
|
+
timeout: Optional[float] = None,
|
|
79
87
|
ca_file: Optional[str] = None,
|
|
80
88
|
):
|
|
89
|
+
uri = parse_connection_uri(url) if is_connection_uri(url) else None
|
|
90
|
+
if uri is not None:
|
|
91
|
+
url = uri.url
|
|
92
|
+
if api_key is None:
|
|
93
|
+
api_key = uri.api_key
|
|
94
|
+
if timeout is None:
|
|
95
|
+
timeout = uri.timeout_seconds
|
|
96
|
+
if ca_file is None:
|
|
97
|
+
ca_file = uri.ca_file
|
|
81
98
|
self.base = url.rstrip("/")
|
|
82
99
|
self.token = api_key
|
|
83
100
|
self.retries = retries
|
|
84
|
-
self.timeout = timeout
|
|
101
|
+
self.timeout = timeout or 30.0
|
|
85
102
|
self._context = ssl.create_default_context(cafile=ca_file) if ca_file else None
|
|
86
103
|
handlers: List[Any] = [_SameOriginAuth()]
|
|
87
104
|
if self._context is not None:
|
|
88
105
|
handlers.append(urllib.request.HTTPSHandler(context=self._context))
|
|
89
106
|
self._opener = urllib.request.build_opener(*handlers)
|
|
107
|
+
if uri is not None and uri.login is not None and api_key is None:
|
|
108
|
+
if uri.password is None:
|
|
109
|
+
raise ValueError("The connection URI has a login but no password; use an API key for accounts without one")
|
|
110
|
+
self.login(uri.login, uri.password)
|
|
90
111
|
|
|
91
112
|
# -- transport ---------------------------------------------------------
|
|
92
113
|
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""Connection URIs: hexdb://[login[:password]@]host[:port][/path][?option=value&...].
|
|
2
|
+
|
|
3
|
+
`hexdb://` is HTTP (port 7700 by default), `hexdbs://` is HTTPS (port 443 by default).
|
|
4
|
+
See "Connection URIs" in the HexDB manual.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import Dict, List, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
__all__ = ["ConnectionUri", "parse_connection_uri", "is_connection_uri"]
|
|
13
|
+
|
|
14
|
+
_SCHEMES = {"hexdb": ("http", 7700), "hexdbs": ("https", 443)}
|
|
15
|
+
_HEX = "0123456789abcdefABCDEF"
|
|
16
|
+
_HOST_CHARS = set("abcdefghijklmnopqrstuvwxyz0123456789-._")
|
|
17
|
+
_IPV6_CHARS = set("0123456789abcdef:.")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class ConnectionUri:
|
|
22
|
+
"""A parsed connection URI. Absent parts are None; `url` has no trailing slash."""
|
|
23
|
+
|
|
24
|
+
url: str
|
|
25
|
+
login: Optional[str] = None
|
|
26
|
+
password: Optional[str] = None
|
|
27
|
+
api_key: Optional[str] = None
|
|
28
|
+
timeout_seconds: Optional[int] = None
|
|
29
|
+
page_size: Optional[int] = None
|
|
30
|
+
ca_file: Optional[str] = None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def is_connection_uri(text: str) -> bool:
|
|
34
|
+
"""True when `text` starts with the hexdb: or hexdbs: scheme (any case)."""
|
|
35
|
+
scheme, sep, _ = text.partition(":")
|
|
36
|
+
return bool(sep) and scheme.lower() in _SCHEMES
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _fail(problem: str) -> ValueError:
|
|
40
|
+
return ValueError(f"Invalid HexDB connection URI: {problem}")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _decode(text: str, what: str) -> str:
|
|
44
|
+
"""Percent-decode `text` strictly: every % starts two hex digits, and the bytes are UTF-8."""
|
|
45
|
+
if "%" not in text:
|
|
46
|
+
return text
|
|
47
|
+
out = bytearray()
|
|
48
|
+
i = 0
|
|
49
|
+
while i < len(text):
|
|
50
|
+
c = text[i]
|
|
51
|
+
if c == "%":
|
|
52
|
+
pair = text[i + 1 : i + 3]
|
|
53
|
+
if len(pair) != 2 or pair[0] not in _HEX or pair[1] not in _HEX:
|
|
54
|
+
raise _fail(f"{what} has a % that isn't followed by two hex digits")
|
|
55
|
+
out.append(int(pair, 16))
|
|
56
|
+
i += 3
|
|
57
|
+
else:
|
|
58
|
+
out.extend(c.encode("utf-8"))
|
|
59
|
+
i += 1
|
|
60
|
+
try:
|
|
61
|
+
return out.decode("utf-8")
|
|
62
|
+
except UnicodeDecodeError:
|
|
63
|
+
raise _fail(f"{what} isn't valid UTF-8 once percent-decoded") from None
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _int_option(name: str, value: str, low: int, high: Optional[int]) -> int:
|
|
67
|
+
if not value.isascii() or not value.isdigit():
|
|
68
|
+
raise _fail(f"{name} isn't a positive integer: {value!r}")
|
|
69
|
+
number = int(value)
|
|
70
|
+
if number < low or (high is not None and number > high):
|
|
71
|
+
if high is None:
|
|
72
|
+
raise _fail(f"{name} isn't a positive integer: {value!r}")
|
|
73
|
+
raise _fail(f"{name} is outside {low} to {high:,}: {value}")
|
|
74
|
+
return number
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _host_port(hostport: str) -> Tuple[str, Optional[str]]:
|
|
78
|
+
"""Split host[:port], with IPv6 in brackets. Returns (host, port text or None)."""
|
|
79
|
+
if "," in hostport:
|
|
80
|
+
raise _fail("more than one host; give one host (a name that reaches several hexes)")
|
|
81
|
+
if hostport.startswith("["):
|
|
82
|
+
end = hostport.find("]")
|
|
83
|
+
if end < 0:
|
|
84
|
+
raise _fail("an IPv6 address has no closing ]")
|
|
85
|
+
address = hostport[1:end].lower()
|
|
86
|
+
if not address or not set(address) <= _IPV6_CHARS or ":" not in address:
|
|
87
|
+
raise _fail(f"the IPv6 address [{address}] isn't valid")
|
|
88
|
+
rest = hostport[end + 1 :]
|
|
89
|
+
if rest and not rest.startswith(":"):
|
|
90
|
+
raise _fail(f"unexpected {rest!r} after the IPv6 address")
|
|
91
|
+
return f"[{address}]", (rest[1:] if rest else None)
|
|
92
|
+
host, sep, port = hostport.partition(":")
|
|
93
|
+
if sep and ":" in port:
|
|
94
|
+
raise _fail("an IPv6 host must be in brackets, as in [::1]")
|
|
95
|
+
host = host.lower()
|
|
96
|
+
if not host:
|
|
97
|
+
raise _fail("no host")
|
|
98
|
+
if not set(host) <= _HOST_CHARS:
|
|
99
|
+
raise _fail(f"the host {host!r} has characters a host name can't have")
|
|
100
|
+
return host, (port if sep else None)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def parse_connection_uri(uri: str) -> ConnectionUri:
|
|
104
|
+
"""Parse a hexdb:// or hexdbs:// URI. Raises ValueError naming the problem when it isn't valid."""
|
|
105
|
+
if not isinstance(uri, str):
|
|
106
|
+
raise TypeError("a connection URI is a string")
|
|
107
|
+
scheme, sep, rest = uri.partition(":")
|
|
108
|
+
scheme = scheme.lower()
|
|
109
|
+
if not sep or scheme not in _SCHEMES:
|
|
110
|
+
raise _fail("not a hexdb:// or hexdbs:// URI")
|
|
111
|
+
if not rest.startswith("//"):
|
|
112
|
+
raise _fail(f"no // after {scheme}:")
|
|
113
|
+
rest = rest[2:]
|
|
114
|
+
if "#" in rest:
|
|
115
|
+
raise _fail("a fragment (#...) isn't allowed")
|
|
116
|
+
rest, _, query = rest.partition("?")
|
|
117
|
+
slash = rest.find("/")
|
|
118
|
+
authority, path = (rest, "") if slash < 0 else (rest[:slash], rest[slash:])
|
|
119
|
+
|
|
120
|
+
login: Optional[str] = None
|
|
121
|
+
password: Optional[str] = None
|
|
122
|
+
at = authority.rfind("@")
|
|
123
|
+
if at >= 0:
|
|
124
|
+
userinfo, authority = authority[:at], authority[at + 1 :]
|
|
125
|
+
raw_login, has_password, raw_password = userinfo.partition(":")
|
|
126
|
+
login = _decode(raw_login, "the login")
|
|
127
|
+
if not login:
|
|
128
|
+
raise _fail("the login before @ is empty")
|
|
129
|
+
if has_password:
|
|
130
|
+
password = _decode(raw_password, "the password")
|
|
131
|
+
|
|
132
|
+
if not authority:
|
|
133
|
+
raise _fail("no host")
|
|
134
|
+
host, port_text = _host_port(authority)
|
|
135
|
+
http_scheme, default_port = _SCHEMES[scheme]
|
|
136
|
+
port = default_port
|
|
137
|
+
if port_text is not None:
|
|
138
|
+
if not port_text:
|
|
139
|
+
raise _fail("the port after : is empty")
|
|
140
|
+
if not port_text.isascii() or not port_text.isdigit():
|
|
141
|
+
raise _fail(f"the port isn't a number: {port_text!r}")
|
|
142
|
+
port = int(port_text)
|
|
143
|
+
if not 1 <= port <= 65535:
|
|
144
|
+
raise _fail(f"the port is outside 1 to 65535: {port}")
|
|
145
|
+
|
|
146
|
+
options: Dict[str, str] = {}
|
|
147
|
+
pieces: List[str] = [p for p in query.split("&") if p] if query else []
|
|
148
|
+
for piece in pieces:
|
|
149
|
+
raw_name, _, raw_value = piece.partition("=")
|
|
150
|
+
name = _decode(raw_name, "an option name").lower()
|
|
151
|
+
options[name] = _decode(raw_value, f"the {name} option")
|
|
152
|
+
|
|
153
|
+
api_key = options.get("apikey")
|
|
154
|
+
if api_key is not None and not api_key:
|
|
155
|
+
raise _fail("apiKey is empty")
|
|
156
|
+
if api_key is not None and login is not None:
|
|
157
|
+
raise _fail("a login and apiKey together; use one or the other")
|
|
158
|
+
timeout = options.get("timeout")
|
|
159
|
+
page_size = options.get("pagesize")
|
|
160
|
+
ca_file = options.get("cafile")
|
|
161
|
+
if ca_file is not None and not ca_file:
|
|
162
|
+
raise _fail("caFile is empty")
|
|
163
|
+
|
|
164
|
+
path = path.rstrip("/")
|
|
165
|
+
port_part = "" if http_scheme == "https" and port == 443 else f":{port}"
|
|
166
|
+
return ConnectionUri(
|
|
167
|
+
url=f"{http_scheme}://{host}{port_part}{path}",
|
|
168
|
+
login=login,
|
|
169
|
+
password=password,
|
|
170
|
+
api_key=api_key,
|
|
171
|
+
timeout_seconds=None if timeout is None else _int_option("timeout", timeout, 1, None),
|
|
172
|
+
page_size=None if page_size is None else _int_option("pageSize", page_size, 1, 10_000),
|
|
173
|
+
ca_file=ca_file,
|
|
174
|
+
)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: hexdb
|
|
3
|
+
Version: 2.3.3
|
|
4
|
+
Summary: Client for HexDB, the hexagonal document database.
|
|
5
|
+
Author: Dream In Hex
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://hexdb.app
|
|
8
|
+
Project-URL: Source, https://github.com/dreaminhex/hexdb
|
|
9
|
+
Project-URL: Issues, https://github.com/dreaminhex/hexdb/issues
|
|
10
|
+
Keywords: hexdb,database,document,nosql,client
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# hexdb (Python)
|
|
15
|
+
|
|
16
|
+
Client for HexDB. Python 3.9+, standard library only. See ../README.md and the HexDB manual.
|
|
17
|
+
|
|
18
|
+
## Connection URIs
|
|
19
|
+
|
|
20
|
+
`HexDB` takes a connection URI wherever it takes a URL:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from hexdb import HexDB, parse_connection_uri
|
|
24
|
+
|
|
25
|
+
db = HexDB("hexdbs://analyst:s3cret%21@db.example.com") # signs in with POST /auth/login
|
|
26
|
+
db = HexDB("hexdb://127.0.0.1:7700?apiKey=hxk_...&timeout=10")
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
- `hexdb://` is HTTP, port 7700 by default. `hexdbs://` is HTTPS, port 443 by default.
|
|
30
|
+
- A login in the URI signs in when `HexDB` is created. Percent-encode `@`, `:` and `/` in the login and password.
|
|
31
|
+
- The `apiKey`, `timeout` and `caFile` options set the API key, the request timeout in seconds and the CA file. The `api_key`, `timeout` and `ca_file` arguments win over them; an explicit `api_key` also skips the sign-in. `pageSize` is parsed but not used by this driver.
|
|
32
|
+
- A path is kept as the base of every request (`hexdbs://example.com/hexdb`).
|
|
33
|
+
- An invalid URI raises `ValueError` naming the problem.
|
|
34
|
+
- Plain `http://` and `https://` URLs work as before.
|
|
35
|
+
|
|
36
|
+
`parse_connection_uri(uri)` returns a `ConnectionUri` with `url`, `login`, `password`, `api_key`, `timeout_seconds`, `page_size` and `ca_file` (None when absent). See "Connection URIs" in the HexDB manual for the full format.
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
README.md
|
|
2
2
|
pyproject.toml
|
|
3
3
|
hexdb/__init__.py
|
|
4
|
+
hexdb/connection_uri.py
|
|
4
5
|
hexdb.egg-info/PKG-INFO
|
|
5
6
|
hexdb.egg-info/SOURCES.txt
|
|
6
7
|
hexdb.egg-info/dependency_links.txt
|
|
7
8
|
hexdb.egg-info/top_level.txt
|
|
8
|
-
tests/test_client.py
|
|
9
|
+
tests/test_client.py
|
|
10
|
+
tests/test_connection_uri.py
|
|
@@ -213,6 +213,7 @@ class RedirectTests(unittest.TestCase):
|
|
|
213
213
|
client.request("GET", "/elsewhere")
|
|
214
214
|
self.assertEqual(seen["first"], "Bearer hxk_secret")
|
|
215
215
|
self.assertIsNone(seen["other"], "dropped for another server")
|
|
216
|
-
first
|
|
217
|
-
|
|
216
|
+
for httpd in (first, other):
|
|
217
|
+
httpd.shutdown()
|
|
218
|
+
httpd.server_close()
|
|
218
219
|
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""Connection URIs: the shared test vectors, and HexDB(uri) signing in. No server needed."""
|
|
2
|
+
|
|
3
|
+
import http.server
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import threading
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
from hexdb import ConnectionUri, HexDB, parse_connection_uri
|
|
10
|
+
|
|
11
|
+
VECTORS = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "testing", "connection-uris.json")
|
|
12
|
+
|
|
13
|
+
# A word from each vector's reason that the error message must contain.
|
|
14
|
+
NAMED = {
|
|
15
|
+
"mongodb://localhost:27017": "hexdb://",
|
|
16
|
+
"hexdb://": "no host",
|
|
17
|
+
"hexdb://:7700": "no host",
|
|
18
|
+
"hexdb://ada:pw@localhost?apiKey=hxk_1": "login and apiKey",
|
|
19
|
+
"hexdb://localhost?timeout=abc": "timeout",
|
|
20
|
+
"hexdb://localhost?timeout=0": "timeout",
|
|
21
|
+
"hexdb://localhost?pageSize=0": "pageSize",
|
|
22
|
+
"hexdb://localhost?pageSize=10001": "pageSize",
|
|
23
|
+
"hexdb://localhost:0": "port",
|
|
24
|
+
"hexdb://localhost:65536": "port",
|
|
25
|
+
"hexdb://localhost:abc": "port",
|
|
26
|
+
"hexdb://a.example.com,b.example.com": "more than one host",
|
|
27
|
+
"hexdb://localhost#section": "fragment",
|
|
28
|
+
"hexdb://localhost?apiKey=": "apiKey",
|
|
29
|
+
"hexdb:localhost": "//",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def load_vectors():
|
|
34
|
+
with open(VECTORS, encoding="utf-8") as f:
|
|
35
|
+
return json.load(f)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class VectorTests(unittest.TestCase):
|
|
39
|
+
def test_valid(self):
|
|
40
|
+
for v in load_vectors()["valid"]:
|
|
41
|
+
with self.subTest(uri=v["uri"]):
|
|
42
|
+
self.assertEqual(
|
|
43
|
+
parse_connection_uri(v["uri"]),
|
|
44
|
+
ConnectionUri(
|
|
45
|
+
url=v["url"],
|
|
46
|
+
login=v["login"],
|
|
47
|
+
password=v["password"],
|
|
48
|
+
api_key=v["apiKey"],
|
|
49
|
+
timeout_seconds=v["timeoutSeconds"],
|
|
50
|
+
page_size=v["pageSize"],
|
|
51
|
+
ca_file=v["caFile"],
|
|
52
|
+
),
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def test_invalid(self):
|
|
56
|
+
for v in load_vectors()["invalid"]:
|
|
57
|
+
with self.subTest(uri=v["uri"], reason=v["reason"]):
|
|
58
|
+
with self.assertRaises(ValueError) as caught:
|
|
59
|
+
parse_connection_uri(v["uri"])
|
|
60
|
+
self.assertIn(NAMED[v["uri"]], str(caught.exception))
|
|
61
|
+
|
|
62
|
+
def test_bad_percent_encoding(self):
|
|
63
|
+
with self.assertRaises(ValueError):
|
|
64
|
+
parse_connection_uri("hexdb://ada:p%4@localhost")
|
|
65
|
+
|
|
66
|
+
def test_plain_urls_unchanged(self):
|
|
67
|
+
db = HexDB("http://127.0.0.1:7700/", "hxk_x")
|
|
68
|
+
self.assertEqual((db.base, db.token, db.timeout), ("http://127.0.0.1:7700", "hxk_x", 30.0))
|
|
69
|
+
|
|
70
|
+
def test_uri_options_and_explicit_arguments(self):
|
|
71
|
+
db = HexDB("hexdbs://db.example.com/hexdb/?apiKey=hxk_uri&timeout=7")
|
|
72
|
+
self.assertEqual((db.base, db.token, db.timeout), ("https://db.example.com/hexdb", "hxk_uri", 7))
|
|
73
|
+
db = HexDB("hexdbs://db.example.com?apiKey=hxk_uri&timeout=7", api_key="hxk_arg", timeout=3)
|
|
74
|
+
self.assertEqual((db.token, db.timeout), ("hxk_arg", 3))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class SignInTests(unittest.TestCase):
|
|
78
|
+
"""HexDB(uri) with a login signs in with POST /auth/login."""
|
|
79
|
+
|
|
80
|
+
def test_login_in_uri_signs_in(self):
|
|
81
|
+
seen = []
|
|
82
|
+
|
|
83
|
+
class Handler(http.server.BaseHTTPRequestHandler):
|
|
84
|
+
def do_POST(self):
|
|
85
|
+
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
|
|
86
|
+
seen.append((self.path, body))
|
|
87
|
+
answer = json.dumps({"token": "sess_1", "user": {"login": body["login"]}, "expires_at": "later"}).encode()
|
|
88
|
+
self.send_response(200)
|
|
89
|
+
self.send_header("Content-Type", "application/json")
|
|
90
|
+
self.send_header("Content-Length", str(len(answer)))
|
|
91
|
+
self.end_headers()
|
|
92
|
+
self.wfile.write(answer)
|
|
93
|
+
|
|
94
|
+
def log_message(self, *args):
|
|
95
|
+
pass
|
|
96
|
+
|
|
97
|
+
httpd = http.server.HTTPServer(("127.0.0.1", 0), Handler)
|
|
98
|
+
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
|
|
99
|
+
thread.start()
|
|
100
|
+
try:
|
|
101
|
+
port = httpd.server_address[1]
|
|
102
|
+
db = HexDB(f"hexdb://ada:p%40ss@127.0.0.1:{port}/base")
|
|
103
|
+
finally:
|
|
104
|
+
httpd.shutdown()
|
|
105
|
+
httpd.server_close()
|
|
106
|
+
thread.join()
|
|
107
|
+
self.assertEqual(seen, [("/base/auth/login", {"login": "ada", "password": "p@ss", "return_token": True})])
|
|
108
|
+
self.assertEqual(db.token, "sess_1")
|
|
109
|
+
self.assertEqual(db.base, f"http://127.0.0.1:{port}/base")
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
if __name__ == "__main__":
|
|
113
|
+
unittest.main()
|
hexdb-2.3.2/PKG-INFO
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: hexdb
|
|
3
|
-
Version: 2.3.2
|
|
4
|
-
Summary: Client for HexDB, the hexagonal document database.
|
|
5
|
-
Author: Dream In Hex
|
|
6
|
-
License-Expression: Apache-2.0
|
|
7
|
-
Project-URL: Homepage, https://hexdb.app
|
|
8
|
-
Project-URL: Source, https://github.com/dreaminhex/hexdb
|
|
9
|
-
Project-URL: Issues, https://github.com/dreaminhex/hexdb/issues
|
|
10
|
-
Keywords: hexdb,database,document,nosql,client
|
|
11
|
-
Requires-Python: >=3.9
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
|
|
14
|
-
# hexdb (Python)
|
|
15
|
-
|
|
16
|
-
Client for HexDB. Python 3.9+, standard library only. See ../README.md and the HexDB manual.
|
hexdb-2.3.2/README.md
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: hexdb
|
|
3
|
-
Version: 2.3.2
|
|
4
|
-
Summary: Client for HexDB, the hexagonal document database.
|
|
5
|
-
Author: Dream In Hex
|
|
6
|
-
License-Expression: Apache-2.0
|
|
7
|
-
Project-URL: Homepage, https://hexdb.app
|
|
8
|
-
Project-URL: Source, https://github.com/dreaminhex/hexdb
|
|
9
|
-
Project-URL: Issues, https://github.com/dreaminhex/hexdb/issues
|
|
10
|
-
Keywords: hexdb,database,document,nosql,client
|
|
11
|
-
Requires-Python: >=3.9
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
|
|
14
|
-
# hexdb (Python)
|
|
15
|
-
|
|
16
|
-
Client for HexDB. Python 3.9+, standard library only. See ../README.md and the HexDB manual.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|