Python code API beyond CLI
I hope the correct API would be used within the call ethz_iam_webservice.main.login().get_groups()
to reproduce the same behavior as in the CLI command iam groups -n ID-SIS*
or at least to get a more user-friendly error message could be retrieved (see message below when receiving response 500 from https://iamws.ethz.ch/groups):
The following example code was used to demonstrate that the alternative API was not used.
import json
from dataclasses import asdict
from ethz_iam_webservice.main import login
name = f"ID-SIS*"
iam = login()
groups = iam.get_groups(group_name=name)
print(json.dumps([asdict(group) for group in groups], indent=4, sort_keys=True))
The error on my system reads as follows:
JSONDecodeError Traceback (most recent call last)
File ~/.virtualenvs/lm2-test/lib/python3.10/site-packages/requests/models.py:971, in Response.json(self, **kwargs)
970 try:
--> 971 return complexjson.loads(self.text, **kwargs)
972 except JSONDecodeError as e:
973 # Catch JSON-related errors and raise as requests.JSONDecodeError
974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
File /usr/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
343 if (cls is None and object_hook is None and
344 parse_int is None and parse_float is None and
345 parse_constant is None and object_pairs_hook is None and not kw):
--> 346 return _default_decoder.decode(s)
347 if cls is None:
File /usr/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
File /usr/lib/python3.10/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
354 except StopIteration as err:
--> 355 raise JSONDecodeError("Expecting value", s, err.value) from None
356 return obj, end
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
JSONDecodeError Traceback (most recent call last)
/mnt/c/Users/strpeter/test_file.py in line 9
7 name = f"ID-SIS*"
8 iam = login()
----> 9 groups = iam.get_groups(group_name=name)
10 print(json.dumps([asdict(group) for group in groups], indent=4, sort_keys=True))
File ~/.virtualenvs/lm2-test/lib/python3.10/site-packages/ethz_iam_webservice/ethz_iam.py:92, in ETH_IAM.get_groups(self, **kwargs)
90 raise ValueError("please provide a name or agroup parameter (or both)")
91 iam = IAMApi(self._admin_username, self._admin_password)
---> 92 data = iam.get_request(endpoint)
93 groups = []
94 for data_entry in data:
File ~/.virtualenvs/lm2-test/lib/python3.10/site-packages/ethz_iam_webservice/conn.py:66, in IAMApi.get_request(self, endpoint)
64 raise ValueError("No such user/person/group.")
65 else:
---> 66 message = resp.json()
67 raise ValueError(message)
File ~/.virtualenvs/lm2-test/lib/python3.10/site-packages/requests/models.py:975, in Response.json(self, **kwargs)
971 return complexjson.loads(self.text, **kwargs)
972 except JSONDecodeError as e:
973 # Catch JSON-related errors and raise as requests.JSONDecodeError
974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
JSONDecodeError: Expecting value: line 1 column 1 (char 0)