Skip to content

Commit 017cda2

Browse files
Juliana MashonJuliana Mashon
authored andcommitted
Updated functions for accessing API endpoints
1 parent 77597a6 commit 017cda2

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ bot.set_verbosity(2)
8484
| `1` The name of the function will be output. | `e_stop called` |
8585
| `2` The name of the function will be output with additional information about the return value. | `Triggered device emergency stop at: 2024-08-21 11:16:18.547813` |
8686

87+
### Test 1: Add a new plant to your garden
88+
89+
This test will help familiarize you with sending commands via the [API](https://developer.farm.bot/docs/web-app/rest-api).
90+
91+
### Test 2: Turn your LED strip on and off
92+
93+
This test will help familiarize you with sending commands via the [Message Broker](https://developer.farm.bot/docs/message-broker).
94+
```
95+
on(7) # Turn ON pin 7 (LED strip)
96+
wait(2000) # Wait 2000 milliseconds
97+
off(7) # Turn OFF pin 7 (LED strip)
98+
```
99+
87100
## :compass: Functions
88101

89102
```

functions/authentication.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def request(self, method, endpoint, database_id, payload=None):
9292
url = f'https:{self.state.token["token"]["unencoded"]["iss"]}/api/{endpoint}/{database_id}'
9393

9494
headers = {'authorization': self.state.token['token']['encoded'], 'content-type': 'application/json'}
95-
response = requests.request(method, url, headers=headers, json=payload)
95+
96+
session = requests.Session()
97+
response = session.request(method, url, headers=headers, json=payload)
9698

9799
if self.request_handling(response) == 200:
98100
self.state.error = None

functions/information.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
# └── functions/information
66
# ├── [API] get_info()
7-
# ├── [API] set_info()
7+
# ├── [API] edit_info()
8+
# ├── [API] add_info()
89
# ├── [API] safe_z()
910
# ├── [API] garden_size()
1011
# ├── [API] group()
@@ -25,23 +26,30 @@ def __init__(self, state):
2526
def get_info(self, endpoint, id=None):
2627
"""Get information about a specific endpoint."""
2728

28-
endpoint_data = self.auth.request('GET', endpoint, id)
29+
endpoint_data = self.auth.request("GET", endpoint, id)
2930

3031
self.broker.state.print_status("get_info()", endpoint_json=endpoint_data)
3132

3233
return endpoint_data
3334

34-
def set_info(self, endpoint, field, value, id=None):
35+
def edit_info(self, endpoint, new_data, id=None):
3536
"""Change information contained within an endpoint."""
3637

37-
new_value = {
38-
field: value
39-
}
38+
self.auth.request("PATCH", endpoint, database_id=id, payload=new_data)
39+
endpoint_data = self.get_info(endpoint, id)
40+
41+
self.broker.state.print_status("edit_info()", endpoint_json=endpoint_data)
42+
43+
return endpoint_data
44+
45+
def add_info(self, endpoint, new_data, id=None):
46+
"""Create new informated contained within an endpoint."""
47+
48+
self.auth.request("POST", endpoint, database_id=id, payload=new_data)
4049

41-
self.auth.request('PATCH', endpoint, id, new_value)
4250
endpoint_data = self.get_info(endpoint, id)
4351

44-
self.broker.state.print_status("set_info()", endpoint_json=endpoint_data)
52+
self.broker.state.print_status("add_info()", endpoint_json=endpoint_data)
4553

4654
return endpoint_data
4755

main.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def print_status(self, function, endpoint_json=None, description=None):
1515
"""Handle changes to output based on user-defined verbosity."""
1616

1717
if self.verbosity >= 1:
18-
return print(f"`{function}` called")
18+
print(f"`{function}` called")
1919
if self.verbosity >= 2 and endpoint_json:
20-
return print(json.dumps(endpoint_json, indent=4))
20+
print(json.dumps(endpoint_json, indent=4))
2121
if self.verbosity >= 2 and description:
22-
return print(description)
22+
print(description)
2323

2424
class Farmbot():
2525
def __init__(self):
@@ -106,9 +106,13 @@ def get_info(self, endpoint, id=None):
106106
"""Get information about a specific endpoint."""
107107
return self.info.get_info(endpoint, id)
108108

109-
def set_info(self, endpoint, field, value, id=None):
109+
def edit_info(self, endpoint, new_data, id=None):
110110
"""Change information contained within an endpoint."""
111-
return self.info.set_info(endpoint, field, value, id)
111+
return self.info.edit_info(endpoint, new_data, id)
112+
113+
def add_info(self, endpoint, new_data, id=None):
114+
"""Create new informated contained within an endpoint."""
115+
return self.info.add_info(endpoint, new_data, id)
112116

113117
def safe_z(self):
114118
"""Returns the highest safe point along the z-axis."""

0 commit comments

Comments
 (0)