Skip to content

Commit 24daeb8

Browse files
Juliana MashonJuliana Mashon
Juliana Mashon
authored and
Juliana Mashon
committed
Bookmark--'request' method as argument
1 parent 25f3ced commit 24daeb8

File tree

3 files changed

+36
-53
lines changed

3 files changed

+36
-53
lines changed

api_connect.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,27 @@ def check_token(self):
7171
print("ERROR: You have no token, please call `get_token` using your login credentials and the server you wish to connect to.")
7272
sys.exit(1)
7373

74-
def request(self, method, endpoint, id, payload):
75-
"""Send requests from user-accessible functions via API."""
74+
def request(self, method, endpoint, database_id, payload):
75+
"""Send requests to the API using various methods."""
7676

7777
self.check_token()
7878

79-
if id is None:
79+
# use 'GET' method to view endpoint data
80+
# use 'POST' method to overwrite/create new endpoint data
81+
# use 'PATCH' method to edit endpoint data (used for new logs)
82+
# use 'DELETE' method to delete endpoint data (hidden)
83+
84+
if database_id is None:
8085
url = f'https:{self.token["token"]["unencoded"]["iss"]}/api/{endpoint}'
8186
else:
82-
url = f'https:{self.token["token"]["unencoded"]["iss"]}/api/{endpoint}/{id}'
87+
url = f'https:{self.token["token"]["unencoded"]["iss"]}/api/{endpoint}/{database_id}'
8388

8489
headers = {'authorization': self.token['token']['encoded'], 'content-type': 'application/json'}
8590
response = requests.request(method, url, headers=headers, json=payload)
8691

8792
if self.request_handling(response) == 200:
88-
user_request = response.json()
8993
self.error = None
90-
return user_request
94+
request_data = response.json()
95+
return request_data
9196
else:
9297
return self.error
93-
94-
## REQUEST METHODS
95-
96-
def get(self, endpoint, id):
97-
"""METHOD: 'get' allows user to view endpoint data."""
98-
return self.request('GET', endpoint, id, payload=None)
99-
100-
def post(self, endpoint, id, payload):
101-
"""METHOD: 'post' allows user to overwrite/create new endpoint data."""
102-
return self.request('POST', endpoint, id, payload)
103-
104-
def patch(self, endpoint, id, payload):
105-
"""METHOD: 'patch' allows user to edit endpoint data (used for new logs)."""
106-
return self.request('PATCH', endpoint, id, payload)
107-
108-
def delete(self, endpoint, id):
109-
"""METHOD: 'delete' allows user to delete endpoint data (hidden)."""
110-
return self.request('DELETE', endpoint, id, payload=None)

broker_connect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def publish(self, message):
5454
## FUNCTIONS -- RECEIVING MESSAGES
5555

5656
def on_connect(self, _client, _userdata, _flags, _rc, channel):
57-
"""Subscribe to specified broker response channel."""
57+
"""Subscribe to specified message broker channel."""
5858
self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/{channel}")
5959

6060
def on_message(self, _client, _userdata, msg):

broker_functions.py

+24-28
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self):
3535

3636
self.client = None
3737

38-
def read_status(self):
38+
def read_status(self): #########################################
3939
# Get device status tree
4040
message = {
4141
"kind": "rpc_request",
@@ -59,7 +59,7 @@ def read_status(self):
5959
# Return status as json object: status[""]
6060
return status_tree
6161

62-
def read_sensor(self, id):
62+
def read_sensor(self, id): #########################################
6363
# Get sensor data
6464
peripheral_str = self.api.get_info('peripherals', id)
6565
mode = peripheral_str['mode']
@@ -159,17 +159,7 @@ def e_stop(self):
159159

160160
def unlock(self):
161161
# Tell bot to unlock
162-
# No inherent return value
163-
164-
# unlock_message = {
165-
# **RPC_REQUEST,
166-
# "body": {
167-
# "kind": "emergency_unlock",
168-
# "args": {}
169-
# }
170-
# }
171-
172-
new_unlock = {
162+
message = {
173163
"kind": "rpc_request",
174164
"args": {
175165
"label": "",
@@ -182,12 +172,12 @@ def unlock(self):
182172
}
183173
]
184174
}
185-
self.broker_connect.publish(new_unlock)
175+
self.broker_connect.publish(message)
186176

187-
# self.broker_connect.publish(unlock_message)
177+
# No inherent return value
188178
return print("Triggered device unlock.")
189179

190-
def reboot(self):
180+
def reboot(self): #########################################
191181
# Tell bot to reboot
192182
# No inherent return value
193183
reboot_message = {
@@ -203,7 +193,7 @@ def reboot(self):
203193
self.broker_connect.publish(reboot_message)
204194
return print("Triggered device reboot.")
205195

206-
def shutdown(self):
196+
def shutdown(self): #########################################
207197
# Tell bot to shutdown
208198
# No inherent return value
209199
shutdown_message = {
@@ -278,22 +268,28 @@ def set_home(self, axis='all'):
278268

279269
def find_home(self, axis='all', speed=100):
280270
# Move to 0,0,0
281-
# Return new xyz position as values
282271
if speed > 100 or speed < 1:
283272
return print("ERROR: Speed constrained to 1-100.")
284273
else:
285-
find_home_message = {
286-
**RPC_REQUEST,
287-
"body": {
288-
"kind": "find_home",
289-
"args": {
290-
"axis": axis,
291-
"speed": speed
274+
message = {
275+
"kind": "rpc_request",
276+
"args": {
277+
"label": "",
278+
"priority": 600
279+
},
280+
"body": [
281+
{
282+
"kind": "find_home",
283+
"args": {
284+
"axis": axis,
285+
"speed": speed
286+
}
292287
}
293-
}
288+
]
294289
}
290+
self.broker_connect.publish(message)
295291

296-
self.broker_connect.publish(find_home_message)
292+
# Return new xyz position as values
297293

298294
def axis_length(self, axis='all'):
299295
# Get axis length
@@ -629,7 +625,7 @@ def mark_coord(self, x, y, z, property, mark_as): # TODO: Fix "label"
629625

630626
# TODO: water() --> all or single coords
631627
# Dispense water at all or single xyz coords
632-
# No inherent return value
628+
# No inherent return value
633629
# TODO: dispense() --> single coords?
634630
# Dispense from source at all or single xyz coords
635631
# No inherent return value

0 commit comments

Comments
 (0)