Skip to content

Commit a03afdb

Browse files
Merge pull request #12 from Waltham-Data-Science/claude/align-python-api-response-fUucA
Claude/align python api response f uuc a
2 parents 78672b4 + 50ef23a commit a03afdb

6 files changed

Lines changed: 49 additions & 19 deletions

File tree

src/ndi/cloud/api/datasets.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121
_Client = Annotated[CloudClient | None, SkipValidation()]
2222

2323

24+
def _resolve_org_id(org_id: str | None, client: CloudClient) -> str:
25+
"""Return *org_id* if given, otherwise pull it from client config."""
26+
if org_id:
27+
return org_id
28+
resolved = getattr(client, "config", None)
29+
if resolved and getattr(resolved, "org_id", ""):
30+
return resolved.org_id
31+
raise ValueError(
32+
"org_id is required but was not provided and could not be "
33+
"resolved from the client config. Either pass org_id explicitly "
34+
"or set NDI_CLOUD_ORGANIZATION_ID in the environment."
35+
)
36+
37+
2438
@_auto_client
2539
@validate_call(config=VALIDATE_CONFIG)
2640
def getDataset(dataset_id: CloudId, *, client: _Client = None) -> dict[str, Any]:
@@ -29,16 +43,22 @@ def getDataset(dataset_id: CloudId, *, client: _Client = None) -> dict[str, Any]
2943

3044

3145
@_auto_client
32-
@validate_call(config=VALIDATE_CONFIG)
3346
def createDataset(
34-
org_id: NonEmptyStr,
35-
name: NonEmptyStr,
47+
org_id: str | None = None,
48+
name: str = "",
3649
description: str = "",
3750
*,
3851
client: _Client = None,
3952
**kwargs: Any,
4053
) -> dict[str, Any]:
41-
"""POST /organizations/{organizationId}/datasets"""
54+
"""POST /organizations/{organizationId}/datasets
55+
56+
If *org_id* is omitted it is resolved from the client's config
57+
(populated automatically during login), matching MATLAB behaviour.
58+
"""
59+
org_id = _resolve_org_id(org_id, client)
60+
if not name:
61+
raise ValueError("name is required")
4262
body: dict[str, Any] = {"name": name}
4363
if description:
4464
body["description"] = description
@@ -92,15 +112,19 @@ def deleteDataset(
92112

93113

94114
@_auto_client
95-
@validate_call(config=VALIDATE_CONFIG)
96115
def listDatasets(
97-
org_id: NonEmptyStr,
98-
page: PageNumber = 1,
99-
page_size: PageSize = 1000,
116+
org_id: str | None = None,
117+
page: int = 1,
118+
page_size: int = 1000,
100119
*,
101120
client: _Client = None,
102121
) -> dict[str, Any]:
103-
"""GET /organizations/{organizationId}/datasets?page=&pageSize="""
122+
"""GET /organizations/{organizationId}/datasets?page=&pageSize=
123+
124+
If *org_id* is omitted it is resolved from the client's config
125+
(populated automatically during login), matching MATLAB behaviour.
126+
"""
127+
org_id = _resolve_org_id(org_id, client)
104128
return client.get(
105129
"/organizations/{organizationId}/datasets",
106130
params={"page": page, "pageSize": page_size},
@@ -112,9 +136,13 @@ def listDatasets(
112136

113137

114138
@_auto_client
115-
@validate_call(config=VALIDATE_CONFIG)
116-
def listAllDatasets(org_id: NonEmptyStr, *, client: _Client = None) -> APIResponse:
117-
"""Auto-paginate through all datasets for an organisation."""
139+
def listAllDatasets(org_id: str | None = None, *, client: _Client = None) -> APIResponse:
140+
"""Auto-paginate through all datasets for an organisation.
141+
142+
If *org_id* is omitted it is resolved from the client's config
143+
(populated automatically during login), matching MATLAB behaviour.
144+
"""
145+
org_id = _resolve_org_id(org_id, client)
118146
all_datasets: list[dict[str, Any]] = []
119147
page = 1
120148
while page <= _MAX_PAGES:

src/ndi/cloud/download.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ def downloadGenericFiles(
567567
all_docs = []
568568
for doc_id in ndi_document_ids:
569569
q = Query("base.id", "exact_string", doc_id, "")
570-
results = ndi_dataset.database_search(q) if hasattr(ndi_dataset, "database_search") else []
570+
results = (
571+
ndi_dataset.database_search(q) if hasattr(ndi_dataset, "database_search") else []
572+
)
571573
all_docs.extend(results)
572574

573575
if not all_docs:

src/ndi/cloud/internal.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ def duplicateDocuments(
288288
for i in range(0, len(doc_ids_to_delete), maximum_delete_batch_size):
289289
batch = doc_ids_to_delete[i : i + maximum_delete_batch_size]
290290
batch_num = i // maximum_delete_batch_size + 1
291-
total_batches = (len(doc_ids_to_delete) + maximum_delete_batch_size - 1) // maximum_delete_batch_size
291+
total_batches = (
292+
len(doc_ids_to_delete) + maximum_delete_batch_size - 1
293+
) // maximum_delete_batch_size
292294
if verbose:
293295
print(f"Deleting batch {batch_num} of {total_batches}...")
294296
try:

src/ndi/cloud/orchestration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ def newDataset(
377377
# Re-export from upload module (MATLAB: ndi.cloud.upload.scanForUpload)
378378
from .upload import scanForUpload # noqa: F401
379379

380-
381380
# ---------------------------------------------------------------------------
382381
# Private sync helpers
383382
# ---------------------------------------------------------------------------

src/ndi/cloud/sync/operations.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,7 @@ def twoWaySync(
477477
failed.append(doc_id)
478478

479479
# 4. Download remote-only docs
480-
docs, dl_failed = downloadNdiDocuments(
481-
cloud_dataset_id, remote_ids, to_download, client=client
482-
)
480+
docs, dl_failed = downloadNdiDocuments(cloud_dataset_id, remote_ids, to_download, client=client)
483481
saved = _save_downloaded_docs(ds_path, docs)
484482
report["downloaded"] = saved
485483
failed.extend(dl_failed)

tests/test_cloud_live.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def _cleanup_stale_pytest_datasets(client, cloud_config):
207207
result = listDatasets(cloud_config.org_id, client=client)
208208
datasets = result.get("datasets", [])
209209
stale = [
210-
ds for ds in datasets
210+
ds
211+
for ds in datasets
211212
if ds.get("name", "").startswith("NDI_PYTEST") and ds.get("_id", ds.get("id", ""))
212213
]
213214
if stale:

0 commit comments

Comments
 (0)