diff --git a/setra_client/client.py b/setra_client/client.py index b8c1062b4e7c163ec430c9c01ba1db8685ac8de1..db935d32feb13b892abcbc76f4ef271a6cf56bee 100644 --- a/setra_client/client.py +++ b/setra_client/client.py @@ -2,7 +2,7 @@ import logging import urllib.parse from datetime import date -from typing import Union, Optional +from typing import Union, Optional, List import requests @@ -187,16 +187,17 @@ class SetraClient(object): def post(self, url, **kwargs): return self.call('POST', url, **kwargs) - def object_or_data(self, cls, data) -> Union[object, dict]: - if not self.return_objects: - return data - return cls.from_dict(data) - - def objectlist_or_data(self, cls, data) -> Union[object, dict]: + def object_or_data( + self, cls, data: Union[dict, List[dict]] + ) -> Union[object, dict]: """Create list of objects or return data as is""" if not self.return_objects: return data - return [cls.from_dict(i) for i in data] + if isinstance(data, dict): + return cls.from_dict(data) + if isinstance(data, list): + return [cls.from_dict(i) for i in data] + raise TypeError(f"Expected list or dict, got {type(data)}") def get_batch(self, batch_id: Optional[int] = None, @@ -282,7 +283,7 @@ class SetraClient(object): if interface: queryparams = {"interface": interface} response = self.get(url, params=queryparams) - return self.objectlist_or_data(Parameter, response.json()) + return self.object_or_data(Parameter, response.json()) def get_client(config_dict):