himring 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.
- himring-0.1.0/.gitignore +18 -0
- himring-0.1.0/LICENSE +21 -0
- himring-0.1.0/PKG-INFO +113 -0
- himring-0.1.0/README.md +90 -0
- himring-0.1.0/pyproject.toml +71 -0
- himring-0.1.0/src/himring/__init__.py +87 -0
- himring-0.1.0/src/himring/_async_client.py +163 -0
- himring-0.1.0/src/himring/_async_transfers.py +339 -0
- himring-0.1.0/src/himring/_calls.py +122 -0
- himring-0.1.0/src/himring/_download.py +130 -0
- himring-0.1.0/src/himring/_errors.py +81 -0
- himring-0.1.0/src/himring/_file_content.py +5 -0
- himring-0.1.0/src/himring/_file_kinds.py +23 -0
- himring-0.1.0/src/himring/_generated/__init__.py +0 -0
- himring-0.1.0/src/himring/_generated/async_methods.py +4829 -0
- himring-0.1.0/src/himring/_generated/operations.py +4096 -0
- himring-0.1.0/src/himring/_generated/sync_methods.py +4820 -0
- himring-0.1.0/src/himring/_generated/types.py +14097 -0
- himring-0.1.0/src/himring/_omitted.py +17 -0
- himring-0.1.0/src/himring/_operation_description.py +30 -0
- himring-0.1.0/src/himring/_request_parts.py +220 -0
- himring-0.1.0/src/himring/_responses.py +199 -0
- himring-0.1.0/src/himring/_settings.py +112 -0
- himring-0.1.0/src/himring/_sync_client.py +161 -0
- himring-0.1.0/src/himring/_sync_transfers.py +307 -0
- himring-0.1.0/src/himring/_upload_integrity.py +25 -0
- himring-0.1.0/src/himring/_upload_plan.py +341 -0
- himring-0.1.0/src/himring/_webhooks.py +162 -0
- himring-0.1.0/src/himring/py.typed +0 -0
- himring-0.1.0/src/himring/types.py +2 -0
himring-0.1.0/.gitignore
ADDED
himring-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Himring, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
himring-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: himring
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The Python SDK of the Himring API, which lets AI agents answer from what a business knows.
|
|
5
|
+
Project-URL: Homepage, https://himring.ai
|
|
6
|
+
Project-URL: Repository, https://github.com/himringhq/himring-sdk
|
|
7
|
+
Project-URL: Issues, https://github.com/himringhq/himring-sdk/issues
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: ai-agents,himring,rag,retrieval,sdk
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
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
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: httpx<1,>=0.28.1
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# himring
|
|
25
|
+
|
|
26
|
+
The Python SDK of the [Himring API](https://himring.ai), which lets AI agents answer from what a business knows.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pip install himring
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The package needs Python 3.10 or later, and its only dependency is httpx.
|
|
33
|
+
|
|
34
|
+
## Calling the API
|
|
35
|
+
|
|
36
|
+
`Himring` is the synchronous client, and `AsyncHimring` is the asyncio client. Each operation of the API is a method that takes keyword arguments. A method returns the JSON answer as a typed dictionary, or a streamed `Download` when the operation answers with a file.
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from himring import Himring
|
|
40
|
+
|
|
41
|
+
with Himring(api_key="hk_live_...") as himring:
|
|
42
|
+
found = himring.search_knowledge_base(
|
|
43
|
+
project_id="prj_...",
|
|
44
|
+
knowledge_base_id="kb_...",
|
|
45
|
+
question="When does the store open on Sunday?",
|
|
46
|
+
)
|
|
47
|
+
for passage in found["passages"]:
|
|
48
|
+
print(passage["file_path"], passage["text"])
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The client sends the API key, or a session token passed as `session_token`, in the `Authorization` header. It never follows redirects, and it stops waiting after `timeout_seconds`, which is 30 seconds unless you set another value on the client or on one call. A call that sends a file stops only when the upload makes no progress for `upload_idle_timeout_seconds`, which is 120 seconds by default.
|
|
52
|
+
|
|
53
|
+
## Errors
|
|
54
|
+
|
|
55
|
+
Every refusal raises `HimringApiError`. Its `kind` is `refused` when the API answered with an error, `invalid_input` when the client refused the call before sending it, `invalid_response` when the answer was not JSON or carried an error of the wrong shape, and `network` or `timeout` when the API did not answer. A refusal carries the `status`, the documented `code`, the `message`, the `issues` of each refused field, the `missing_permission` of an API key, `retry_after_seconds`, and the `request_id` of the call.
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from himring import HimringApiError
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
himring.get_database(project_id="prj_...", database_id="db_...")
|
|
62
|
+
except HimringApiError as error:
|
|
63
|
+
if error.kind == "refused" and error.code == "database_not_found":
|
|
64
|
+
print("The database is gone.")
|
|
65
|
+
else:
|
|
66
|
+
raise
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Files
|
|
70
|
+
|
|
71
|
+
`add_file` sends a file to a knowledge base. It sends a large file in parts, at most four at a time, straight to storage, and it never sends your API key to storage. After each part arrives, the client calls `on_progress` with the number of bytes in that part. `download_file` reads a version of a file from storage.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from pathlib import Path
|
|
75
|
+
|
|
76
|
+
from himring import safe_download_name
|
|
77
|
+
|
|
78
|
+
with Path("refunds.pdf").open("rb") as file:
|
|
79
|
+
added = himring.add_file(
|
|
80
|
+
project_id="prj_...",
|
|
81
|
+
knowledge_base_id="kb_...",
|
|
82
|
+
path="policies/refunds.pdf",
|
|
83
|
+
content=file,
|
|
84
|
+
on_progress=lambda part_bytes: print(f"Storage took {part_bytes} more bytes."),
|
|
85
|
+
)
|
|
86
|
+
print(added["outcome"], added["file"]["id"])
|
|
87
|
+
|
|
88
|
+
with himring.download_file(
|
|
89
|
+
project_id="prj_...", knowledge_base_id="kb_...", file_id=added["file"]["id"], version=1
|
|
90
|
+
) as download:
|
|
91
|
+
Path(safe_download_name(download.file_name, "file.pdf")).write_bytes(download.read())
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Memory webhooks
|
|
95
|
+
|
|
96
|
+
`verify_memory_webhook` checks the `himring-signature` header of a Memory webhook against its signing secret. It returns the event only when the signature matches the body and Himring signed the request no more than 5 minutes away from the current time.
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from himring import MemoryWebhookRejected, verify_memory_webhook
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
event = verify_memory_webhook(
|
|
103
|
+
body=request_body,
|
|
104
|
+
signature=request_headers.get("himring-signature"),
|
|
105
|
+
secret=signing_secret,
|
|
106
|
+
)
|
|
107
|
+
except MemoryWebhookRejected as rejection:
|
|
108
|
+
print(rejection.reason)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT
|
himring-0.1.0/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# himring
|
|
2
|
+
|
|
3
|
+
The Python SDK of the [Himring API](https://himring.ai), which lets AI agents answer from what a business knows.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pip install himring
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The package needs Python 3.10 or later, and its only dependency is httpx.
|
|
10
|
+
|
|
11
|
+
## Calling the API
|
|
12
|
+
|
|
13
|
+
`Himring` is the synchronous client, and `AsyncHimring` is the asyncio client. Each operation of the API is a method that takes keyword arguments. A method returns the JSON answer as a typed dictionary, or a streamed `Download` when the operation answers with a file.
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from himring import Himring
|
|
17
|
+
|
|
18
|
+
with Himring(api_key="hk_live_...") as himring:
|
|
19
|
+
found = himring.search_knowledge_base(
|
|
20
|
+
project_id="prj_...",
|
|
21
|
+
knowledge_base_id="kb_...",
|
|
22
|
+
question="When does the store open on Sunday?",
|
|
23
|
+
)
|
|
24
|
+
for passage in found["passages"]:
|
|
25
|
+
print(passage["file_path"], passage["text"])
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The client sends the API key, or a session token passed as `session_token`, in the `Authorization` header. It never follows redirects, and it stops waiting after `timeout_seconds`, which is 30 seconds unless you set another value on the client or on one call. A call that sends a file stops only when the upload makes no progress for `upload_idle_timeout_seconds`, which is 120 seconds by default.
|
|
29
|
+
|
|
30
|
+
## Errors
|
|
31
|
+
|
|
32
|
+
Every refusal raises `HimringApiError`. Its `kind` is `refused` when the API answered with an error, `invalid_input` when the client refused the call before sending it, `invalid_response` when the answer was not JSON or carried an error of the wrong shape, and `network` or `timeout` when the API did not answer. A refusal carries the `status`, the documented `code`, the `message`, the `issues` of each refused field, the `missing_permission` of an API key, `retry_after_seconds`, and the `request_id` of the call.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from himring import HimringApiError
|
|
36
|
+
|
|
37
|
+
try:
|
|
38
|
+
himring.get_database(project_id="prj_...", database_id="db_...")
|
|
39
|
+
except HimringApiError as error:
|
|
40
|
+
if error.kind == "refused" and error.code == "database_not_found":
|
|
41
|
+
print("The database is gone.")
|
|
42
|
+
else:
|
|
43
|
+
raise
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Files
|
|
47
|
+
|
|
48
|
+
`add_file` sends a file to a knowledge base. It sends a large file in parts, at most four at a time, straight to storage, and it never sends your API key to storage. After each part arrives, the client calls `on_progress` with the number of bytes in that part. `download_file` reads a version of a file from storage.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from pathlib import Path
|
|
52
|
+
|
|
53
|
+
from himring import safe_download_name
|
|
54
|
+
|
|
55
|
+
with Path("refunds.pdf").open("rb") as file:
|
|
56
|
+
added = himring.add_file(
|
|
57
|
+
project_id="prj_...",
|
|
58
|
+
knowledge_base_id="kb_...",
|
|
59
|
+
path="policies/refunds.pdf",
|
|
60
|
+
content=file,
|
|
61
|
+
on_progress=lambda part_bytes: print(f"Storage took {part_bytes} more bytes."),
|
|
62
|
+
)
|
|
63
|
+
print(added["outcome"], added["file"]["id"])
|
|
64
|
+
|
|
65
|
+
with himring.download_file(
|
|
66
|
+
project_id="prj_...", knowledge_base_id="kb_...", file_id=added["file"]["id"], version=1
|
|
67
|
+
) as download:
|
|
68
|
+
Path(safe_download_name(download.file_name, "file.pdf")).write_bytes(download.read())
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Memory webhooks
|
|
72
|
+
|
|
73
|
+
`verify_memory_webhook` checks the `himring-signature` header of a Memory webhook against its signing secret. It returns the event only when the signature matches the body and Himring signed the request no more than 5 minutes away from the current time.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from himring import MemoryWebhookRejected, verify_memory_webhook
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
event = verify_memory_webhook(
|
|
80
|
+
body=request_body,
|
|
81
|
+
signature=request_headers.get("himring-signature"),
|
|
82
|
+
secret=signing_secret,
|
|
83
|
+
)
|
|
84
|
+
except MemoryWebhookRejected as rejection:
|
|
85
|
+
print(rejection.reason)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "himring"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "The Python SDK of the Himring API, which lets AI agents answer from what a business knows."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
keywords = ["himring", "sdk", "retrieval", "rag", "ai-agents"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 4 - Beta",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.10",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
19
|
+
"Typing :: Typed",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"httpx>=0.28.1,<1",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://himring.ai"
|
|
27
|
+
Repository = "https://github.com/himringhq/himring-sdk"
|
|
28
|
+
Issues = "https://github.com/himringhq/himring-sdk/issues"
|
|
29
|
+
|
|
30
|
+
[build-system]
|
|
31
|
+
requires = ["hatchling>=1.27,<2"]
|
|
32
|
+
build-backend = "hatchling.build"
|
|
33
|
+
|
|
34
|
+
[tool.hatch.build.targets.wheel]
|
|
35
|
+
packages = ["src/himring"]
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.sdist]
|
|
38
|
+
include = ["/src", "/README.md", "/LICENSE", "/pyproject.toml"]
|
|
39
|
+
|
|
40
|
+
[tool.ruff]
|
|
41
|
+
line-length = 100
|
|
42
|
+
src = ["src", "tests", "codegen"]
|
|
43
|
+
target-version = "py310"
|
|
44
|
+
|
|
45
|
+
[tool.ruff.lint]
|
|
46
|
+
select = ["B", "E", "F", "I", "RUF", "SIM", "UP", "S", "ANN"]
|
|
47
|
+
|
|
48
|
+
[tool.ruff.lint.per-file-ignores]
|
|
49
|
+
"tests/**/*.py" = ["S101", "S105", "S106", "S108", "S310", "S603", "S607"]
|
|
50
|
+
"codegen/generate.py" = ["S603"]
|
|
51
|
+
"src/himring/_generated/*.py" = ["E501"]
|
|
52
|
+
"src/himring/types.py" = ["F403"]
|
|
53
|
+
|
|
54
|
+
[tool.pyright]
|
|
55
|
+
include = ["src", "tests", "codegen"]
|
|
56
|
+
pythonVersion = "3.10"
|
|
57
|
+
typeCheckingMode = "strict"
|
|
58
|
+
venvPath = "."
|
|
59
|
+
venv = ".venv"
|
|
60
|
+
|
|
61
|
+
[tool.pytest.ini_options]
|
|
62
|
+
addopts = "-ra -p no:cacheprovider"
|
|
63
|
+
testpaths = ["tests"]
|
|
64
|
+
tmp_path_retention_policy = "none"
|
|
65
|
+
|
|
66
|
+
[dependency-groups]
|
|
67
|
+
dev = [
|
|
68
|
+
"pyright==1.1.414",
|
|
69
|
+
"pytest==9.1.1",
|
|
70
|
+
"ruff==0.16.10",
|
|
71
|
+
]
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from himring._async_client import AsyncHimring
|
|
2
|
+
from himring._download import AsyncDownload, Download
|
|
3
|
+
from himring._errors import (
|
|
4
|
+
HimringApiError,
|
|
5
|
+
HimringApiErrorKind,
|
|
6
|
+
HimringApiIssue,
|
|
7
|
+
HimringMissingPermission,
|
|
8
|
+
documented_status_of,
|
|
9
|
+
is_refusal,
|
|
10
|
+
)
|
|
11
|
+
from himring._file_content import FileContent
|
|
12
|
+
from himring._file_kinds import READABLE_FILE_EXTENSIONS, file_extension_of, file_kind_of
|
|
13
|
+
from himring._generated.operations import (
|
|
14
|
+
ERROR_MESSAGES,
|
|
15
|
+
ERROR_STATUSES,
|
|
16
|
+
FILE_KIND_LIST,
|
|
17
|
+
FILE_KINDS,
|
|
18
|
+
OPERATIONS,
|
|
19
|
+
SPEC_API_URL,
|
|
20
|
+
FileKind,
|
|
21
|
+
)
|
|
22
|
+
from himring._omitted import OMITTED, Omitted
|
|
23
|
+
from himring._operation_description import OperationDescription, ResponseDescription
|
|
24
|
+
from himring._responses import safe_download_name
|
|
25
|
+
from himring._settings import (
|
|
26
|
+
DEFAULT_HIMRING_API_URL,
|
|
27
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
28
|
+
DEFAULT_UPLOAD_IDLE_TIMEOUT_SECONDS,
|
|
29
|
+
)
|
|
30
|
+
from himring._sync_client import Himring
|
|
31
|
+
from himring._upload_plan import AddedFile, FileOutcome, UploadContent, UploadSource
|
|
32
|
+
from himring._webhooks import (
|
|
33
|
+
DEFAULT_MEMORY_WEBHOOK_TOLERANCE_SECONDS,
|
|
34
|
+
MEMORY_WEBHOOK_EVENT_ID_HEADER,
|
|
35
|
+
MEMORY_WEBHOOK_REJECTIONS,
|
|
36
|
+
MEMORY_WEBHOOK_SIGNATURE_HEADER,
|
|
37
|
+
MemoryWebhookEvent,
|
|
38
|
+
MemoryWebhookRejected,
|
|
39
|
+
MemoryWebhookRejection,
|
|
40
|
+
verify_memory_webhook,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
__version__ = "0.1.0"
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"DEFAULT_HIMRING_API_URL",
|
|
47
|
+
"DEFAULT_MEMORY_WEBHOOK_TOLERANCE_SECONDS",
|
|
48
|
+
"DEFAULT_TIMEOUT_SECONDS",
|
|
49
|
+
"DEFAULT_UPLOAD_IDLE_TIMEOUT_SECONDS",
|
|
50
|
+
"ERROR_MESSAGES",
|
|
51
|
+
"ERROR_STATUSES",
|
|
52
|
+
"FILE_KINDS",
|
|
53
|
+
"FILE_KIND_LIST",
|
|
54
|
+
"MEMORY_WEBHOOK_EVENT_ID_HEADER",
|
|
55
|
+
"MEMORY_WEBHOOK_REJECTIONS",
|
|
56
|
+
"MEMORY_WEBHOOK_SIGNATURE_HEADER",
|
|
57
|
+
"OMITTED",
|
|
58
|
+
"OPERATIONS",
|
|
59
|
+
"READABLE_FILE_EXTENSIONS",
|
|
60
|
+
"SPEC_API_URL",
|
|
61
|
+
"AddedFile",
|
|
62
|
+
"AsyncDownload",
|
|
63
|
+
"AsyncHimring",
|
|
64
|
+
"Download",
|
|
65
|
+
"FileContent",
|
|
66
|
+
"FileKind",
|
|
67
|
+
"FileOutcome",
|
|
68
|
+
"Himring",
|
|
69
|
+
"HimringApiError",
|
|
70
|
+
"HimringApiErrorKind",
|
|
71
|
+
"HimringApiIssue",
|
|
72
|
+
"HimringMissingPermission",
|
|
73
|
+
"MemoryWebhookEvent",
|
|
74
|
+
"MemoryWebhookRejected",
|
|
75
|
+
"MemoryWebhookRejection",
|
|
76
|
+
"Omitted",
|
|
77
|
+
"OperationDescription",
|
|
78
|
+
"ResponseDescription",
|
|
79
|
+
"UploadContent",
|
|
80
|
+
"UploadSource",
|
|
81
|
+
"documented_status_of",
|
|
82
|
+
"file_extension_of",
|
|
83
|
+
"file_kind_of",
|
|
84
|
+
"is_refusal",
|
|
85
|
+
"safe_download_name",
|
|
86
|
+
"verify_memory_webhook",
|
|
87
|
+
]
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Mapping
|
|
4
|
+
from types import TracebackType
|
|
5
|
+
from typing import Literal
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
|
|
9
|
+
from himring._async_transfers import AsyncFileTransfers
|
|
10
|
+
from himring._calls import (
|
|
11
|
+
PreparedCall,
|
|
12
|
+
download_details_of,
|
|
13
|
+
passed_deadline,
|
|
14
|
+
prepare_call,
|
|
15
|
+
transport_failure,
|
|
16
|
+
)
|
|
17
|
+
from himring._download import AsyncDownload
|
|
18
|
+
from himring._generated.async_methods import AsyncOperationMethods
|
|
19
|
+
from himring._request_parts import async_file_chunks
|
|
20
|
+
from himring._responses import json_of, refusal_of
|
|
21
|
+
from himring._settings import DEFAULT_HIMRING_API_URL, ClientSettings, settings_of
|
|
22
|
+
from himring._upload_plan import AddedFile, UploadContent
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AsyncHimring(AsyncOperationMethods):
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
*,
|
|
29
|
+
api_key: str | None = None,
|
|
30
|
+
session_token: str | None = None,
|
|
31
|
+
base_url: str = DEFAULT_HIMRING_API_URL,
|
|
32
|
+
timeout_seconds: float | None = None,
|
|
33
|
+
upload_idle_timeout_seconds: float | None = None,
|
|
34
|
+
headers: Mapping[str, str] | None = None,
|
|
35
|
+
http_client: httpx.AsyncClient | None = None,
|
|
36
|
+
) -> None:
|
|
37
|
+
self._settings: ClientSettings = settings_of(
|
|
38
|
+
api_key=api_key,
|
|
39
|
+
session_token=session_token,
|
|
40
|
+
base_url=base_url,
|
|
41
|
+
timeout_seconds=timeout_seconds,
|
|
42
|
+
upload_idle_timeout_seconds=upload_idle_timeout_seconds,
|
|
43
|
+
headers=headers,
|
|
44
|
+
)
|
|
45
|
+
self._owns_http = http_client is None
|
|
46
|
+
self._http = (
|
|
47
|
+
http_client if http_client is not None else httpx.AsyncClient(follow_redirects=False)
|
|
48
|
+
)
|
|
49
|
+
self._transfers = AsyncFileTransfers(self, self._http, self._settings)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def base_url(self) -> str:
|
|
53
|
+
return str(self._settings.base_url)
|
|
54
|
+
|
|
55
|
+
async def aclose(self) -> None:
|
|
56
|
+
if self._owns_http:
|
|
57
|
+
await self._http.aclose()
|
|
58
|
+
|
|
59
|
+
async def __aenter__(self) -> AsyncHimring:
|
|
60
|
+
return self
|
|
61
|
+
|
|
62
|
+
async def __aexit__(
|
|
63
|
+
self,
|
|
64
|
+
kind: type[BaseException] | None,
|
|
65
|
+
error: BaseException | None,
|
|
66
|
+
trace: TracebackType | None,
|
|
67
|
+
) -> None:
|
|
68
|
+
await self.aclose()
|
|
69
|
+
|
|
70
|
+
async def invoke(
|
|
71
|
+
self,
|
|
72
|
+
operation_id: str,
|
|
73
|
+
fields: Mapping[str, object] | None = None,
|
|
74
|
+
*,
|
|
75
|
+
timeout_seconds: float | None = None,
|
|
76
|
+
) -> object:
|
|
77
|
+
prepared = prepare_call(self._settings, operation_id, fields, timeout_seconds)
|
|
78
|
+
response = await self._send(prepared)
|
|
79
|
+
if prepared.operation.response.kind == "download" and response.is_success:
|
|
80
|
+
return AsyncDownload(response, download_details_of(response, prepared), "the API")
|
|
81
|
+
body = await self._read(response, prepared)
|
|
82
|
+
if not response.is_success:
|
|
83
|
+
raise refusal_of(response, body, prepared.operation.path)
|
|
84
|
+
return json_of(response, body, prepared.operation.path)
|
|
85
|
+
|
|
86
|
+
async def _call_operation(
|
|
87
|
+
self, operation_id: str, fields: Mapping[str, object], timeout_seconds: float | None
|
|
88
|
+
) -> object:
|
|
89
|
+
return await self.invoke(operation_id, fields, timeout_seconds=timeout_seconds)
|
|
90
|
+
|
|
91
|
+
async def add_file(
|
|
92
|
+
self,
|
|
93
|
+
*,
|
|
94
|
+
project_id: str,
|
|
95
|
+
knowledge_base_id: str,
|
|
96
|
+
path: str,
|
|
97
|
+
content: UploadContent,
|
|
98
|
+
address: str | None = None,
|
|
99
|
+
content_sha256: str | None = None,
|
|
100
|
+
metadata: Mapping[str, str] | None = None,
|
|
101
|
+
on_progress: Callable[[int], object] | None = None,
|
|
102
|
+
) -> AddedFile:
|
|
103
|
+
return await self._transfers.add_file(
|
|
104
|
+
project_id=project_id,
|
|
105
|
+
knowledge_base_id=knowledge_base_id,
|
|
106
|
+
path=path,
|
|
107
|
+
content=content,
|
|
108
|
+
address=address,
|
|
109
|
+
content_sha256=content_sha256,
|
|
110
|
+
metadata=metadata,
|
|
111
|
+
on_progress=on_progress,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
async def download_file(
|
|
115
|
+
self,
|
|
116
|
+
*,
|
|
117
|
+
project_id: str,
|
|
118
|
+
knowledge_base_id: str,
|
|
119
|
+
file_id: str,
|
|
120
|
+
version: int,
|
|
121
|
+
rendition: Literal["pdf"] | None = None,
|
|
122
|
+
range: str | None = None,
|
|
123
|
+
) -> AsyncDownload:
|
|
124
|
+
return await self._transfers.download_file(
|
|
125
|
+
project_id=project_id,
|
|
126
|
+
knowledge_base_id=knowledge_base_id,
|
|
127
|
+
file_id=file_id,
|
|
128
|
+
version=version,
|
|
129
|
+
rendition=rendition,
|
|
130
|
+
byte_range=range,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
async def _send(self, prepared: PreparedCall) -> httpx.Response:
|
|
134
|
+
file_body = prepared.parts.file_body
|
|
135
|
+
content = (
|
|
136
|
+
async_file_chunks(file_body) if file_body is not None else prepared.parts.json_body
|
|
137
|
+
)
|
|
138
|
+
request = self._http.build_request(
|
|
139
|
+
prepared.operation.method,
|
|
140
|
+
prepared.url,
|
|
141
|
+
params=list(prepared.parts.query) or None,
|
|
142
|
+
content=content,
|
|
143
|
+
headers=prepared.headers,
|
|
144
|
+
timeout=prepared.timeout,
|
|
145
|
+
)
|
|
146
|
+
try:
|
|
147
|
+
return await self._http.send(request, stream=True, follow_redirects=False)
|
|
148
|
+
except httpx.HTTPError as error:
|
|
149
|
+
raise transport_failure(error, self._settings, prepared) from error
|
|
150
|
+
|
|
151
|
+
async def _read(self, response: httpx.Response, prepared: PreparedCall) -> bytes:
|
|
152
|
+
chunks: list[bytes] = []
|
|
153
|
+
try:
|
|
154
|
+
async for chunk in response.aiter_bytes():
|
|
155
|
+
chunks.append(chunk)
|
|
156
|
+
late = passed_deadline(prepared, self._settings)
|
|
157
|
+
if late is not None:
|
|
158
|
+
raise late
|
|
159
|
+
except httpx.HTTPError as error:
|
|
160
|
+
raise transport_failure(error, self._settings, prepared) from error
|
|
161
|
+
finally:
|
|
162
|
+
await response.aclose()
|
|
163
|
+
return b"".join(chunks)
|