Skip to content

Commit 37663ab

Browse files
author
brentru
committed
add new time endpoint, return time as a struct_time, remove plaintext http get responses, update example to remove topics
1 parent 51b0cd9 commit 37663ab

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

Adafruit_IO/client.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
21+
from time import struct_time
2122
import json
2223
import platform
2324
import pkg_resources
@@ -107,19 +108,15 @@ def _handle_error(response):
107108
raise RequestError(response)
108109
# Else do nothing if there was no error.
109110

110-
def _compose_url(self, path, is_time=None):
111-
if is_time: # return a call to https://io.adafruit.com/api/v2/time/{unit}
112-
return '{0}/api/{1}/{2}'.format(self.base_url, 'v2', path)
111+
def _compose_url(self, path):
113112
return '{0}/api/{1}/{2}/{3}'.format(self.base_url, 'v2', self.username, path)
114113

115-
def _get(self, path, is_time=None):
116-
response = requests.get(self._compose_url(path, is_time),
114+
def _get(self, path):
115+
response = requests.get(self._compose_url(path),
117116
headers=self._headers({'X-AIO-Key': self.key}),
118117
proxies=self.proxies)
119118
self._handle_error(response)
120-
if not is_time:
121-
return response.json()
122-
return response.text
119+
return response.json()
123120

124121
def _post(self, path, data):
125122
response = requests.post(self._compose_url(path),
@@ -180,12 +177,14 @@ def append(self, feed, value):
180177
"""
181178
return self.create_data(feed, Data(value=value))
182179

183-
def receive_time(self, time):
184-
"""Returns the time from the Adafruit IO server.
185-
:param string time: Time to be returned: `millis`, `seconds`, `ISO-8601`.
180+
def receive_time(self):
181+
"""Returns a struct_time from the Adafruit IO Server based on the device's IP address.
182+
https://docs.python.org/3.7/library/time.html#time.struct_time
186183
"""
187-
timepath = "time/{0}".format(time)
188-
return self._get(timepath, is_time=True)
184+
path = 'integrations/time/struct.json'
185+
time = self._get(path)
186+
return struct_time((time['year'], time['mon'], time['mday'], time['hour'],
187+
time['min'], time['sec'], time['wday'], time['yday'], time['isdst']))
189188

190189
def receive_weather(self, weather_id=None):
191190
"""Adafruit IO Weather Service, Powered by Dark Sky
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""
2-
`time-topics.py`
3-
====================================
2+
`time.py`
3+
==========================================
44
Don't have a RTC handy and need
55
accurate time measurements?
66
7-
Let Adafruit IO serve real-time values!
7+
Let Adafruit IO serve up real-time values
8+
based off your device's IP-address!
89
910
Author: Brent Rubell
1011
"""
@@ -23,16 +24,8 @@
2324
# Create an instance of the REST client.
2425
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
2526

26-
print('---Adafruit IO REST API Time Helpers---')
27-
28-
print('Seconds: aio.receive_time(seconds)')
29-
secs_val = aio.receive_time('seconds')
30-
print('\t' + secs_val)
31-
32-
print('Milliseconds: aio.receive_time(millis)')
33-
ms_val = aio.receive_time('millis')
34-
print('\t' + ms_val)
35-
36-
print('ISO-8601: aio.receive_time(ISO-8601)')
37-
iso_val = aio.receive_time('ISO-8601')
38-
print('\t' + iso_val)
27+
# Get the time from Adafruit IO
28+
time = aio.receive_time()
29+
# Time is returned as a `struct_time`
30+
# https://docs.python.org/3.7/library/time.html#time.struct_time
31+
print(time)

0 commit comments

Comments
 (0)