Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit e4e69a6

Browse files
committed
Add filtering models by project ID and fix message when wrong api key was used
1 parent 667452e commit e4e69a6

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

paperspace/cli/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def models_group():
1616
"experimentId",
1717
help="Use to filter jobs by experiment ID",
1818
)
19+
@click.option(
20+
"--projectId",
21+
"projectId",
22+
help="Use to filter jobs by project ID",
23+
)
1924
@common.api_key_option
2025
def list_jobs(api_key, **filters):
2126
common.del_if_value_is_none(filters)

paperspace/commands/models.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ def execute(self, filters):
1414
response = self.api.get("/mlModels/getModelList/", json=json_, params=params)
1515

1616
try:
17-
models = self._get_models_list(response)
17+
data = response.json()
18+
if not response.ok:
19+
self.logger.log_error_response(data)
20+
return
21+
models = data["modelList"]
1822
except (ValueError, KeyError) as e:
1923
self.logger.log("Error while parsing response data: {}".format(e))
2024
else:
2125
self._log_models_list(models)
2226

2327
@staticmethod
2428
def _get_request_json(filters):
25-
experiment_id = filters.get("experimentId")
26-
if not experiment_id:
29+
if not filters:
2730
return None
2831

29-
json_ = {"filter": {"where": {"and": [{"experimentId": experiment_id}]}}}
32+
json_ = {"filter": {"where": {"and": [filters]}}}
3033
return json_
3134

3235
def _get_models_list(self, response):
@@ -37,11 +40,11 @@ def _get_models_list(self, response):
3740
self.logger.debug(data)
3841
return data
3942

40-
def _log_models_list(self, model):
41-
if not model:
43+
def _log_models_list(self, models):
44+
if not models:
4245
self.logger.log("No models found")
4346
else:
44-
table_str = self._make_models_list_table(model)
47+
table_str = self._make_models_list_table(models)
4548
if len(table_str.splitlines()) > get_terminal_lines():
4649
pydoc.pager(table_str)
4750
else:

tests/functional/test_models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class TestModelsList(object):
3030
+------+-----------------+------------+------------+---------------+
3131
"""
3232

33+
EXPECTED_RESPONSE_WHEN_WRONG_API_KEY_WAS_USED = {"status": 401, "message": "No such API token"}
34+
3335
@mock.patch("paperspace.cli.cli.client.requests.get")
3436
def test_should_send_get_request_and_print_list_of_experiments(self, get_patched):
3537
get_patched.return_value = MockResponse(example_responses.LIST_MODELS_RESPONSE_JSON, 200, "fake content")
@@ -88,3 +90,17 @@ def test_should_send_get_request_and_print_proper_message_when_no_models_were_fo
8890
params={"limit": -1})
8991

9092
assert result.output == "No models found\n"
93+
94+
@mock.patch("paperspace.cli.cli.client.requests.get")
95+
def test_should_print_proper_message_when_wrong_api_key_was_used(self, get_patched):
96+
get_patched.return_value = MockResponse(self.EXPECTED_RESPONSE_WHEN_WRONG_API_KEY_WAS_USED, 401)
97+
98+
runner = CliRunner()
99+
result = runner.invoke(cli.cli, self.COMMAND)
100+
101+
get_patched.assert_called_once_with(self.URL,
102+
headers=self.EXPECTED_HEADERS,
103+
json=None,
104+
params={"limit": -1})
105+
106+
assert result.output == "No such API token\n"

0 commit comments

Comments
 (0)