Skip to content
Snippets Groups Projects
Commit 230b6658 authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Simplify data to object conversion

Instead of two methods we now have one that does the appropriate
action based on the input type.
parent 2279758a
No related branches found
No related tags found
1 merge request!12Add support for api/parameters endpoint
Pipeline #68354 passed
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment