Skip to content

Commit

Permalink
Updated imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jmashon committed Jul 11, 2024
1 parent 64ff603 commit ad624da
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.coverage
.DS_Store
__pycache__
DO_NOT_COMMIT
test_commands.py
19 changes: 16 additions & 3 deletions api_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import requests

# api_functions.py -> api_connect.py

class ApiConnect():
def __init__(self):
self.token = None
Expand Down Expand Up @@ -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

Expand All @@ -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."""

Expand Down
2 changes: 2 additions & 0 deletions api_functions.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
2 changes: 2 additions & 0 deletions broker_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions broker_functions.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -10,6 +13,7 @@
class BrokerFunctions():
def __init__(self):
self.connect = BrokerConnect()
self.api = ApiFunctions()

self.token = None
self.client = None
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ad624da

Please sign in to comment.