Refactor API interactions in CLI#44
Conversation
Signed-off-by: rafsanneloy <rafsanneloy@gmail.com>
|
This refactor appears to turn some validation failures into successful CLI exits. That is, an Error occurs but the CLI returns status 0 instead of non-zero. Could we keep validation failures returning a non-zero exit status, either by having the data-layer functions return an error code/sentinel or by having handlers treat None from validation as failure? |
|
In your description could you show what specific issues in plan.md you are addressing? |
Signed-off-by: rafsanneloy <rafsanneloy@gmail.com>
|
Previously in capture_tests/expected_captures.txt has cbrain tag update 99 --name Renamed --user-id 2 --group-id 3But it should be
@dlq I'm trying to find and refactor the def show_file(args):
......
userfile_endpoint = f"{cbrain_url}/userfiles/{file_id}"
headers = auth_headers(api_token)
request = urllib.request.Request(userfile_endpoint, data=None, headers=headers, method="GET")
with urllib.request.urlopen(request) as response:
data = response.read().decode("utf-8")
file_data = json.loads(data)
return file_dataBut now i updated the function, with by calling the api_get like this - def show_file(args):
....
return api_get(f"{cbrain_url}/userfiles/{file_id}", api_token)the get function is - def api_get(url, token, params=None):
if params:
url = f"{url}?{urllib.parse.urlencode(params)}"
req = urllib.request.Request(url, headers=auth_headers(token), method="GET")
with urllib.request.urlopen(req) as r:
return json.loads(r.read().decode())Did you see that, by using a common get function we can remove the extra lines from show file and we can call the api where we need. As this cli project will be improving day by day, I believe we really want that transformation before working on the plan.md. What do you think @dlq ? |
|
Thanks, this tag update fix makes sense to me. Nice catch. It looks like the old command was actually updating the tag successfully, then tripping afterward because the successful response was empty/non-JSON. Updating the capture for that behavior seems reasonable, and it would be good to call that out explicitly in the PR description. I do think there is still one important issue to fix before this can merge, though. On the latest head, invalid commands like: cbrain file list --per-page 4
cbrain dataprovider list --per-page 4
cbrain task list bourreau-idstill return status For the Could you either fix the validation contract in this PR, or narrow the PR to the tag update / empty-response fix plus a smaller API-helper migration with tests? That would make this much easier to review and get merged. |
Signed-off-by: rafsanneloy <rafsanneloy@gmail.com>
|
Hi @dlq updated according to your suggestions. Also I tried to implement task list bourreau-id filtering and Add typed CLI exceptions from plan.md . Waiting for your review. |
|
Thanks Rafsan, this is looking much better! The new exception-based validation flow is a good improvement, and it looks like it addresses the main issue I was worried about. I rechecked the cases we discussed earlier: cbrain file list --per-page 4
cbrain dataprovider list --per-page 4
cbrain task list bourreau-idand they now return a non-zero status with clear validation errors. I also checked the There are just a couple of follow-ups before this is ready:
Once the capture expectations are updated and CI is green, this should be much easier to finish reviewing and accepting. Nice progress here! |
|
Hi @dlq, Thanks for your review. I have updated the PR. Please take another look when you have a chance. |
Refactor API Request Handling and Improve CLI Error Management
This PR centralizes HTTP request logic through shared API utilities, improves CLI error handling with typed exceptions, and enhances maintainability by refactoring data modules to use the new helpers. It also fixes
task list bourreau-id <id>filtering and updates related test expectations.Addresses from
plan.mdtask list bourreau-id <id>filteringAdd typed CLI exceptionsCentralize request handling