aidbox-python-sdk 0.2.2__tar.gz → 0.3.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.
Files changed (26) hide show
  1. {aidbox_python_sdk-0.2.2/aidbox_python_sdk.egg-info → aidbox_python_sdk-0.3.0}/PKG-INFO +6 -6
  2. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/README.md +2 -2
  3. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/__init__.py +2 -2
  4. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/db.py +41 -4
  5. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/handlers.py +13 -0
  6. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/pytest_plugin.py +2 -1
  7. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0/aidbox_python_sdk.egg-info}/PKG-INFO +6 -6
  8. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/pyproject.toml +3 -3
  9. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/tests/test_sdk.py +76 -2
  10. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/LICENSE.md +0 -0
  11. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/MANIFEST.in +0 -0
  12. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/aidboxpy.py +0 -0
  13. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/app_keys.py +0 -0
  14. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/db_migrations.py +0 -0
  15. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/exceptions.py +0 -0
  16. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/main.py +0 -0
  17. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/py.typed +0 -0
  18. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/sdk.py +0 -0
  19. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/settings.py +0 -0
  20. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk/types.py +0 -0
  21. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk.egg-info/SOURCES.txt +0 -0
  22. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk.egg-info/dependency_links.txt +0 -0
  23. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk.egg-info/not-zip-safe +0 -0
  24. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk.egg-info/requires.txt +0 -0
  25. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/aidbox_python_sdk.egg-info/top_level.txt +0 -0
  26. {aidbox_python_sdk-0.2.2 → aidbox_python_sdk-0.3.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aidbox-python-sdk
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Aidbox SDK for python
5
5
  Author-email: "beda.software" <aidbox-python-sdk@beda.software>
6
6
  License: MIT License
@@ -35,13 +35,13 @@ Classifier: Intended Audience :: Developers
35
35
  Classifier: Operating System :: OS Independent
36
36
  Classifier: Programming Language :: Python
37
37
  Classifier: Programming Language :: Python :: 3
38
- Classifier: Programming Language :: Python :: 3.9
39
- Classifier: Programming Language :: Python :: 3.10
40
38
  Classifier: Programming Language :: Python :: 3.11
41
39
  Classifier: Programming Language :: Python :: 3.12
40
+ Classifier: Programming Language :: Python :: 3.13
41
+ Classifier: Programming Language :: Python :: 3.14
42
42
  Classifier: Topic :: Internet :: WWW/HTTP
43
43
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
44
- Requires-Python: >=3.9
44
+ Requires-Python: >=3.11
45
45
  Description-Content-Type: text/markdown
46
46
  License-File: LICENSE.md
47
47
  Requires-Dist: aiohttp>=3.11.0
@@ -62,11 +62,11 @@ Dynamic: license-file
62
62
 
63
63
  [![build status](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml/badge.svg)](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml)
64
64
  [![pypi](https://img.shields.io/pypi/v/aidbox-python-sdk.svg)](https://pypi.org/project/aidbox-python-sdk/)
65
- [![Supported Python version](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
65
+ [![Supported Python version](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/release/python-3110/)
66
66
 
67
67
  # aidbox-python-sdk
68
68
 
69
- 1. Create a python 3.9+ environment `pyenv `
69
+ 1. Create a python 3.11+ environment `pyenv `
70
70
  2. Set env variables and activate virtual environment `source activate_settings.sh`
71
71
  2. Install the required packages with `pipenv install --dev`
72
72
  3. Make sure the app's settings are configured correctly (see `activate_settings.sh` and `aidbox_python_sdk/settings.py`). You can also
@@ -1,10 +1,10 @@
1
1
  [![build status](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml/badge.svg)](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml)
2
2
  [![pypi](https://img.shields.io/pypi/v/aidbox-python-sdk.svg)](https://pypi.org/project/aidbox-python-sdk/)
3
- [![Supported Python version](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
3
+ [![Supported Python version](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/release/python-3110/)
4
4
 
5
5
  # aidbox-python-sdk
6
6
 
7
- 1. Create a python 3.9+ environment `pyenv `
7
+ 1. Create a python 3.11+ environment `pyenv `
8
8
  2. Set env variables and activate virtual environment `source activate_settings.sh`
9
9
  2. Install the required packages with `pipenv install --dev`
10
10
  3. Make sure the app's settings are configured correctly (see `activate_settings.sh` and `aidbox_python_sdk/settings.py`). You can also
@@ -1,8 +1,8 @@
1
1
  __title__ = "aidbox-python-sdk"
2
- __version__ = "0.2.2"
2
+ __version__ = "0.3.0"
3
3
  __author__ = "beda.software"
4
4
  __license__ = "None"
5
- __copyright__ = "Copyright 2024 beda.software"
5
+ __copyright__ = "Copyright 2026 beda.software"
6
6
 
7
7
  # Version synonym
8
8
  VERSION = __version__
@@ -22,6 +22,36 @@ from aidbox_python_sdk.settings import Settings
22
22
 
23
23
  from .exceptions import AidboxDBException
24
24
 
25
+
26
+ def parse_psql_response(response):
27
+ """
28
+ Normalizes a $psql response into a list of result sets (one per statement).
29
+ Supports both formats:
30
+ - pre-2604: [{status, result: [row, ...], ...}]
31
+ - 2604+: {status, result: [{type, data: [row, ...]}, ...], ...}
32
+ Raises AidboxDBException on error status.
33
+ """
34
+ entry = response[0] if isinstance(response, list) else response
35
+
36
+ if entry.get("status") == "error":
37
+ raise AidboxDBException(entry)
38
+
39
+ result = entry.get("result")
40
+
41
+ if not isinstance(result, list):
42
+ return [result] # None or True (old format execute=true)
43
+
44
+ if not result:
45
+ return [result]
46
+
47
+ # New format (2604+): result items carry {type, data}
48
+ if isinstance(result[0], dict) and "data" in result[0]:
49
+ return [rset["data"] for rset in result]
50
+
51
+ # Old format: single flat list of rows
52
+ return [result]
53
+
54
+
25
55
  logger = logging.getLogger("aidbox_sdk.db")
26
56
  table_metadata = MetaData()
27
57
 
@@ -124,10 +154,17 @@ class DBProxy:
124
154
  ) as resp:
125
155
  logger.debug("$psql answer %s", await resp.text())
126
156
  results = await resp.json()
127
-
128
- if results[0]["status"] == "error":
129
- raise AidboxDBException(results[0])
130
- return results[0].get("result", None)
157
+ result = parse_psql_response(results)
158
+ if execute:
159
+ return True
160
+ if len(result) > 1:
161
+ raise AidboxDBException(
162
+ {
163
+ "status": "error",
164
+ "error": "Multiple result sets returned; use execute=True for multi-statement queries",
165
+ }
166
+ )
167
+ return result[0]
131
168
 
132
169
  def compile_statement(self, statement):
133
170
  return str(
@@ -1,5 +1,7 @@
1
1
  import asyncio
2
+ import base64
2
3
  import logging
4
+ import secrets
3
5
  from typing import Any
4
6
 
5
7
  from aiohttp import web
@@ -11,6 +13,13 @@ logger = logging.getLogger("aidbox_sdk")
11
13
  routes = web.RouteTableDef()
12
14
 
13
15
 
16
+ def is_from_aidbox(request: web.Request) -> bool:
17
+ """Aidbox calls an http-rpc endpoint with the secret the app registered it under."""
18
+ settings = request.app[ak.settings]
19
+ expected = b"Basic " + base64.b64encode(f"{settings.APP_ID}:{settings.APP_SECRET}".encode())
20
+ return secrets.compare_digest(request.headers.get("Authorization", "").encode(), expected)
21
+
22
+
14
23
  async def subscription(request: web.Request, data: dict):
15
24
  logger.debug("Subscription handler: %s", data["handler"])
16
25
  if "handler" not in data or "event" not in data:
@@ -59,6 +68,10 @@ TYPES = {
59
68
  @routes.post("/aidbox")
60
69
  async def dispatch(request):
61
70
  logger.debug("Dispatch new request %s %s", request.method, request.url)
71
+ if not is_from_aidbox(request):
72
+ logger.error("Dispatch request without the app's credentials")
73
+ raise web.HTTPUnauthorized()
74
+
62
75
  data = await request.json()
63
76
  if "type" in data and data["type"] in TYPES:
64
77
  logger.debug("Dispatch to `%s` handler", data["type"])
@@ -13,6 +13,7 @@ from yarl import URL
13
13
  from aidbox_python_sdk.aidboxpy import AsyncAidboxClient
14
14
 
15
15
  from . import app_keys as ak
16
+ from .db import parse_psql_response
16
17
 
17
18
  _TEST_SERVER_URL = "http://127.0.0.1:8081"
18
19
 
@@ -100,7 +101,7 @@ async def safe_db(aidbox_client: AsyncAidboxClient, sdk):
100
101
  "/$psql",
101
102
  data={"query": "SELECT last_value from transaction_id_seq;"},
102
103
  )
103
- txid = results[0]["result"][0]["last_value"]
104
+ txid = parse_psql_response(results)[0][0]["last_value"]
104
105
  sdk._test_start_txid = int(txid)
105
106
 
106
107
  yield txid
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aidbox-python-sdk
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: Aidbox SDK for python
5
5
  Author-email: "beda.software" <aidbox-python-sdk@beda.software>
6
6
  License: MIT License
@@ -35,13 +35,13 @@ Classifier: Intended Audience :: Developers
35
35
  Classifier: Operating System :: OS Independent
36
36
  Classifier: Programming Language :: Python
37
37
  Classifier: Programming Language :: Python :: 3
38
- Classifier: Programming Language :: Python :: 3.9
39
- Classifier: Programming Language :: Python :: 3.10
40
38
  Classifier: Programming Language :: Python :: 3.11
41
39
  Classifier: Programming Language :: Python :: 3.12
40
+ Classifier: Programming Language :: Python :: 3.13
41
+ Classifier: Programming Language :: Python :: 3.14
42
42
  Classifier: Topic :: Internet :: WWW/HTTP
43
43
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
44
- Requires-Python: >=3.9
44
+ Requires-Python: >=3.11
45
45
  Description-Content-Type: text/markdown
46
46
  License-File: LICENSE.md
47
47
  Requires-Dist: aiohttp>=3.11.0
@@ -62,11 +62,11 @@ Dynamic: license-file
62
62
 
63
63
  [![build status](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml/badge.svg)](https://github.com/Aidbox/aidbox-python-sdk/actions/workflows/build.yaml)
64
64
  [![pypi](https://img.shields.io/pypi/v/aidbox-python-sdk.svg)](https://pypi.org/project/aidbox-python-sdk/)
65
- [![Supported Python version](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/release/python-390/)
65
+ [![Supported Python version](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/release/python-3110/)
66
66
 
67
67
  # aidbox-python-sdk
68
68
 
69
- 1. Create a python 3.9+ environment `pyenv `
69
+ 1. Create a python 3.11+ environment `pyenv `
70
70
  2. Set env variables and activate virtual environment `source activate_settings.sh`
71
71
  2. Install the required packages with `pipenv install --dev`
72
72
  3. Make sure the app's settings are configured correctly (see `activate_settings.sh` and `aidbox_python_sdk/settings.py`). You can also
@@ -76,14 +76,14 @@ classifiers = [
76
76
  "Operating System :: OS Independent",
77
77
  "Programming Language :: Python",
78
78
  "Programming Language :: Python :: 3",
79
- "Programming Language :: Python :: 3.9",
80
- "Programming Language :: Python :: 3.10",
81
79
  "Programming Language :: Python :: 3.11",
82
80
  "Programming Language :: Python :: 3.12",
81
+ "Programming Language :: Python :: 3.13",
82
+ "Programming Language :: Python :: 3.14",
83
83
  "Topic :: Internet :: WWW/HTTP",
84
84
  "Topic :: Software Development :: Libraries :: Python Modules",
85
85
  ]
86
- requires-python = ">=3.9"
86
+ requires-python = ">=3.11"
87
87
 
88
88
  [project.optional-dependencies]
89
89
  test = [
@@ -1,4 +1,5 @@
1
1
  import asyncio
2
+ import base64
2
3
  import logging
3
4
  from unittest import mock
4
5
 
@@ -7,6 +8,7 @@ from fhirpathpy import evaluate
7
8
  from fhirpy.base.exceptions import OperationOutcome
8
9
 
9
10
  import main
11
+ from aidbox_python_sdk import app_keys as ak
10
12
  from aidbox_python_sdk.db import DBProxy
11
13
 
12
14
 
@@ -70,11 +72,13 @@ async def test_appointment_sub(sdk, aidbox_client, safe_db):
70
72
  f.set_result("")
71
73
  appointment_sub.return_value = f
72
74
  was_appointment_sub_triggered = sdk.was_subscription_triggered("Appointment")
75
+ patient = aidbox_client.resource("Patient")
76
+ await patient.save()
73
77
  resource = aidbox_client.resource(
74
78
  "Appointment",
75
79
  **{
76
80
  "status": "proposed",
77
- "participant": [{"status": "accepted"}],
81
+ "participant": [{"status": "accepted", "actor": patient}],
78
82
  },
79
83
  )
80
84
  await resource.save()
@@ -84,7 +88,9 @@ async def test_appointment_sub(sdk, aidbox_client, safe_db):
84
88
  expected = {
85
89
  "resource": {
86
90
  "status": "proposed",
87
- "participant": [{"status": "accepted"}],
91
+ "participant": [
92
+ {"status": "accepted", "actor": {"id": patient.id, "resourceType": "Patient"}}
93
+ ],
88
94
  "resourceType": "Appointment",
89
95
  },
90
96
  "action": "create",
@@ -196,3 +202,71 @@ async def test_operation_outcome_test_op(aidbox_client):
196
202
  with pytest.raises(OperationOutcome) as exc:
197
203
  await aidbox_client.execute("/$operation-outcome-test")
198
204
  assert exc.value.resource.get("issue")[0].get("diagnostics") == "test reason"
205
+
206
+
207
+ DISPATCH_TEST_OP = {
208
+ "type": "operation",
209
+ "operation": {"id": "POST.main.dispatch_test_op.ddispatch-test"},
210
+ "request": {},
211
+ }
212
+ DISPATCH_TEST_EVENT = {"type": "subscription", "handler": "dispatch_test_sub", "event": {}}
213
+ ANOTHER_APP_AUTHORIZATION = f"Basic {base64.b64encode(b'app-test:another-secret').decode()}"
214
+
215
+
216
+ @pytest.fixture
217
+ def aidbox_authorization(app):
218
+ settings = app[ak.settings]
219
+ credentials = f"{settings.APP_ID}:{settings.APP_SECRET}".encode()
220
+ return f"Basic {base64.b64encode(credentials).decode()}"
221
+
222
+
223
+ @pytest.mark.asyncio
224
+ async def test_dispatch_refuses_an_operation_without_credentials(client):
225
+ resp = await client.post("/aidbox", json=DISPATCH_TEST_OP)
226
+
227
+ assert resp.status == 401
228
+
229
+
230
+ @pytest.mark.asyncio
231
+ async def test_dispatch_refuses_an_operation_with_another_app_secret(client):
232
+ resp = await client.post(
233
+ "/aidbox", json=DISPATCH_TEST_OP, headers={"Authorization": ANOTHER_APP_AUTHORIZATION}
234
+ )
235
+
236
+ assert resp.status == 401
237
+
238
+
239
+ @pytest.mark.asyncio
240
+ async def test_dispatch_invokes_an_operation_for_aidbox(client, aidbox_authorization):
241
+ resp = await client.post(
242
+ "/aidbox", json=DISPATCH_TEST_OP, headers={"Authorization": aidbox_authorization}
243
+ )
244
+
245
+ assert resp.status == 200
246
+ # The fallback branch answers 200 too, so only the operation's own body proves it ran.
247
+ assert await resp.json() == {"status": "ok"}
248
+
249
+
250
+ @pytest.mark.asyncio
251
+ async def test_dispatch_refuses_a_subscription_without_credentials(client):
252
+ resp = await client.post("/aidbox", json=DISPATCH_TEST_EVENT)
253
+
254
+ assert resp.status == 401
255
+
256
+
257
+ @pytest.mark.asyncio
258
+ async def test_dispatch_refuses_a_subscription_with_another_app_secret(client):
259
+ resp = await client.post(
260
+ "/aidbox", json=DISPATCH_TEST_EVENT, headers={"Authorization": ANOTHER_APP_AUTHORIZATION}
261
+ )
262
+
263
+ assert resp.status == 401
264
+
265
+
266
+ @pytest.mark.asyncio
267
+ async def test_dispatch_triggers_a_subscription_for_aidbox(client, aidbox_authorization):
268
+ resp = await client.post(
269
+ "/aidbox", json=DISPATCH_TEST_EVENT, headers={"Authorization": aidbox_authorization}
270
+ )
271
+
272
+ assert resp.status == 200