truto-python-sdk 0.2.0__tar.gz → 0.3.1__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.
Files changed (51) hide show
  1. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/PKG-INFO +18 -1
  2. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/README.md +17 -0
  3. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/pyproject.toml +1 -1
  4. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/__init__.py +3 -2
  5. truto_python_sdk-0.3.1/truto_python_sdk/errors.py +106 -0
  6. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/truto_api.py +9 -6
  7. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/LICENSE +0 -0
  8. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/alarm.py +0 -0
  9. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/analytics.py +0 -0
  10. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/api_token.py +0 -0
  11. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/base_resource.py +0 -0
  12. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/batch_request.py +0 -0
  13. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/category.py +0 -0
  14. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/custom_api.py +0 -0
  15. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/daemon.py +0 -0
  16. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/daemon_job.py +0 -0
  17. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/daemon_job_cron_trigger.py +0 -0
  18. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/daemon_job_run.py +0 -0
  19. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/datastore.py +0 -0
  20. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/dict_to_query_string.py +0 -0
  21. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/documentation.py +0 -0
  22. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/environment.py +0 -0
  23. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/environment_integration.py +0 -0
  24. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/environment_integration_webhook.py +0 -0
  25. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/environment_unified_model.py +0 -0
  26. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/environment_unified_model_resource_method.py +0 -0
  27. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/integrated_account.py +0 -0
  28. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/integrated_account_webhook.py +0 -0
  29. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/integration.py +0 -0
  30. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/link_token.py +0 -0
  31. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/log.py +0 -0
  32. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/mcp.py +0 -0
  33. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/observability_destination.py +0 -0
  34. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/pagination_result.py +0 -0
  35. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/proxy_api.py +0 -0
  36. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sandbox_integrated_account.py +0 -0
  37. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/static_gate.py +0 -0
  38. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sync_job.py +0 -0
  39. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sync_job_cron_trigger.py +0 -0
  40. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sync_job_run.py +0 -0
  41. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sync_job_run_state.py +0 -0
  42. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/sync_job_template.py +0 -0
  43. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/team.py +0 -0
  44. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/tenant.py +0 -0
  45. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/unified_api.py +0 -0
  46. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/unified_model.py +0 -0
  47. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/unified_model_resource_method.py +0 -0
  48. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/user.py +0 -0
  49. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/webhook.py +0 -0
  50. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/workflow.py +0 -0
  51. {truto_python_sdk-0.2.0 → truto_python_sdk-0.3.1}/truto_python_sdk/workflow_run.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: truto-python-sdk
3
- Version: 0.2.0
3
+ Version: 0.3.1
4
4
  Summary: Python3 SDK for the Truto API
5
5
  License-File: LICENSE
6
6
  Author: Roopendra Talekar
@@ -111,6 +111,23 @@ page = await truto_api.unified_api.list(
111
111
  ).next_page()
112
112
  ```
113
113
 
114
+ ## Errors
115
+
116
+ A 4xx or 5xx response raises `TrutoApiError`. It subclasses `aiohttp.ClientResponseError`, so `.status`, `.message` and `.headers` work as before, and it also carries the response body Truto sent:
117
+
118
+ ```python
119
+ from truto_python_sdk import TrutoApiError
120
+
121
+ try:
122
+ await truto_api.integrated_accounts.get("missing-id")
123
+ except TrutoApiError as e:
124
+ print(e.status) # 404
125
+ print(e.error) # Truto's error message, if the body is JSON
126
+ print(e.truto_error_insight) # extra detail Truto attaches to some errors, or None
127
+ print(e.data) # the full parsed JSON body, or None
128
+ print(e.body) # the raw body as text
129
+ ```
130
+
114
131
  ## Full reference
115
132
 
116
133
  Every resource and method, with examples, is documented at **[truto.one/docs/sdks/python](https://truto.one/docs/sdks/python)**: the Unified, Proxy and Custom APIs, MCP, integrated accounts, tenants, integrations, unified models and mappings, sync jobs, webhooks, workflows, datastores, analytics, and more.
@@ -91,6 +91,23 @@ page = await truto_api.unified_api.list(
91
91
  ).next_page()
92
92
  ```
93
93
 
94
+ ## Errors
95
+
96
+ A 4xx or 5xx response raises `TrutoApiError`. It subclasses `aiohttp.ClientResponseError`, so `.status`, `.message` and `.headers` work as before, and it also carries the response body Truto sent:
97
+
98
+ ```python
99
+ from truto_python_sdk import TrutoApiError
100
+
101
+ try:
102
+ await truto_api.integrated_accounts.get("missing-id")
103
+ except TrutoApiError as e:
104
+ print(e.status) # 404
105
+ print(e.error) # Truto's error message, if the body is JSON
106
+ print(e.truto_error_insight) # extra detail Truto attaches to some errors, or None
107
+ print(e.data) # the full parsed JSON body, or None
108
+ print(e.body) # the raw body as text
109
+ ```
110
+
94
111
  ## Full reference
95
112
 
96
113
  Every resource and method, with examples, is documented at **[truto.one/docs/sdks/python](https://truto.one/docs/sdks/python)**: the Unified, Proxy and Custom APIs, MCP, integrated accounts, tenants, integrations, unified models and mappings, sync jobs, webhooks, workflows, datastores, analytics, and more.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "truto-python-sdk"
3
- version = "0.2.0"
3
+ version = "0.3.1"
4
4
  description = "Python3 SDK for the Truto API"
5
5
  authors = ["Roopendra Talekar <roopendratalekar@gmail.com>"]
6
6
  readme = "README.md"
@@ -8,9 +8,10 @@ A Python3 SDK for the Truto API.
8
8
  """
9
9
 
10
10
  __title__ = 'truto_python_sdk'
11
- __version__ = '0.1.0'
11
+ __version__ = '0.3.1'
12
12
  __author__ = 'Roopendra Talekar <roopendra@truto.dev>'
13
13
  __license__ = 'MIT'
14
14
  __copyright__ = 'Copyright 2023 Yin Yang Inc.'
15
15
 
16
- from .truto_api import TrutoApi
16
+ from .truto_api import TrutoApi
17
+ from .errors import TrutoApiError
@@ -0,0 +1,106 @@
1
+ import asyncio
2
+ import codecs
3
+ import json
4
+
5
+ import aiohttp
6
+
7
+ # Error bodies are kept on the exception, so cap how much of one we read.
8
+ MAX_ERROR_BODY_BYTES = 64 * 1024
9
+
10
+
11
+ class TrutoApiError(aiohttp.ClientResponseError):
12
+ """Raised when the Truto API responds with a 4xx or 5xx status.
13
+
14
+ Subclasses ``aiohttp.ClientResponseError``, so existing ``except
15
+ aiohttp.ClientResponseError`` handlers keep working, and ``.status``,
16
+ ``.message``, ``.headers`` and ``.request_info`` are unchanged. On top of
17
+ that it carries the response body Truto sent back:
18
+
19
+ - ``body``: the raw response body as text (``None`` if it could not be read)
20
+ - ``data``: the body parsed as JSON, or ``None`` if it isn't JSON
21
+ """
22
+
23
+ def __init__(
24
+ self,
25
+ request_info,
26
+ history,
27
+ *,
28
+ status=None,
29
+ message="",
30
+ headers=None,
31
+ body=None,
32
+ data=None,
33
+ ):
34
+ # Defaults match aiohttp.ClientResponseError: copy/pickle rebuild the
35
+ # error as ``cls(request_info, history)`` and then restore __dict__.
36
+ super().__init__(request_info, history, status=status, message=message, headers=headers)
37
+ self.body = body
38
+ self.data = data
39
+
40
+ @property
41
+ def error(self):
42
+ """Truto's error message (the ``error`` field of a JSON error body), if any."""
43
+ if isinstance(self.data, dict):
44
+ for key in ("error", "message"):
45
+ value = self.data.get(key)
46
+ if isinstance(value, str) and value:
47
+ return value
48
+ return None
49
+
50
+ @property
51
+ def truto_error_insight(self):
52
+ """Truto's ``truto_error_insight`` object from the error body, if any."""
53
+ if isinstance(self.data, dict):
54
+ return self.data.get("truto_error_insight")
55
+ return None
56
+
57
+ def __str__(self):
58
+ base = super().__str__()
59
+ detail = self.error
60
+ if detail is None and self.body:
61
+ detail = self.body[:500]
62
+ return f"{base}, body={detail!r}" if detail else base
63
+
64
+
65
+ def _codec(charset):
66
+ """The response's charset if Python knows it, else utf-8."""
67
+ if charset:
68
+ try:
69
+ return codecs.lookup(charset).name
70
+ except LookupError:
71
+ pass
72
+ return "utf-8"
73
+
74
+
75
+ async def raise_for_status(response: aiohttp.ClientResponse):
76
+ """Like ``response.raise_for_status()`` but reads the body first so the
77
+ raised ``TrutoApiError`` carries Truto's error message."""
78
+ if response.ok:
79
+ return
80
+ body = None
81
+ data = None
82
+ try:
83
+ # read(n) returns as soon as anything is buffered, so a body sent in
84
+ # several chunks would be cut short. readexactly waits for n bytes and
85
+ # hands back what arrived when the body ends first.
86
+ try:
87
+ raw = await response.content.readexactly(MAX_ERROR_BODY_BYTES)
88
+ except asyncio.IncompleteReadError as e:
89
+ raw = e.partial
90
+ body = raw.decode(_codec(response.charset), errors="replace")
91
+ except Exception:
92
+ body = None
93
+ if body:
94
+ try:
95
+ data = json.loads(body)
96
+ except ValueError:
97
+ data = None
98
+ raise TrutoApiError(
99
+ response.request_info,
100
+ response.history,
101
+ status=response.status,
102
+ message=response.reason or "",
103
+ headers=response.headers,
104
+ body=body,
105
+ data=data,
106
+ )
@@ -1,6 +1,8 @@
1
1
  import aiohttp
2
2
 
3
+ from . import __version__
3
4
  from .dict_to_query_string import dict_to_query_string
5
+ from .errors import raise_for_status
4
6
  from .alarm import Alarm
5
7
  from .daemon import Daemon
6
8
  from .daemon_job import DaemonJob
@@ -51,7 +53,8 @@ class TrutoApi:
51
53
  self.headers = {
52
54
  "Authorization": f"Bearer {self.token}",
53
55
  "Content-Type": "application/json",
54
- "Accept": "application/json"
56
+ "Accept": "application/json",
57
+ "User-Agent": f"truto-python-sdk/{__version__}",
55
58
  }
56
59
  self.api_tokens = ApiToken(self)
57
60
  self.environments = Environment(self)
@@ -114,7 +117,7 @@ class TrutoApi:
114
117
  query_string = dict_to_query_string(params) if params else ""
115
118
  async with aiohttp.ClientSession() as session:
116
119
  async with session.get(f"{url}?{query_string}", headers=self.headers) as response:
117
- response.raise_for_status()
120
+ await raise_for_status(response)
118
121
  return await self._process_response(response)
119
122
 
120
123
  async def _post(self, endpoint, data=None, params=None):
@@ -122,7 +125,7 @@ class TrutoApi:
122
125
  query_string = dict_to_query_string(params) if params else ""
123
126
  async with aiohttp.ClientSession() as session:
124
127
  async with session.post(f"{url}?{query_string}", headers=self.headers, json=data) as response:
125
- response.raise_for_status()
128
+ await raise_for_status(response)
126
129
  return await self._process_response(response)
127
130
 
128
131
  async def _patch(self, endpoint, data=None, params=None):
@@ -130,7 +133,7 @@ class TrutoApi:
130
133
  query_string = dict_to_query_string(params) if params else ""
131
134
  async with aiohttp.ClientSession() as session:
132
135
  async with session.patch(f"{url}?{query_string}", headers=self.headers, json=data) as response:
133
- response.raise_for_status()
136
+ await raise_for_status(response)
134
137
  return await self._process_response(response)
135
138
 
136
139
  async def _put(self, endpoint, data=None, params=None):
@@ -138,7 +141,7 @@ class TrutoApi:
138
141
  query_string = dict_to_query_string(params) if params else ""
139
142
  async with aiohttp.ClientSession() as session:
140
143
  async with session.put(f"{url}?{query_string}", headers=self.headers, json=data) as response:
141
- response.raise_for_status()
144
+ await raise_for_status(response)
142
145
  return await self._process_response(response)
143
146
 
144
147
  async def _delete(self, endpoint, params=None):
@@ -146,7 +149,7 @@ class TrutoApi:
146
149
  query_string = dict_to_query_string(params) if params else ""
147
150
  async with aiohttp.ClientSession() as session:
148
151
  async with session.delete(f"{url}?{query_string}", headers=self.headers) as response:
149
- response.raise_for_status()
152
+ await raise_for_status(response)
150
153
  return await self._process_response(response)
151
154
 
152
155
  async def ping(self):