Skip to content

Commit 4159329

Browse files
committed
Adding Copilot review: Improve load_all handling and CLI logout parser
Enforce and clarify pagination/parameter behavior and fix CLI parser inheritance. - brainstem_api_client: always pass options to _build_url; disallow load_all=True when an id is provided; raise a clear ValueError if auto-pagination expects a list but the API response contains no list-valued key; change filter merging to include falsy values (check for None instead of truthiness). - cli: make the logout subparser inherit common arguments (parents=[common]). - tests: set mocked response.status_code to 200 for pagination tests. - README: remove the Contributing section. These changes make load_all usage safer and errors clearer, ensure logout CLI gets shared options, and update tests to better mock HTTP responses.
1 parent 477bcef commit 4159329

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ client.save('session', id='<session-uuid>', data={'description': 'updated'})
103103
client.delete('session', id='<session-uuid>')
104104
```
105105

106-
## Contributing
107-
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
108-
109106
## Command-line Interface
110107

111108
After installation a `brainstem` command is available in your shell.

brainstem_api_tools/brainstem_api_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def load(self,
270270
model = _resolve_model(model)
271271
portal = _resolve_portal(portal)
272272
app = _MODEL_TO_APP[model]
273-
url = self._build_url(portal, app, model, id, options if id else None)
273+
url = self._build_url(portal, app, model, id, options)
274274

275275
params = {}
276276
if not id:
@@ -288,6 +288,9 @@ def load(self,
288288
if not load_all:
289289
return self._session.get(url, params=params, timeout=self.DEFAULT_TIMEOUT)
290290

291+
if id is not None:
292+
raise ValueError("load_all=True cannot be used together with id.")
293+
291294
# --- auto-paginate and merge all pages ---
292295
page_size = limit or 100
293296
params["limit"] = page_size
@@ -310,6 +313,11 @@ def load(self,
310313
records_key = next(
311314
(k for k, v in data.items() if isinstance(v, list)), None
312315
)
316+
if records_key is None:
317+
raise ValueError(
318+
"load_all=True requires a paginated list response, but the API "
319+
"returned no list-valued key. Use load_all=False for single-object endpoints."
320+
)
313321
combined = {k: v for k, v in data.items() if k != records_key}
314322
combined[records_key] = []
315323

@@ -396,7 +404,7 @@ def _convenience_load(self, model, default_include, filter_map,
396404
merged_filters = dict(filters or {})
397405
for kwarg, api_field in filter_map.items():
398406
value = field_kwargs.get(kwarg)
399-
if value:
407+
if value is not None:
400408
merged_filters[api_field] = value
401409
return self.load(
402410
model,

brainstem_api_tools/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def _build_parser() -> argparse.ArgumentParser:
6060
# ---- logout -----------------------------------------------------
6161
sub.add_parser(
6262
"logout",
63+
parents=[common],
6364
help="Remove the cached API token.",
6465
)
6566

tests/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def _mock_pages(self, pages):
303303
responses = []
304304
for page in pages:
305305
r = MagicMock()
306+
r.status_code = 200
306307
r.raise_for_status = MagicMock()
307308
r.json.return_value = page
308309
responses.append(r)

0 commit comments

Comments
 (0)