tensoract-sdk 1.0.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.
- tensoract_sdk-1.0.0/PKG-INFO +94 -0
- tensoract_sdk-1.0.0/README.md +71 -0
- tensoract_sdk-1.0.0/pyproject.toml +37 -0
- tensoract_sdk-1.0.0/setup.cfg +4 -0
- tensoract_sdk-1.0.0/setup.py +3 -0
- tensoract_sdk-1.0.0/tensoract_client/__init__.py +40 -0
- tensoract_sdk-1.0.0/tensoract_client/client.py +253 -0
- tensoract_sdk-1.0.0/tensoract_client/errors.py +33 -0
- tensoract_sdk-1.0.0/tensoract_client/models.py +1533 -0
- tensoract_sdk-1.0.0/tensoract_client/py.typed +0 -0
- tensoract_sdk-1.0.0/tensoract_sdk.egg-info/PKG-INFO +94 -0
- tensoract_sdk-1.0.0/tensoract_sdk.egg-info/SOURCES.txt +12 -0
- tensoract_sdk-1.0.0/tensoract_sdk.egg-info/dependency_links.txt +1 -0
- tensoract_sdk-1.0.0/tensoract_sdk.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tensoract-sdk
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Official Python SDK for the Tensoract Platform Public API
|
|
5
|
+
Author: Tensoract
|
|
6
|
+
Project-URL: Homepage, https://tensoract.com
|
|
7
|
+
Project-URL: Documentation, https://tensoract.com/docs
|
|
8
|
+
Keywords: tensoract,sdk,api-client,annotation,ai
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.8
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Tensoract Python SDK
|
|
25
|
+
|
|
26
|
+
The official Python client for the Tensoract Public API.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install tensoract-sdk
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
For local development from source:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install -e ./sdks/python
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Authentication
|
|
41
|
+
|
|
42
|
+
### 1. API Key Authentication (`x-api-key`)
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from tensoract_client import TensoractClient
|
|
46
|
+
|
|
47
|
+
# Defaults to production API (https://api.tensoract.com)
|
|
48
|
+
client = TensoractClient(
|
|
49
|
+
api_key="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
users = client.users.list()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
To target a custom or self-hosted deployment:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
client = TensoractClient(
|
|
59
|
+
api_key="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
60
|
+
base_url="https://api.tensoract.com",
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 2. PAT Bearer Token Authentication
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from tensoract_client import TensoractClient
|
|
68
|
+
|
|
69
|
+
client = TensoractClient(
|
|
70
|
+
token="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
user = client.users.get(1)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Usage Examples
|
|
77
|
+
|
|
78
|
+
### Users API
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
# Create a user (expects 'name' and 'roles' array, not firstName/lastName/role)
|
|
82
|
+
new_user = client.users.create({
|
|
83
|
+
"name": "Alice Annotator",
|
|
84
|
+
"email": "alice@company.com",
|
|
85
|
+
"password": "TemporaryPassword123!",
|
|
86
|
+
"roles": ["ANNOTATOR"],
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
# List users
|
|
90
|
+
users = client.users.list(page=1, limit=10)
|
|
91
|
+
|
|
92
|
+
# Get a user
|
|
93
|
+
user = client.users.get(new_user["id"])
|
|
94
|
+
```
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Tensoract Python SDK
|
|
2
|
+
|
|
3
|
+
The official Python client for the Tensoract Public API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install tensoract-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
For local development from source:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install -e ./sdks/python
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Authentication
|
|
18
|
+
|
|
19
|
+
### 1. API Key Authentication (`x-api-key`)
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from tensoract_client import TensoractClient
|
|
23
|
+
|
|
24
|
+
# Defaults to production API (https://api.tensoract.com)
|
|
25
|
+
client = TensoractClient(
|
|
26
|
+
api_key="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
users = client.users.list()
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
To target a custom or self-hosted deployment:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
client = TensoractClient(
|
|
36
|
+
api_key="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
37
|
+
base_url="https://api.tensoract.com",
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 2. PAT Bearer Token Authentication
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from tensoract_client import TensoractClient
|
|
45
|
+
|
|
46
|
+
client = TensoractClient(
|
|
47
|
+
token="fmn_live_xxxxxxxxxxxxxxxxxxxx",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
user = client.users.get(1)
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Usage Examples
|
|
54
|
+
|
|
55
|
+
### Users API
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
# Create a user (expects 'name' and 'roles' array, not firstName/lastName/role)
|
|
59
|
+
new_user = client.users.create({
|
|
60
|
+
"name": "Alice Annotator",
|
|
61
|
+
"email": "alice@company.com",
|
|
62
|
+
"password": "TemporaryPassword123!",
|
|
63
|
+
"roles": ["ANNOTATOR"],
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
# List users
|
|
67
|
+
users = client.users.list(page=1, limit=10)
|
|
68
|
+
|
|
69
|
+
# Get a user
|
|
70
|
+
user = client.users.get(new_user["id"])
|
|
71
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tensoract-sdk"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Official Python SDK for the Tensoract Platform Public API"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
authors = [{ name = "Tensoract" }]
|
|
12
|
+
keywords = ["tensoract", "sdk", "api-client", "annotation", "ai"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 5 - Production/Stable",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.8",
|
|
19
|
+
"Programming Language :: Python :: 3.9",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
25
|
+
"Typing :: Typed",
|
|
26
|
+
]
|
|
27
|
+
dependencies = []
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://tensoract.com"
|
|
31
|
+
Documentation = "https://tensoract.com/docs"
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
include = ["tensoract_client*"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.package-data]
|
|
37
|
+
tensoract_client = ["py.typed"]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Auto-generated by Tensoract SDK Generator
|
|
2
|
+
"""Tensoract Python SDK Package."""
|
|
3
|
+
from .client import (
|
|
4
|
+
TensoractClient,
|
|
5
|
+
UsersApi,
|
|
6
|
+
ProjectsApi,
|
|
7
|
+
DatasetsApi,
|
|
8
|
+
FilesApi,
|
|
9
|
+
ApiKeysApi,
|
|
10
|
+
TasksApi,
|
|
11
|
+
TemplatesApi,
|
|
12
|
+
WorkflowsApi,
|
|
13
|
+
)
|
|
14
|
+
from .errors import (
|
|
15
|
+
TensoractError,
|
|
16
|
+
AuthenticationError,
|
|
17
|
+
PermissionError,
|
|
18
|
+
NotFoundError,
|
|
19
|
+
ValidationError,
|
|
20
|
+
ApiError,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__version__ = "1.0.0"
|
|
24
|
+
__all__ = [
|
|
25
|
+
"TensoractClient",
|
|
26
|
+
"UsersApi",
|
|
27
|
+
"ProjectsApi",
|
|
28
|
+
"DatasetsApi",
|
|
29
|
+
"FilesApi",
|
|
30
|
+
"ApiKeysApi",
|
|
31
|
+
"TasksApi",
|
|
32
|
+
"TemplatesApi",
|
|
33
|
+
"WorkflowsApi",
|
|
34
|
+
"TensoractError",
|
|
35
|
+
"AuthenticationError",
|
|
36
|
+
"PermissionError",
|
|
37
|
+
"NotFoundError",
|
|
38
|
+
"ValidationError",
|
|
39
|
+
"ApiError",
|
|
40
|
+
]
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Auto-generated by Tensoract SDK Generator
|
|
2
|
+
"""Tensoract API Client supporting Bearer PAT and x-api-key authentication."""
|
|
3
|
+
from typing import Any, Dict, List, Optional, Union
|
|
4
|
+
import json
|
|
5
|
+
import urllib.parse
|
|
6
|
+
import urllib.request
|
|
7
|
+
import urllib.error
|
|
8
|
+
|
|
9
|
+
from .errors import (
|
|
10
|
+
TensoractError,
|
|
11
|
+
AuthenticationError,
|
|
12
|
+
PermissionError,
|
|
13
|
+
NotFoundError,
|
|
14
|
+
ValidationError,
|
|
15
|
+
ApiError,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
class BaseApiResource:
|
|
19
|
+
def __init__(self, client: "TensoractClient"):
|
|
20
|
+
self.client = client
|
|
21
|
+
|
|
22
|
+
class UsersApi(BaseApiResource):
|
|
23
|
+
def list(self, page: int = 1, limit: int = 25, search: Optional[str] = None, sort_by: Optional[str] = None, sort_order: Optional[str] = None) -> Dict[str, Any]:
|
|
24
|
+
"""List users with pagination."""
|
|
25
|
+
params = {"page": page, "limit": limit}
|
|
26
|
+
if search:
|
|
27
|
+
params["search"] = search
|
|
28
|
+
if sort_by:
|
|
29
|
+
params["sortBy"] = sort_by
|
|
30
|
+
if sort_order:
|
|
31
|
+
params["sortOrder"] = sort_order
|
|
32
|
+
return self.client.request("GET", "/users", params=params)
|
|
33
|
+
|
|
34
|
+
def get(self, user_id: int) -> Dict[str, Any]:
|
|
35
|
+
"""Get a single user by ID."""
|
|
36
|
+
return self.client.request("GET", f"/users/{user_id}")
|
|
37
|
+
|
|
38
|
+
def create(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
39
|
+
"""Create a new user (POST /users).
|
|
40
|
+
|
|
41
|
+
Expected schema:
|
|
42
|
+
name (str): Full name (required)
|
|
43
|
+
email (str): Email address (required)
|
|
44
|
+
roles (List[str]): Array of roles, e.g. ["ANNOTATOR"] (required/recommended)
|
|
45
|
+
password (Optional[str]): Password (min 8 chars)
|
|
46
|
+
"""
|
|
47
|
+
return self.client.request("POST", "/users", data=data)
|
|
48
|
+
|
|
49
|
+
def update(self, user_id: int, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
50
|
+
"""Update a user by ID (PATCH /users/:id)."""
|
|
51
|
+
return self.client.request("PATCH", f"/users/{user_id}", data=data)
|
|
52
|
+
|
|
53
|
+
def update_roles(self, user_id: int, roles: List[str]) -> Dict[str, Any]:
|
|
54
|
+
"""Update roles for a user (PATCH /users/:id/roles)."""
|
|
55
|
+
return self.client.request("PATCH", f"/users/{user_id}/roles", data={"roles": roles})
|
|
56
|
+
|
|
57
|
+
def delete(self, user_id: int) -> Dict[str, Any]:
|
|
58
|
+
"""Delete a single user by ID (DELETE /users/:id)."""
|
|
59
|
+
return self.client.request("DELETE", f"/users/{user_id}")
|
|
60
|
+
|
|
61
|
+
class ProjectsApi(BaseApiResource):
|
|
62
|
+
def list(self, page: int = 1, limit: int = 25, search: Optional[str] = None) -> Dict[str, Any]:
|
|
63
|
+
"""List all projects."""
|
|
64
|
+
params = {"page": page, "limit": limit}
|
|
65
|
+
if search:
|
|
66
|
+
params["search"] = search
|
|
67
|
+
return self.client.request("GET", "/projects", params=params)
|
|
68
|
+
|
|
69
|
+
def get(self, project_id: int) -> Dict[str, Any]:
|
|
70
|
+
"""Get project details."""
|
|
71
|
+
return self.client.request("GET", f"/projects/{project_id}")
|
|
72
|
+
|
|
73
|
+
def create(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
74
|
+
"""Create a new project."""
|
|
75
|
+
return self.client.request("POST", "/projects", data=data)
|
|
76
|
+
|
|
77
|
+
def bulk_delete(self, project_ids: List[int]) -> Dict[str, Any]:
|
|
78
|
+
"""Bulk-delete projects (DELETE /projects). No single-project DELETE endpoint exists."""
|
|
79
|
+
return self.client.request("DELETE", "/projects", data={"projectIds": project_ids})
|
|
80
|
+
class DatasetsApi(BaseApiResource):
|
|
81
|
+
def list(self, page: int = 1, limit: int = 25) -> Dict[str, Any]:
|
|
82
|
+
"""List all datasets."""
|
|
83
|
+
return self.client.request("GET", "/datasets", params={"page": page, "limit": limit})
|
|
84
|
+
|
|
85
|
+
def get(self, dataset_id: int) -> Dict[str, Any]:
|
|
86
|
+
"""Get dataset details."""
|
|
87
|
+
return self.client.request("GET", f"/datasets/{dataset_id}")
|
|
88
|
+
|
|
89
|
+
def create(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
90
|
+
"""Create a new dataset."""
|
|
91
|
+
return self.client.request("POST", "/datasets", data=data)
|
|
92
|
+
|
|
93
|
+
class FilesApi(BaseApiResource):
|
|
94
|
+
def list(self, page: int = 1, limit: int = 25, sort_by: Optional[str] = None, sort_order: Optional[str] = None, search: Optional[str] = None, type: Optional[str] = None) -> Dict[str, Any]:
|
|
95
|
+
"""List files. Supported params: page, limit, sort_by (sortBy), sort_order (sortOrder), search, type.
|
|
96
|
+
Note: filtering by dataset_id is not supported on GET /files. Use GET /datasets/:id/files instead."""
|
|
97
|
+
params = {"page": page, "limit": limit}
|
|
98
|
+
if sort_by:
|
|
99
|
+
params["sortBy"] = sort_by
|
|
100
|
+
if sort_order:
|
|
101
|
+
params["sortOrder"] = sort_order
|
|
102
|
+
if search:
|
|
103
|
+
params["search"] = search
|
|
104
|
+
if type:
|
|
105
|
+
params["type"] = type
|
|
106
|
+
return self.client.request("GET", "/files", params=params)
|
|
107
|
+
|
|
108
|
+
def get(self, file_id: int) -> Dict[str, Any]:
|
|
109
|
+
"""Get file metadata."""
|
|
110
|
+
return self.client.request("GET", f"/files/{file_id}")
|
|
111
|
+
|
|
112
|
+
def delete(self, file_id: int) -> Dict[str, Any]:
|
|
113
|
+
"""Delete a file."""
|
|
114
|
+
return self.client.request("DELETE", f"/files/{file_id}")
|
|
115
|
+
|
|
116
|
+
class ApiKeysApi(BaseApiResource):
|
|
117
|
+
def list(self) -> List[Dict[str, Any]]:
|
|
118
|
+
"""List API keys."""
|
|
119
|
+
return self.client.request("GET", "/api-keys")
|
|
120
|
+
|
|
121
|
+
def create(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
122
|
+
"""Create an API key."""
|
|
123
|
+
return self.client.request("POST", "/api-keys", data=data)
|
|
124
|
+
|
|
125
|
+
def revoke(self, key_id: int) -> Dict[str, Any]:
|
|
126
|
+
"""Revoke an API key."""
|
|
127
|
+
return self.client.request("PATCH", f"/api-keys/{key_id}/revoke")
|
|
128
|
+
|
|
129
|
+
class TasksApi(BaseApiResource):
|
|
130
|
+
def get(self, task_id: int) -> Dict[str, Any]:
|
|
131
|
+
"""Get task details."""
|
|
132
|
+
return self.client.request("GET", f"/tasks/{task_id}")
|
|
133
|
+
|
|
134
|
+
def approve(self, task_id: int) -> Dict[str, Any]:
|
|
135
|
+
"""Approve a task."""
|
|
136
|
+
return self.client.request("PATCH", f"/tasks/{task_id}/approve")
|
|
137
|
+
|
|
138
|
+
def reject(self, task_id: int, comment: Optional[str] = None) -> Dict[str, Any]:
|
|
139
|
+
"""Reject a task. The backend expects 'comment', not 'reason'."""
|
|
140
|
+
return self.client.request("PATCH", f"/tasks/{task_id}/reject", data={"comment": comment} if comment else None)
|
|
141
|
+
|
|
142
|
+
class TemplatesApi(BaseApiResource):
|
|
143
|
+
def list(self) -> List[Dict[str, Any]]:
|
|
144
|
+
"""List templates."""
|
|
145
|
+
return self.client.request("GET", "/templates")
|
|
146
|
+
|
|
147
|
+
def get(self, template_id: Union[int, str]) -> Dict[str, Any]:
|
|
148
|
+
"""Get template details."""
|
|
149
|
+
return self.client.request("GET", f"/templates/{template_id}")
|
|
150
|
+
|
|
151
|
+
class WorkflowsApi(BaseApiResource):
|
|
152
|
+
def list(self) -> List[Dict[str, Any]]:
|
|
153
|
+
"""List workflows."""
|
|
154
|
+
return self.client.request("GET", "/workflows")
|
|
155
|
+
|
|
156
|
+
def get(self, workflow_id: int) -> Dict[str, Any]:
|
|
157
|
+
"""Get workflow details."""
|
|
158
|
+
return self.client.request("GET", f"/workflows/{workflow_id}")
|
|
159
|
+
|
|
160
|
+
class TensoractClient:
|
|
161
|
+
"""Tensoract API Client."""
|
|
162
|
+
def __init__(
|
|
163
|
+
self,
|
|
164
|
+
api_key: Optional[str] = None,
|
|
165
|
+
token: Optional[str] = None,
|
|
166
|
+
tenant_id: Optional[Union[int, str]] = None,
|
|
167
|
+
base_url: str = "https://api.tensoract.com",
|
|
168
|
+
timeout: int = 30,
|
|
169
|
+
):
|
|
170
|
+
if not api_key and not token:
|
|
171
|
+
raise ValueError("Either 'api_key' or 'token' must be provided.")
|
|
172
|
+
|
|
173
|
+
self.api_key = api_key
|
|
174
|
+
self.token = token
|
|
175
|
+
self.tenant_id = tenant_id
|
|
176
|
+
self.base_url = base_url.rstrip("/")
|
|
177
|
+
self.timeout = timeout
|
|
178
|
+
|
|
179
|
+
self.users = UsersApi(self)
|
|
180
|
+
self.projects = ProjectsApi(self)
|
|
181
|
+
self.datasets = DatasetsApi(self)
|
|
182
|
+
self.files = FilesApi(self)
|
|
183
|
+
self.api_keys = ApiKeysApi(self)
|
|
184
|
+
self.tasks = TasksApi(self)
|
|
185
|
+
self.templates = TemplatesApi(self)
|
|
186
|
+
self.workflows = WorkflowsApi(self)
|
|
187
|
+
|
|
188
|
+
def request(
|
|
189
|
+
self,
|
|
190
|
+
method: str,
|
|
191
|
+
path: str,
|
|
192
|
+
params: Optional[Dict[str, Any]] = None,
|
|
193
|
+
data: Optional[Dict[str, Any]] = None,
|
|
194
|
+
) -> Any:
|
|
195
|
+
url = f"{self.base_url}{path}"
|
|
196
|
+
if params:
|
|
197
|
+
query = urllib.parse.urlencode({k: v for k, v in params.items() if v is not None})
|
|
198
|
+
if query:
|
|
199
|
+
url = f"{url}?{query}"
|
|
200
|
+
|
|
201
|
+
headers = {
|
|
202
|
+
"Accept": "application/json",
|
|
203
|
+
"Content-Type": "application/json",
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if self.token:
|
|
207
|
+
prefix = "" if self.token.startswith("Bearer ") else "Bearer "
|
|
208
|
+
headers["Authorization"] = f"{prefix}{self.token}"
|
|
209
|
+
|
|
210
|
+
if self.api_key:
|
|
211
|
+
headers["x-api-key"] = self.api_key
|
|
212
|
+
|
|
213
|
+
if self.tenant_id is not None:
|
|
214
|
+
headers["x-tenant-id"] = str(self.tenant_id)
|
|
215
|
+
|
|
216
|
+
req_body = json.dumps(data).encode("utf-8") if data is not None else None
|
|
217
|
+
req = urllib.request.Request(url, data=req_body, headers=headers, method=method)
|
|
218
|
+
|
|
219
|
+
try:
|
|
220
|
+
with urllib.request.urlopen(req, timeout=self.timeout) as response:
|
|
221
|
+
body = response.read().decode("utf-8")
|
|
222
|
+
if not body:
|
|
223
|
+
return None
|
|
224
|
+
try:
|
|
225
|
+
return json.loads(body)
|
|
226
|
+
except Exception:
|
|
227
|
+
return body
|
|
228
|
+
except urllib.error.HTTPError as e:
|
|
229
|
+
status = e.code
|
|
230
|
+
err_body = e.read().decode("utf-8")
|
|
231
|
+
parsed_body = None
|
|
232
|
+
msg = str(e)
|
|
233
|
+
try:
|
|
234
|
+
parsed_body = json.loads(err_body)
|
|
235
|
+
if isinstance(parsed_body, dict):
|
|
236
|
+
msg = parsed_body.get("message") or msg
|
|
237
|
+
if isinstance(msg, list):
|
|
238
|
+
msg = "; ".join(msg)
|
|
239
|
+
except Exception:
|
|
240
|
+
pass
|
|
241
|
+
|
|
242
|
+
if status == 400:
|
|
243
|
+
raise ValidationError(msg, status_code=status, response_body=parsed_body)
|
|
244
|
+
elif status == 401:
|
|
245
|
+
raise AuthenticationError(msg, status_code=status, response_body=parsed_body)
|
|
246
|
+
elif status == 403:
|
|
247
|
+
raise PermissionError(msg, status_code=status, response_body=parsed_body)
|
|
248
|
+
elif status == 404:
|
|
249
|
+
raise NotFoundError(msg, status_code=status, response_body=parsed_body)
|
|
250
|
+
else:
|
|
251
|
+
raise ApiError(msg, status_code=status, response_body=parsed_body)
|
|
252
|
+
except Exception as e:
|
|
253
|
+
raise TensoractError(str(e))
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Auto-generated by Tensoract SDK Generator
|
|
2
|
+
"""Error hierarchy for Tensoract SDK."""
|
|
3
|
+
|
|
4
|
+
class TensoractError(Exception):
|
|
5
|
+
"""Base exception for all Tensoract SDK errors."""
|
|
6
|
+
def __init__(self, message: str, status_code: int = 0, response_body: any = None):
|
|
7
|
+
super().__init__(message)
|
|
8
|
+
self.message = message
|
|
9
|
+
self.status_code = status_code
|
|
10
|
+
self.response_body = response_body
|
|
11
|
+
|
|
12
|
+
def __str__(self):
|
|
13
|
+
return f"{self.__class__.__name__}(status={self.status_code}, message={self.message})"
|
|
14
|
+
|
|
15
|
+
class AuthenticationError(TensoractError):
|
|
16
|
+
"""Raised when authentication fails (HTTP 401)."""
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
class PermissionError(TensoractError):
|
|
20
|
+
"""Raised when access is forbidden (HTTP 403)."""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
class NotFoundError(TensoractError):
|
|
24
|
+
"""Raised when a resource is not found (HTTP 404)."""
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
class ValidationError(TensoractError):
|
|
28
|
+
"""Raised when input validation fails (HTTP 400)."""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
class ApiError(TensoractError):
|
|
32
|
+
"""Raised for server and unexpected HTTP errors."""
|
|
33
|
+
pass
|