Skip to content

Add new IP-based time endpoint #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI
https://github.com/adafruit/Adafruit_CircuitPython_ESP_ATcontrol
"""
from time import struct_time
from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError

__version__ = "0.0.0-auto.0"
Expand Down Expand Up @@ -119,18 +120,15 @@ def _post(self, path, payload):
self._handle_error(response)
return response.json()

def _get(self, path, return_text=False):
def _get(self, path):
"""
GET data from Adafruit IO
:param str path: Formatted Adafruit IO URL from _compose_path
:param bool return_text: Returns text instead of json
"""
response = self.wifi.get(
path,
headers=self._create_headers(self._aio_headers[1]))
self._handle_error(response)
if return_text:
return response.text
return response.json()

def _delete(self, path):
Expand Down Expand Up @@ -268,10 +266,12 @@ def receive_random_data(self, generator_id):
path = self._compose_path("integrations/words/{0}".format(generator_id))
return self._get(path)

def receive_time(self, time_type):
def receive_time(self):
"""
Returns the current time from the Adafruit IO Server
:param string time_type: Type of time to be returned: millis, seconds, or ISO-8601
Returns a struct_time from the Adafruit IO Server based on the device's IP address.
https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/__init__.html#time.struct_time
"""
path = 'https://io.adafruit.com/api/v2/time/{0}'.format(time_type)
return self._get(path, return_text=True)
path = self._compose_path('integrations/time/struct.json')
time = self._get(path)
return struct_time((time['year'], time['mon'], time['mday'], time['hour'],
time['min'], time['sec'], time['wday'], time['yday'], time['isdst']))