mitra-sdk 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.
- mitra_sdk-0.1.0/.gitignore +22 -0
- mitra_sdk-0.1.0/LICENSE +201 -0
- mitra_sdk-0.1.0/NOTICE +5 -0
- mitra_sdk-0.1.0/PKG-INFO +274 -0
- mitra_sdk-0.1.0/README.md +243 -0
- mitra_sdk-0.1.0/pyproject.toml +57 -0
- mitra_sdk-0.1.0/scripts/README.md +15 -0
- mitra_sdk-0.1.0/src/mitra/__init__.py +129 -0
- mitra_sdk-0.1.0/src/mitra/_core.py +205 -0
- mitra_sdk-0.1.0/src/mitra/_version.py +1 -0
- mitra_sdk-0.1.0/src/mitra/client.py +1267 -0
- mitra_sdk-0.1.0/src/mitra/errors.py +220 -0
- mitra_sdk-0.1.0/src/mitra/models.py +515 -0
- mitra_sdk-0.1.0/src/mitra/py.typed +0 -0
- mitra_sdk-0.1.0/src/mitra/webhooks.py +72 -0
- mitra_sdk-0.1.0/tests/conftest.py +102 -0
- mitra_sdk-0.1.0/tests/test_admin.py +153 -0
- mitra_sdk-0.1.0/tests/test_async.py +53 -0
- mitra_sdk-0.1.0/tests/test_client.py +226 -0
- mitra_sdk-0.1.0/tests/test_contract.py +148 -0
- mitra_sdk-0.1.0/tests/test_embeds.py +307 -0
- mitra_sdk-0.1.0/tests/test_jobs.py +76 -0
- mitra_sdk-0.1.0/tests/test_modes.py +229 -0
- mitra_sdk-0.1.0/tests/test_stream.py +45 -0
- mitra_sdk-0.1.0/tests/test_webhooks.py +51 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
.env
|
|
2
|
+
.env.*
|
|
3
|
+
!.env.example
|
|
4
|
+
node_modules/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
__pycache__/
|
|
8
|
+
*.egg-info/
|
|
9
|
+
.venv/
|
|
10
|
+
.DS_Store
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.ruff_cache/
|
|
13
|
+
.mypy_cache/
|
|
14
|
+
.coverage
|
|
15
|
+
htmlcov/
|
|
16
|
+
*.sqlite
|
|
17
|
+
*.sqlite3
|
|
18
|
+
*.db
|
|
19
|
+
.idea/
|
|
20
|
+
.vscode/
|
|
21
|
+
sdks/typescript/src/schema.d.ts
|
|
22
|
+
|
mitra_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Matih Labs
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
mitra_sdk-0.1.0/NOTICE
ADDED
mitra_sdk-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mitra-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python client for the Matih public API (ask, Gems, dashboards, forecasts).
|
|
5
|
+
Project-URL: Homepage, https://github.com/matih-labs/mitra-sdk
|
|
6
|
+
Project-URL: Source, https://github.com/matih-labs/mitra-sdk
|
|
7
|
+
Project-URL: Issues, https://github.com/matih-labs/mitra-sdk/issues
|
|
8
|
+
Author: Matih Labs
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Keywords: analytics,api,matih,mcp,sdk
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: httpx<1,>=0.27
|
|
24
|
+
Requires-Dist: pydantic<3,>=2.6
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
28
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
29
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
|
|
32
|
+
<p align="center"><img src="https://raw.githubusercontent.com/matih-labs/mitra-sdk/main/assets/matih-logo.png" alt="Matih" width="160"></p>
|
|
33
|
+
|
|
34
|
+
# mitra-sdk (Python)
|
|
35
|
+
|
|
36
|
+
Python client for the Matih public API. Sync (`Mitra`) and async (`AsyncMitra`) clients on
|
|
37
|
+
[httpx](https://www.python-httpx.org/), pydantic v2 models for the main response shapes, SSE
|
|
38
|
+
streaming, background-job polling and webhook signature verification.
|
|
39
|
+
|
|
40
|
+
> Pre-release (0.1.0): the package is not on PyPI until `/api/v1` is live for every workspace. Install from a checkout with
|
|
41
|
+
> `pip install -e sdks/python`.
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install mitra-sdk # once released
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python 3.10+. The package imports as `mitra`.
|
|
48
|
+
|
|
49
|
+
Full documentation: [matih.ai/docs/api](https://matih.ai/docs/api/overview), with the [SDK reference](https://matih.ai/docs/api/sdk/python).
|
|
50
|
+
|
|
51
|
+
## Authenticate
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from mitra import Mitra
|
|
55
|
+
|
|
56
|
+
client = Mitra(api_key="mtk_...") # or set MATIH_API_KEY
|
|
57
|
+
client = Mitra(access_token="mto_...", workspace_id="ws_123") # OAuth token
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
API keys are pinned to one workspace. OAuth tokens pick a workspace per client with
|
|
61
|
+
`workspace_id=` (sent as `X-Matih-Workspace`) or per call, or set a default with
|
|
62
|
+
`client.workspaces.use(workspace_id)`. Keep keys in environment variables, never in source.
|
|
63
|
+
|
|
64
|
+
Admins create keys under `Settings › API & MCP`, choose the products the key may use (all on by default) and revoke keys in the same screen. Keys never expire by default. Use an API key here; an MCP connector key works only on `/api/v1/mcp`. Scopes: `workspaces:read`, `members:read`, `members:manage`, `sources:read`, `sources:write`, `files:read`, `files:write`, `gems:read`, `gems:generate`, `ask`, `dashboards:read`, `predict`, `embeds:create`, `embeds:read`, `webhooks:manage`.
|
|
65
|
+
|
|
66
|
+
## Ask
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
answer = client.ask("What were last month's sales by region?", language="en-IN")
|
|
70
|
+
|
|
71
|
+
if answer.status == "answered":
|
|
72
|
+
print(answer.answer)
|
|
73
|
+
for row in answer.result.rows:
|
|
74
|
+
print(row)
|
|
75
|
+
if answer.image_url:
|
|
76
|
+
open("chart.png", "wb").write(client.download_image(answer.image_url))
|
|
77
|
+
elif answer.status == "clarification":
|
|
78
|
+
print(answer.clarification.question, answer.clarification.options)
|
|
79
|
+
# reply in the same conversation
|
|
80
|
+
answer = client.ask(answer.clarification.options[0], conversation_id=answer.conversation_id)
|
|
81
|
+
|
|
82
|
+
follow_up = client.ask(answer.follow_ups[0], conversation_id=answer.conversation_id)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Questions may be in any of 22 Indian languages and are auto-detected; pass `language="hi-IN"` to
|
|
86
|
+
force the response language. Data values, column names and figures are never translated.
|
|
87
|
+
|
|
88
|
+
Voice notes: `answer = client.ask_audio("note.ogg", language="hi-IN")`; `answer.heard` holds the
|
|
89
|
+
transcript and detected language.
|
|
90
|
+
|
|
91
|
+
### Streaming
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
for event in client.ask_stream("Top 5 products by revenue"):
|
|
95
|
+
if event.event == "progress":
|
|
96
|
+
print("...", event.data)
|
|
97
|
+
elif event.event == "answer":
|
|
98
|
+
answer = event.as_answer()
|
|
99
|
+
elif event.event == "error":
|
|
100
|
+
raise RuntimeError(event.data)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Gems, dashboards, forecasts
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
gems = client.gems.list(source_id="src_1")
|
|
107
|
+
png = client.gems.image(gems.gems[0].id) # bytes
|
|
108
|
+
|
|
109
|
+
job = client.gems.generate("src_1") # waits, returns the finished Job
|
|
110
|
+
job = client.gems.generate("src_1", background=True) # returns the queued Job at once
|
|
111
|
+
job = client.jobs.wait(job, 600) # poll it later
|
|
112
|
+
|
|
113
|
+
dashboards = client.dashboards.list()
|
|
114
|
+
png = client.dashboards.render(dashboards.dashboards[0].id) # job + wait + download
|
|
115
|
+
|
|
116
|
+
job = client.predictions.create("Forecast next quarter's revenue", source_id="src_1")
|
|
117
|
+
print(job.result["answer"], job.result.get("attachments"))
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`jobs.wait(job, timeout=1860, poll_interval=1.0, max_poll_interval=15.0, on_update=fn)` raises
|
|
121
|
+
`JobTimeoutError` (the job keeps running) or `JobFailedError` (failed or cancelled). Predictions
|
|
122
|
+
can take up to 30 minutes. `ask` and `predictions.create` send an `Idempotency-Key` automatically
|
|
123
|
+
and reuse it across retries, so a retry is never charged twice (the server honours the key on
|
|
124
|
+
these two calls only). Pass `idempotency_key=` to control it.
|
|
125
|
+
|
|
126
|
+
## Sources and files
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
client.sources.list()
|
|
130
|
+
client.sources.get("src_1") # includes readiness
|
|
131
|
+
client.sources.connectors()
|
|
132
|
+
client.sources.test({"connector_type": "postgres", "display_name": "Orders DB", "host": "...", "database": "..."})
|
|
133
|
+
try:
|
|
134
|
+
client.sources.create({...}, activate=True) # admin, sources:write
|
|
135
|
+
except SourceTestFailedError as e: # HTTP 422
|
|
136
|
+
print(e.test_result)
|
|
137
|
+
client.sources.activate("src_1"); client.sources.delete("src_1")
|
|
138
|
+
|
|
139
|
+
# files: files:read to list and get; files:write to upload, answer a duplicate prompt and delete
|
|
140
|
+
f = client.files.upload("sales.csv", on_duplicate="ask") # CSV or Excel; "ask" (default) or "keep_both"
|
|
141
|
+
client.files.get(f.id) # status, decision_required, decision_options
|
|
142
|
+
client.files.duplicate(f.id, "use_existing") # answer a duplicate prompt: use_existing, keep_both or cancel
|
|
143
|
+
client.files.list(); client.files.delete(f.id)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Members and invitations
|
|
147
|
+
|
|
148
|
+
Needs `members:read` / `members:manage`; only do these when a person asked for them.
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
client.members.list()
|
|
152
|
+
client.invitations.create("user", email="new@example.com") # or phone="+91..."
|
|
153
|
+
client.invitations.list(); client.invitations.revoke("inv_1")
|
|
154
|
+
client.members.update("mem_1", "admin"); client.members.remove("mem_1")
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Embeds
|
|
158
|
+
|
|
159
|
+
Show a dashboard (with per-viewer row filters) or a Gems feed inside your own product. Needs
|
|
160
|
+
`embeds:create` (mint, revoke) or `embeds:read` (list). Call it from your backend: the API key
|
|
161
|
+
never goes to the browser. The browser gets only the short-lived token, through the
|
|
162
|
+
`<matih-embed>` element (`@matihlabs/embed`, see `sdks/embed/`).
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
embed = client.embeds.create(
|
|
166
|
+
resource={"type": "dashboard", "id": "d_123"}, # or {"type": "gems", "id": "<source id>"}
|
|
167
|
+
filters={"customer_id": "C123"}, # exactly the dashboard's embed parameters; none for Gems
|
|
168
|
+
origins=["https://app.customer.com"], # 1 to 10 exact origins that may frame the page
|
|
169
|
+
ttl_seconds=300, # 60 to 900, default 300; never renewed, mint again
|
|
170
|
+
)
|
|
171
|
+
embed.src # the iframe URL without the token (keeps ?e=emt_...)
|
|
172
|
+
embed.token # met_..., a secret; left out of repr
|
|
173
|
+
embed.id # emt_..., not secret: pass it to revoke
|
|
174
|
+
|
|
175
|
+
client.embeds.list() # tokens minted by this key (never the secret or filter values)
|
|
176
|
+
client.embeds.revoke(embed.id) # 204, idempotent, returns None
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Minting is not idempotent (every call mints) and is never retried after a 5xx. A refusal raises
|
|
180
|
+
`EmbedRefusedError` (422) with `.code` one of `missing_parameter`, `unknown_parameter`,
|
|
181
|
+
`invalid_value`, `invalid_origin`, `filters_not_supported` (Gems cannot be filtered),
|
|
182
|
+
`no_embed_parameters`; an unknown or unreadable dashboard or source raises `EmbedNotFoundError`
|
|
183
|
+
(404); a role that no longer allows it is a `PermissionDeniedError` (403). `AsyncMitra` is the
|
|
184
|
+
same with `await`.
|
|
185
|
+
|
|
186
|
+
## Webhooks
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
hook = client.webhooks.create("https://example.com/hooks/matih", ["prediction.completed"])
|
|
190
|
+
print(hook.secret) # whsec_..., shown once
|
|
191
|
+
client.webhooks.test(hook.id); client.webhooks.deliveries(hook.id)
|
|
192
|
+
|
|
193
|
+
# Deliveries carry Matih-Signature, Matih-Event and Matih-Delivery headers.
|
|
194
|
+
|
|
195
|
+
# In your web handler, with the RAW request body:
|
|
196
|
+
from mitra import verify_webhook
|
|
197
|
+
if not verify_webhook(secret, request.headers["Matih-Signature"], raw_body): # constant-time
|
|
198
|
+
return 400
|
|
199
|
+
# assert_webhook(...) is the same check but raises WebhookVerificationError with the reason.
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Sync vs async
|
|
203
|
+
|
|
204
|
+
Two independent choices: the client (`Mitra` blocks the thread, `AsyncMitra` is awaited) and the
|
|
205
|
+
mode of each long call (wait for the result, or get a job back and come back later). Both
|
|
206
|
+
clients have the same methods, including members, sources, files, webhooks, voice and jobs.
|
|
207
|
+
|
|
208
|
+
Every long call (`gems.generate`, `predictions.create`, `dashboards.image`, `sources.create`,
|
|
209
|
+
`files.upload`) takes:
|
|
210
|
+
|
|
211
|
+
- `wait=` seconds (default 10 minutes for Gems, 5 for images, sources and files, 31 for
|
|
212
|
+
predictions). The SDK sends `Prefer: wait=N` (the server honours at most 60 s for jobs, 120 s
|
|
213
|
+
for sources and files) and keeps polling until the result is final or `wait` runs out.
|
|
214
|
+
- Job calls (`gems.generate`, `predictions.create`, `dashboards.image`) poll the job. Running
|
|
215
|
+
out raises `JobTimeoutError`; the job keeps running.
|
|
216
|
+
- Resource calls (`sources.create`, `files.upload`) poll the source or the file. Running out
|
|
217
|
+
raises nothing: you get the source or file as it stands (for example `phase: building`), so
|
|
218
|
+
check `readiness.can_ask` or `status`.
|
|
219
|
+
- `background=True` to return right away: the queued `Job`, or for `sources.create` and
|
|
220
|
+
`files.upload` whatever the server answered. No `Prefer: wait` is sent.
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
job = client.predictions.create("Forecast Q4", wait=900) # blocks, finished Job
|
|
224
|
+
job = client.predictions.create("Forecast Q4", background=True) # returns immediately
|
|
225
|
+
job = client.jobs.wait(job.id, 900) # later, or from another process
|
|
226
|
+
|
|
227
|
+
queued = client.ask("Top customers this year", background=True) # Prefer: respond-async
|
|
228
|
+
done = client.jobs.wait(queued.id, 120)
|
|
229
|
+
answer = done.as_answer() # the Answer held in job.result
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
Without `background=True`, `ask` answers in one call. `predictions.run` and
|
|
233
|
+
`dashboards.render` are shortcuts for the waiting form (`render` also downloads the PNG).
|
|
234
|
+
|
|
235
|
+
## Async
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
import asyncio
|
|
239
|
+
from mitra import AsyncMitra
|
|
240
|
+
|
|
241
|
+
async def main():
|
|
242
|
+
async with AsyncMitra(api_key="mtk_...") as client:
|
|
243
|
+
answer = await client.ask("How many orders today?")
|
|
244
|
+
job = await client.predictions.create("Forecast Q4", background=True)
|
|
245
|
+
job = await client.jobs.wait(job.id, 900)
|
|
246
|
+
async for event in client.ask_stream("Top customers"):
|
|
247
|
+
print(event.event)
|
|
248
|
+
|
|
249
|
+
asyncio.run(main())
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Errors
|
|
253
|
+
|
|
254
|
+
All API errors derive from `MitraError` with `.status`, `.category`, `.code`, `.request_id` and
|
|
255
|
+
`.body`. Subclasses: `AuthError` (401), `PermissionDeniedError` (403: `scope`, `workspace_role`,
|
|
256
|
+
`source_access`), `CreditsError` (402: `not_ready`, `member_cap`), `BusyError` (409, a conflict with the current state),
|
|
257
|
+
`RateLimitError` (429, `.retry_after`), `EmbedRefusedError` (422 from `embeds.create`, read `.code`), `EmbedNotFoundError` (404 from `embeds.create` and `embeds.revoke`), `SourceTestFailedError` (422 from `sources.create`, `.test_result`), plus `JobFailedError`, `JobTimeoutError`. 429 is retried
|
|
258
|
+
automatically (honouring `Retry-After`) up to `max_retries` (default 2), as are 5xx and network
|
|
259
|
+
errors on GETs and on calls with an idempotency key.
|
|
260
|
+
|
|
261
|
+
## Models
|
|
262
|
+
|
|
263
|
+
`mitra.models` holds pydantic models for the main shapes (`Answer`, `Job`, `Gem`, `Workspace`, ...).
|
|
264
|
+
They accept unknown fields, so new server fields never break old clients. See
|
|
265
|
+
[`scripts/README.md`](scripts/README.md) for generating models from the OpenAPI spec.
|
|
266
|
+
|
|
267
|
+
## Develop
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
pip install -e "sdks/python[dev]"
|
|
271
|
+
pytest && ruff check .
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
License: Apache-2.0.
|