23
23
`adafruit_io`
24
24
================================================================================
25
25
26
- A CircuitPython/Python library for communicating with Adafruit IO
26
+ A CircuitPython/Python library for communicating with Adafruit IO over WiFi
27
27
28
28
* Author(s): Brent Rubell for Adafruit Industries
29
29
@@ -66,13 +66,18 @@ def __init__(self, adafruit_io_username, adafruit_io_key, wifi_manager):
66
66
67
67
@staticmethod
68
68
def _create_data (data , metadata ):
69
+ """Creates JSON data payload
70
+ """
69
71
if metadata is not None :
70
72
return {'value' :data , 'lat' :metadata ['lat' ], 'lon' :metadata ['lon' ],
71
73
'ele' :metadata ['ele' ], 'created_at' :metadata ['created_at' ]}
72
74
return {'value' :data }
73
75
74
76
@staticmethod
75
77
def _handle_error (response ):
78
+ """Checks HTTP status codes
79
+ and raises errors.
80
+ """
76
81
if response .status_code == 429 :
77
82
raise AdafruitIO_ThrottleError
78
83
elif response .status_code == 400 :
@@ -81,13 +86,16 @@ def _handle_error(response):
81
86
raise AdafruitIO_RequestError (response )
82
87
83
88
def _compose_path (self , path ):
89
+ """Composes a valid API request path.
90
+ :param str path: Adafruit IO API URL path.
91
+ """
84
92
return "{0}/{1}/{2}/{3}" .format ('https://io.adafruit.com/api' , 'v2' , self .username , path )
85
93
86
94
# HTTP Requests
87
95
def _post (self , path , payload ):
88
96
"""
89
- Send data to Adafruit IO
90
- :param str path: Formatted Adafruit IO URL
97
+ POST data to Adafruit IO
98
+ :param str path: Formatted Adafruit IO URL from _compose_path
91
99
:param json payload: JSON data to send to Adafruit IO
92
100
"""
93
101
response = self .wifi .post (
@@ -99,8 +107,8 @@ def _post(self, path, payload):
99
107
100
108
def _get (self , path ):
101
109
"""
102
- Get data from Adafruit IO
103
- :param str path: Formatted Adafruit IO URL
110
+ GET data from Adafruit IO
111
+ :param str path: Formatted Adafruit IO URL from _compose_path
104
112
"""
105
113
response = self .wifi .get (
106
114
path ,
@@ -110,8 +118,8 @@ def _get(self, path):
110
118
111
119
def _delete (self , path ):
112
120
"""
113
- Delete data from Adafruit IO.
114
- :param str path: Formatted Adafruit IO URL
121
+ DELETE data from Adafruit IO.
122
+ :param str path: Formatted Adafruit IO URL from _compose_path
115
123
"""
116
124
response = self .wifi .delete (
117
125
path ,
@@ -122,10 +130,10 @@ def _delete(self, path):
122
130
# Data
123
131
def send_data (self , feed_key , data , metadata = None ):
124
132
"""
125
- Sends value data to an Adafruit IO feed.
126
- :param str feed_key: Specified Adafruit IO feed
127
- :param str data: Data to send to an Adafruit IO feed
128
- :param dict metadata: Metadata associated with the data being sent
133
+ Sends value data to a specified Adafruit IO feed.
134
+ :param str feed_key: Adafruit IO feed key
135
+ :param str data: Data to send to the Adafruit IO feed
136
+ :param dict metadata: Optional metadata associated with the data
129
137
"""
130
138
path = self ._compose_path ("feeds/{0}/data" .format (feed_key ))
131
139
payload = self ._create_data (data , metadata )
@@ -134,16 +142,16 @@ def send_data(self, feed_key, data, metadata=None):
134
142
def receive_data (self , feed_key ):
135
143
"""
136
144
Return the most recent value for the specified feed.
137
- :param string feed_key: Name/Key/ID of Adafruit IO feed.
145
+ :param string feed_key: Adafruit IO feed key
138
146
"""
139
147
path = self ._compose_path ("feeds/{0}/data/last" .format (feed_key ))
140
148
return self ._get (path )
141
149
142
150
def delete_data (self , feed_key , data_id ):
143
151
"""
144
- Delete an existing Data point from a feed.
145
- :param string feed: Feed Key
146
- :param string data_id: Data point to delete
152
+ Deletes an existing Data point from a feed.
153
+ :param string feed: Adafruit IO feed key
154
+ :param string data_id: Data point to delete from the feed
147
155
"""
148
156
path = self ._compose_path ("feeds/{0}/data/{1}" .format (feed_key , data_id ))
149
157
return self ._delete (path )
@@ -188,9 +196,9 @@ def get_group(self, group_key):
188
196
# Feeds
189
197
def get_feed (self , feed_key , detailed = False ):
190
198
"""
191
- Returns feed based on the feed key
192
- :param str feed_key: Feed Key
193
- :param bool detailed: Returns a more detailed feed record
199
+ Returns an Adafruit IO feed based on the feed key
200
+ :param str feed_key: Adafruit IO Feed Key
201
+ :param bool detailed: Returns a more verbose feed record
194
202
"""
195
203
if detailed :
196
204
path = self ._compose_path ("feeds/{0}/details" .format (feed_key ))
@@ -200,10 +208,10 @@ def get_feed(self, feed_key, detailed=False):
200
208
201
209
def create_new_feed (self , feed_key , feed_desc = None , feed_license = None ):
202
210
"""
203
- Creates a new feed.
204
- :param str feed_key: Feed key
211
+ Creates a new Adafruit IO feed.
212
+ :param str feed_key: Adafruit IO Feed Key
205
213
:param str feed_desc: Optional description of feed
206
- :param str feed_license: Optional feed License
214
+ :param str feed_license: Optional feed license
207
215
"""
208
216
path = self ._compose_path ("feeds" )
209
217
payload = {'name' :feed_key ,
0 commit comments