From 29d49dfeb26c0fb797cb4d8326be48acfd539f19 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Fri, 31 May 2024 19:12:42 +0400 Subject: [PATCH] Refactor: separate models.Device method for device JSON obj --- models.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/models.py b/models.py index d546476..9a9c919 100644 --- a/models.py +++ b/models.py @@ -93,13 +93,23 @@ class Device: '''Generate a JSON format request body''' obj = { - 'devices': [{ - 'mac': self.mac, - 'sensors': [], - }] + 'devices': [ + self._json_device_obj(), + ] + } + + return json.dumps(obj) + + def _json_device_obj(self) -> dict[str, Any]: + '''Create a device JSON object for json_req_data(). + Moved to a separate function to allow performing tests + with multiple devices in a request at once.''' + + dev = { + 'mac': self.mac, + 'sensors': [], } - dev = obj['devices'][0] if self.name is not None: dev['name'] = self.name if self.owner is not None: @@ -121,4 +131,4 @@ class Device: sobj['name'] = s.name sl.append(sobj) - return json.dumps(obj) + return dev