helix-db 0.1.0__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.
- helix_db-0.1.0/PKG-INFO +94 -0
- helix_db-0.1.0/README.md +74 -0
- helix_db-0.1.0/pyproject.toml +33 -0
- helix_db-0.1.0/setup.cfg +4 -0
- helix_db-0.1.0/src/helix_db/__init__.py +3 -0
- helix_db-0.1.0/src/helix_db.egg-info/PKG-INFO +94 -0
- helix_db-0.1.0/src/helix_db.egg-info/SOURCES.txt +13 -0
- helix_db-0.1.0/src/helix_db.egg-info/dependency_links.txt +1 -0
- helix_db-0.1.0/src/helix_db.egg-info/top_level.txt +2 -0
- helix_db-0.1.0/src/helixdb/__init__.py +13 -0
- helix_db-0.1.0/src/helixdb/client.py +218 -0
- helix_db-0.1.0/src/helixdb/dsl.py +3255 -0
- helix_db-0.1.0/src/helixdb/py.typed +1 -0
- helix_db-0.1.0/tests/test_client.py +92 -0
- helix_db-0.1.0/tests/test_dsl.py +330 -0
helix_db-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: helix-db
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for HelixDB dynamic query DSL and client
|
|
5
|
+
Author: HelixDB
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HelixDB/helix-db
|
|
8
|
+
Project-URL: Repository, https://github.com/HelixDB/helix-db
|
|
9
|
+
Keywords: helixdb,graph,vector,database,dsl
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# HelixDB Python SDK
|
|
22
|
+
|
|
23
|
+
The Python SDK pairs an idiomatic query-builder DSL with a small dependency-free
|
|
24
|
+
HTTP client for sending dynamic HelixDB queries to `POST /v1/query`.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from helixdb import Client, Predicate, g, read_batch
|
|
28
|
+
|
|
29
|
+
query = (
|
|
30
|
+
read_batch()
|
|
31
|
+
.var_as(
|
|
32
|
+
"users",
|
|
33
|
+
g()
|
|
34
|
+
.n_with_label("User")
|
|
35
|
+
.where(Predicate.eq("status", "active"))
|
|
36
|
+
.limit(25)
|
|
37
|
+
.value_map(["$id", "name", "status"]),
|
|
38
|
+
)
|
|
39
|
+
.returning(["users"])
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
request = query.to_dynamic_request()
|
|
43
|
+
result = Client("http://localhost:6969").query().dynamic(request).send()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The DSL emits the same dynamic-query JSON AST as the Rust, TypeScript, and Go
|
|
47
|
+
SDKs. Python methods use `snake_case`; compatibility aliases such as
|
|
48
|
+
`nWithLabel` and `valueMap` are also available for users translating TypeScript
|
|
49
|
+
examples directly.
|
|
50
|
+
|
|
51
|
+
## Dynamic Parameters
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from helixdb import Predicate, define_params, g, param, read_batch
|
|
55
|
+
|
|
56
|
+
params = define_params({
|
|
57
|
+
"tenant_id": param.string(),
|
|
58
|
+
"limit": param.i64(),
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
query = (
|
|
62
|
+
read_batch()
|
|
63
|
+
.var_as(
|
|
64
|
+
"users",
|
|
65
|
+
g()
|
|
66
|
+
.n_with_label("User")
|
|
67
|
+
.where(Predicate.eq("tenantId", params.tenant_id))
|
|
68
|
+
.limit(params.limit)
|
|
69
|
+
.value_map(["$id", "name", "tenantId"]),
|
|
70
|
+
)
|
|
71
|
+
.returning(["users"])
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
body = query.to_dynamic_json(
|
|
75
|
+
params,
|
|
76
|
+
{"tenant_id": "acme", "limit": 10},
|
|
77
|
+
query_name="find_users",
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Stored Queries
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from helixdb import Client
|
|
85
|
+
|
|
86
|
+
client = Client("https://cluster.helix-db.com", api_key="hx_secret")
|
|
87
|
+
response = client.query().body({"tenant_id": "acme"}).stored("find_users").send()
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Run the SDK tests from the repository root:
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
PYTHONPATH=sdks/python/src python -m unittest discover sdks/python/tests
|
|
94
|
+
```
|
helix_db-0.1.0/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# HelixDB Python SDK
|
|
2
|
+
|
|
3
|
+
The Python SDK pairs an idiomatic query-builder DSL with a small dependency-free
|
|
4
|
+
HTTP client for sending dynamic HelixDB queries to `POST /v1/query`.
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
from helixdb import Client, Predicate, g, read_batch
|
|
8
|
+
|
|
9
|
+
query = (
|
|
10
|
+
read_batch()
|
|
11
|
+
.var_as(
|
|
12
|
+
"users",
|
|
13
|
+
g()
|
|
14
|
+
.n_with_label("User")
|
|
15
|
+
.where(Predicate.eq("status", "active"))
|
|
16
|
+
.limit(25)
|
|
17
|
+
.value_map(["$id", "name", "status"]),
|
|
18
|
+
)
|
|
19
|
+
.returning(["users"])
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
request = query.to_dynamic_request()
|
|
23
|
+
result = Client("http://localhost:6969").query().dynamic(request).send()
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The DSL emits the same dynamic-query JSON AST as the Rust, TypeScript, and Go
|
|
27
|
+
SDKs. Python methods use `snake_case`; compatibility aliases such as
|
|
28
|
+
`nWithLabel` and `valueMap` are also available for users translating TypeScript
|
|
29
|
+
examples directly.
|
|
30
|
+
|
|
31
|
+
## Dynamic Parameters
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from helixdb import Predicate, define_params, g, param, read_batch
|
|
35
|
+
|
|
36
|
+
params = define_params({
|
|
37
|
+
"tenant_id": param.string(),
|
|
38
|
+
"limit": param.i64(),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
query = (
|
|
42
|
+
read_batch()
|
|
43
|
+
.var_as(
|
|
44
|
+
"users",
|
|
45
|
+
g()
|
|
46
|
+
.n_with_label("User")
|
|
47
|
+
.where(Predicate.eq("tenantId", params.tenant_id))
|
|
48
|
+
.limit(params.limit)
|
|
49
|
+
.value_map(["$id", "name", "tenantId"]),
|
|
50
|
+
)
|
|
51
|
+
.returning(["users"])
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
body = query.to_dynamic_json(
|
|
55
|
+
params,
|
|
56
|
+
{"tenant_id": "acme", "limit": 10},
|
|
57
|
+
query_name="find_users",
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Stored Queries
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from helixdb import Client
|
|
65
|
+
|
|
66
|
+
client = Client("https://cluster.helix-db.com", api_key="hx_secret")
|
|
67
|
+
response = client.query().body({"tenant_id": "acme"}).stored("find_users").send()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Run the SDK tests from the repository root:
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
PYTHONPATH=sdks/python/src python -m unittest discover sdks/python/tests
|
|
74
|
+
```
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "helix-db"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python SDK for HelixDB dynamic query DSL and client"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "HelixDB" }]
|
|
13
|
+
keywords = ["helixdb", "graph", "vector", "database", "dsl"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/HelixDB/helix-db"
|
|
27
|
+
Repository = "https://github.com/HelixDB/helix-db"
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.packages.find]
|
|
30
|
+
where = ["src"]
|
|
31
|
+
|
|
32
|
+
[tool.setuptools.package-data]
|
|
33
|
+
helixdb = ["py.typed"]
|
helix_db-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: helix-db
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for HelixDB dynamic query DSL and client
|
|
5
|
+
Author: HelixDB
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/HelixDB/helix-db
|
|
8
|
+
Project-URL: Repository, https://github.com/HelixDB/helix-db
|
|
9
|
+
Keywords: helixdb,graph,vector,database,dsl
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# HelixDB Python SDK
|
|
22
|
+
|
|
23
|
+
The Python SDK pairs an idiomatic query-builder DSL with a small dependency-free
|
|
24
|
+
HTTP client for sending dynamic HelixDB queries to `POST /v1/query`.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from helixdb import Client, Predicate, g, read_batch
|
|
28
|
+
|
|
29
|
+
query = (
|
|
30
|
+
read_batch()
|
|
31
|
+
.var_as(
|
|
32
|
+
"users",
|
|
33
|
+
g()
|
|
34
|
+
.n_with_label("User")
|
|
35
|
+
.where(Predicate.eq("status", "active"))
|
|
36
|
+
.limit(25)
|
|
37
|
+
.value_map(["$id", "name", "status"]),
|
|
38
|
+
)
|
|
39
|
+
.returning(["users"])
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
request = query.to_dynamic_request()
|
|
43
|
+
result = Client("http://localhost:6969").query().dynamic(request).send()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The DSL emits the same dynamic-query JSON AST as the Rust, TypeScript, and Go
|
|
47
|
+
SDKs. Python methods use `snake_case`; compatibility aliases such as
|
|
48
|
+
`nWithLabel` and `valueMap` are also available for users translating TypeScript
|
|
49
|
+
examples directly.
|
|
50
|
+
|
|
51
|
+
## Dynamic Parameters
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from helixdb import Predicate, define_params, g, param, read_batch
|
|
55
|
+
|
|
56
|
+
params = define_params({
|
|
57
|
+
"tenant_id": param.string(),
|
|
58
|
+
"limit": param.i64(),
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
query = (
|
|
62
|
+
read_batch()
|
|
63
|
+
.var_as(
|
|
64
|
+
"users",
|
|
65
|
+
g()
|
|
66
|
+
.n_with_label("User")
|
|
67
|
+
.where(Predicate.eq("tenantId", params.tenant_id))
|
|
68
|
+
.limit(params.limit)
|
|
69
|
+
.value_map(["$id", "name", "tenantId"]),
|
|
70
|
+
)
|
|
71
|
+
.returning(["users"])
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
body = query.to_dynamic_json(
|
|
75
|
+
params,
|
|
76
|
+
{"tenant_id": "acme", "limit": 10},
|
|
77
|
+
query_name="find_users",
|
|
78
|
+
)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Stored Queries
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from helixdb import Client
|
|
85
|
+
|
|
86
|
+
client = Client("https://cluster.helix-db.com", api_key="hx_secret")
|
|
87
|
+
response = client.query().body({"tenant_id": "acme"}).stored("find_users").send()
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Run the SDK tests from the repository root:
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
PYTHONPATH=sdks/python/src python -m unittest discover sdks/python/tests
|
|
94
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/helix_db/__init__.py
|
|
4
|
+
src/helix_db.egg-info/PKG-INFO
|
|
5
|
+
src/helix_db.egg-info/SOURCES.txt
|
|
6
|
+
src/helix_db.egg-info/dependency_links.txt
|
|
7
|
+
src/helix_db.egg-info/top_level.txt
|
|
8
|
+
src/helixdb/__init__.py
|
|
9
|
+
src/helixdb/client.py
|
|
10
|
+
src/helixdb/dsl.py
|
|
11
|
+
src/helixdb/py.typed
|
|
12
|
+
tests/test_client.py
|
|
13
|
+
tests/test_dsl.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""HelixDB Python SDK."""
|
|
2
|
+
|
|
3
|
+
from .client import Client, HelixDBClient, HelixError, QueryBuilder, QueryRequest
|
|
4
|
+
from .dsl import *
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Client",
|
|
8
|
+
"HelixDBClient",
|
|
9
|
+
"HelixError",
|
|
10
|
+
"QueryBuilder",
|
|
11
|
+
"QueryRequest",
|
|
12
|
+
*[name for name in globals() if not name.startswith("_")],
|
|
13
|
+
]
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""HTTP client for HelixDB query routes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import json
|
|
7
|
+
from typing import Any
|
|
8
|
+
from urllib.error import HTTPError, URLError
|
|
9
|
+
from urllib.parse import urljoin, urlparse
|
|
10
|
+
from urllib.request import Request, urlopen
|
|
11
|
+
|
|
12
|
+
from .dsl import DynamicQueryRequest, stringify_json
|
|
13
|
+
|
|
14
|
+
DEFAULT_URL = "http://localhost:6969"
|
|
15
|
+
QUERY_PATH = "/v1/query"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class HelixError(Exception):
|
|
19
|
+
"""Error raised by the HelixDB client."""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
kind: str,
|
|
24
|
+
message: str,
|
|
25
|
+
*,
|
|
26
|
+
details: str | None = None,
|
|
27
|
+
status_code: int | None = None,
|
|
28
|
+
cause: BaseException | None = None,
|
|
29
|
+
) -> None:
|
|
30
|
+
super().__init__(message)
|
|
31
|
+
self.kind = kind
|
|
32
|
+
self.details = details
|
|
33
|
+
self.status_code = status_code
|
|
34
|
+
self.__cause__ = cause
|
|
35
|
+
|
|
36
|
+
@classmethod
|
|
37
|
+
def network(cls, message: str, *, cause: BaseException | None = None) -> "HelixError":
|
|
38
|
+
return cls(
|
|
39
|
+
"Network",
|
|
40
|
+
f"error communicating with server: {message}",
|
|
41
|
+
details=message,
|
|
42
|
+
cause=cause,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
@classmethod
|
|
46
|
+
def remote(cls, details: str, *, status_code: int | None = None) -> "HelixError":
|
|
47
|
+
return cls(
|
|
48
|
+
"Remote",
|
|
49
|
+
f"got error from server: {details}",
|
|
50
|
+
details=details,
|
|
51
|
+
status_code=status_code,
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
@classmethod
|
|
55
|
+
def serialization(cls, message: str, *, cause: BaseException | None = None) -> "HelixError":
|
|
56
|
+
return cls(
|
|
57
|
+
"Serialization",
|
|
58
|
+
f"error serializing data: {message}",
|
|
59
|
+
details=message,
|
|
60
|
+
cause=cause,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def invalid_url(cls, message: str, *, cause: BaseException | None = None) -> "HelixError":
|
|
65
|
+
return cls("InvalidUrl", f"invalid url: {message}", details=message, cause=cause)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Client:
|
|
69
|
+
"""Synchronous HTTP client for running queries against a Helix instance."""
|
|
70
|
+
|
|
71
|
+
def __init__(self, url: str | None = None, *, api_key: str | None = None) -> None:
|
|
72
|
+
self._base_url = url or DEFAULT_URL
|
|
73
|
+
parsed = urlparse(self._base_url)
|
|
74
|
+
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
|
75
|
+
raise HelixError.invalid_url("missing scheme or host")
|
|
76
|
+
self._api_key = api_key
|
|
77
|
+
|
|
78
|
+
def with_api_key(self, api_key: str | None = None) -> "Client":
|
|
79
|
+
"""Set or clear the bearer API key sent on every request."""
|
|
80
|
+
|
|
81
|
+
self._api_key = api_key
|
|
82
|
+
return self
|
|
83
|
+
|
|
84
|
+
def query(self) -> "QueryBuilder":
|
|
85
|
+
return QueryBuilder(self._base_url, self._api_key)
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def base_url(self) -> str:
|
|
89
|
+
return self._base_url
|
|
90
|
+
|
|
91
|
+
def execute(self, request: DynamicQueryRequest, **options: Any) -> Any:
|
|
92
|
+
"""Convenience wrapper for ``client.query().dynamic(request).send()``."""
|
|
93
|
+
|
|
94
|
+
builder = self.query()
|
|
95
|
+
if options.pop("writer_only", False):
|
|
96
|
+
builder.writer_only()
|
|
97
|
+
if options.pop("warm_only", False):
|
|
98
|
+
builder.warm_only()
|
|
99
|
+
if "await_durability" in options:
|
|
100
|
+
builder.should_await_durability(bool(options.pop("await_durability")))
|
|
101
|
+
if options:
|
|
102
|
+
unknown = ", ".join(sorted(options))
|
|
103
|
+
raise TypeError(f"unknown execute option(s): {unknown}")
|
|
104
|
+
return builder.dynamic(request).send()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
HelixDBClient = Client
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@dataclass
|
|
111
|
+
class QueryBuilder:
|
|
112
|
+
_base_url: str
|
|
113
|
+
_api_key: str | None = None
|
|
114
|
+
_headers: dict[str, str] | None = None
|
|
115
|
+
_body: str | None = None
|
|
116
|
+
|
|
117
|
+
def __post_init__(self) -> None:
|
|
118
|
+
if self._headers is None:
|
|
119
|
+
self._headers = {"Content-Type": "application/json"}
|
|
120
|
+
|
|
121
|
+
def writer_only(self) -> "QueryBuilder":
|
|
122
|
+
self._headers["x-helix-require-writer"] = "true" # type: ignore[index]
|
|
123
|
+
return self
|
|
124
|
+
|
|
125
|
+
def warm_only(self) -> "QueryBuilder":
|
|
126
|
+
self._headers["x-helix-warm"] = "true" # type: ignore[index]
|
|
127
|
+
return self
|
|
128
|
+
|
|
129
|
+
def should_await_durability(self, should: bool) -> "QueryBuilder":
|
|
130
|
+
self._headers["x-helix-await-durable"] = "true" if should else "false" # type: ignore[index]
|
|
131
|
+
return self
|
|
132
|
+
|
|
133
|
+
def body(self, data: Any) -> "QueryBuilder":
|
|
134
|
+
try:
|
|
135
|
+
self._body = stringify_json(data)
|
|
136
|
+
except (TypeError, ValueError) as exc:
|
|
137
|
+
raise HelixError.serialization(str(exc), cause=exc) from exc
|
|
138
|
+
return self
|
|
139
|
+
|
|
140
|
+
def stored(self, query_name: str) -> "QueryRequest":
|
|
141
|
+
return QueryRequest(
|
|
142
|
+
base_url=self._base_url,
|
|
143
|
+
api_key=self._api_key,
|
|
144
|
+
headers=dict(self._headers or {}),
|
|
145
|
+
query_type="stored",
|
|
146
|
+
query_name=query_name,
|
|
147
|
+
body=self._body,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
def dynamic(self, query: DynamicQueryRequest) -> "QueryRequest":
|
|
151
|
+
return QueryRequest(
|
|
152
|
+
base_url=self._base_url,
|
|
153
|
+
api_key=self._api_key,
|
|
154
|
+
headers=dict(self._headers or {}),
|
|
155
|
+
query_type="dynamic",
|
|
156
|
+
dynamic_query=query,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
@dataclass(frozen=True)
|
|
161
|
+
class QueryRequest:
|
|
162
|
+
base_url: str
|
|
163
|
+
api_key: str | None
|
|
164
|
+
headers: dict[str, str]
|
|
165
|
+
query_type: str
|
|
166
|
+
query_name: str | None = None
|
|
167
|
+
dynamic_query: DynamicQueryRequest | None = None
|
|
168
|
+
body: str | None = None
|
|
169
|
+
|
|
170
|
+
def send(self) -> Any:
|
|
171
|
+
if self.query_type == "dynamic":
|
|
172
|
+
if self.dynamic_query is None:
|
|
173
|
+
raise HelixError.serialization("dynamic query request is missing")
|
|
174
|
+
path = QUERY_PATH
|
|
175
|
+
payload = self.dynamic_query.to_json_bytes()
|
|
176
|
+
elif self.query_type == "stored":
|
|
177
|
+
if self.query_name is None:
|
|
178
|
+
raise HelixError.serialization("stored query name is missing")
|
|
179
|
+
path = f"{QUERY_PATH}/{self.query_name}"
|
|
180
|
+
payload = self.body.encode("utf-8") if self.body is not None else None
|
|
181
|
+
else:
|
|
182
|
+
raise HelixError.serialization(f"unknown query type: {self.query_type}")
|
|
183
|
+
|
|
184
|
+
try:
|
|
185
|
+
url = urljoin(self.base_url.rstrip("/") + "/", path)
|
|
186
|
+
except Exception as exc:
|
|
187
|
+
raise HelixError.invalid_url(str(exc), cause=exc) from exc
|
|
188
|
+
|
|
189
|
+
headers = dict(self.headers)
|
|
190
|
+
if self.api_key is not None:
|
|
191
|
+
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
192
|
+
|
|
193
|
+
request = Request(url, data=payload, headers=headers, method="POST")
|
|
194
|
+
try:
|
|
195
|
+
with urlopen(request) as response: # nosec B310: user controls Helix endpoint.
|
|
196
|
+
status = response.getcode()
|
|
197
|
+
response_body = response.read()
|
|
198
|
+
reason = getattr(response, "reason", "") or f"unknown error with code: {status}"
|
|
199
|
+
except HTTPError as exc:
|
|
200
|
+
details = exc.read().decode("utf-8", errors="replace") or exc.reason or str(exc)
|
|
201
|
+
raise HelixError.remote(details, status_code=exc.code) from exc
|
|
202
|
+
except URLError as exc:
|
|
203
|
+
raise HelixError.network(str(exc.reason), cause=exc) from exc
|
|
204
|
+
except OSError as exc:
|
|
205
|
+
raise HelixError.network(str(exc), cause=exc) from exc
|
|
206
|
+
|
|
207
|
+
if status != 200:
|
|
208
|
+
details = response_body.decode("utf-8", errors="replace") or reason
|
|
209
|
+
raise HelixError.remote(details, status_code=status)
|
|
210
|
+
if not response_body:
|
|
211
|
+
return None
|
|
212
|
+
try:
|
|
213
|
+
return json.loads(response_body)
|
|
214
|
+
except json.JSONDecodeError as exc:
|
|
215
|
+
raise HelixError.serialization(str(exc), cause=exc) from exc
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
__all__ = ["Client", "HelixDBClient", "HelixError", "QueryBuilder", "QueryRequest"]
|