From ad624da984535eeaf3b3896bd13e2b4753dd2359 Mon Sep 17 00:00:00 2001 From: Juliana Mashon Date: Thu, 11 Jul 2024 13:19:43 -0700 Subject: [PATCH] Updated imports --- .gitignore | 3 ++- api_connect.py | 19 ++++++++++++++++--- api_functions.py | 2 ++ broker_connect.py | 2 ++ broker_functions.py | 8 ++++++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9f5743f..d47b677 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .coverage +.DS_Store __pycache__ -DO_NOT_COMMIT \ No newline at end of file +test_commands.py diff --git a/api_connect.py b/api_connect.py index 7eb8b79..7912670 100644 --- a/api_connect.py +++ b/api_connect.py @@ -2,6 +2,8 @@ import json import requests +# api_functions.py -> api_connect.py + class ApiConnect(): def __init__(self): self.token = None @@ -74,10 +76,12 @@ def get_token(self, email, password, server): response = requests.post(f'{server}/api/tokens', headers=headers, json=user) if self.token_handling(response) == 200: - user_data = response.json() - user_token = user_data['token'] + token_obj = response.json() + token_str = json.dumps(token_obj) + # token_dict = json.loads(token_str) + self.token = token_str self.error = None - return user_token + return token_str else: return self.error @@ -88,6 +92,15 @@ def check_token(self): print("ERROR: You have no token, please call `get_token` using your login credentials and the server you wish to connect to.") sys.exit(1) + # save response as JSON object: json_obj = response.json() + # json.dumps(json_obj) + + # save response as a string: json_str = response.text + + # access JSON object fields: json_obj = response.json() + # data = json.dumps(json_obj) + # name = data["name"] + def request(self, method, endpoint, id, payload): """Send requests from user-accessible functions via API.""" diff --git a/api_functions.py b/api_functions.py index 8751135..465bdd1 100644 --- a/api_functions.py +++ b/api_functions.py @@ -1,5 +1,7 @@ from api_connect import ApiConnect +# main.py -> api_functions.py -> api_connect.py + class ApiFunctions(): def __init__(self): self.connect = ApiConnect() diff --git a/broker_connect.py b/broker_connect.py index b558225..2fab509 100644 --- a/broker_connect.py +++ b/broker_connect.py @@ -3,6 +3,8 @@ from datetime import datetime import paho.mqtt.client as mqtt +# broker_functions.py -> broker_connect.py + class BrokerConnect(): def __init__(self): self.token = None diff --git a/broker_functions.py b/broker_functions.py index a995afd..39853fd 100644 --- a/broker_functions.py +++ b/broker_functions.py @@ -1,4 +1,7 @@ from broker_connect import BrokerConnect +from api_functions import ApiFunctions + +# main.py -> broker_functions.py -> broker_connect.py RPC_REQUEST = { "kind": "rpc_request", @@ -10,6 +13,7 @@ class BrokerFunctions(): def __init__(self): self.connect = BrokerConnect() + self.api = ApiFunctions() self.token = None self.client = None @@ -172,7 +176,7 @@ def control_servo(self, pin, angle): def control_peripheral(self, id, value, mode=None): if mode is None: - peripheral_str = self.get_info('peripherals', id) + peripheral_str = self.api.get_info('peripherals', id) mode = peripheral_str['mode'] control_peripheral_message = { @@ -217,7 +221,7 @@ def toggle_peripheral(self, id): # return ... def on(self, id): - peripheral_str = self.get_info('peripherals', id) + peripheral_str = self.api.get_info('peripherals', id) mode = peripheral_str['mode'] if mode == 1: