1- # Copyright (c) 2014 Adafruit Industries
1+ # Copyright (c) 2018 Adafruit Industries
22# Authors: Justin Cooper & Tony DiCola
33
44# Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -60,8 +60,11 @@ def __init__(self, username, key, proxies=None, base_url='https://io.adafruit.co
6060 # constructing the path.
6161 self .base_url = base_url .rstrip ('/' )
6262
63- def _compose_url (self , path ):
64- return '{0}/api/{1}/{2}/{3}' .format (self .base_url , self .api_version , self .username , path )
63+ def _compose_url (self , path , is_time = None ):
64+ if not is_time :
65+ return '{0}/api/{1}/{2}/{3}' .format (self .base_url , self .api_version , self .username , path )
66+ else : # return a call to https://io.adafruit.com/api/v2/time/{unit}
67+ return '{0}/api/{1}/{2}' .format (self .base_url , self .api_version , path )
6568
6669
6770 def _handle_error (self , response ):
@@ -78,12 +81,15 @@ def _headers(self, given):
7881 headers .update (given )
7982 return headers
8083
81- def _get (self , path ):
82- response = requests .get (self ._compose_url (path ),
84+ def _get (self , path , is_time = None ):
85+ response = requests .get (self ._compose_url (path , is_time ),
8386 headers = self ._headers ({'X-AIO-Key' : self .key }),
8487 proxies = self .proxies )
8588 self ._handle_error (response )
86- return response .json ()
89+ if not is_time :
90+ return response .json ()
91+ else : # time doesn't need to serialize into json, just return text
92+ return response .text
8793
8894 def _post (self , path , data ):
8995 response = requests .post (self ._compose_url (path ),
@@ -141,6 +147,15 @@ def send_location_data(self, feed, value, lat, lon, ele):
141147 """
142148 return self .create_data (feed , Data (value = value ,lat = lat , lon = lon , ele = ele ))
143149
150+ def receive_time (self , time ):
151+ """Returns the time from the Adafruit IO server.
152+
153+ args:
154+ - time (string): millis, seconds, ISO-8601
155+ """
156+ timepath = "time/{0}" .format (time )
157+ return self ._get (timepath , is_time = True )
158+
144159 def receive (self , feed ):
145160 """Retrieve the most recent value for the specified feed. Feed can be a
146161 feed ID, feed key, or feed name. Returns a Data instance whose value
0 commit comments