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. |
Introduce utility functions for
GETandPOSTrequests, streamline error handling, and enhance session management. Update various data handling modules to utilize new API functions for improved readability and maintainability.