From 230b665882454d6d827258e1a25a05edd54dbf4a Mon Sep 17 00:00:00 2001 From: Andreas Ellewsen <andreas.ellewsen@usit.uio.no> Date: Mon, 22 Mar 2021 11:04:26 +0100 Subject: [PATCH] Simplify data to object conversion Instead of two methods we now have one that does the appropriate action based on the input type. --- setra_client/client.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/setra_client/client.py b/setra_client/client.py index b8c1062..db935d3 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): -- GitLab