|
18 | 18 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19 | 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20 | 20 | # SOFTWARE.
|
| 21 | +from time import struct_time |
21 | 22 | import json
|
22 | 23 | import platform
|
23 | 24 | import pkg_resources
|
@@ -107,19 +108,15 @@ def _handle_error(response):
|
107 | 108 | raise RequestError(response)
|
108 | 109 | # Else do nothing if there was no error.
|
109 | 110 |
|
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): |
113 | 112 | return '{0}/api/{1}/{2}/{3}'.format(self.base_url, 'v2', self.username, path)
|
114 | 113 |
|
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), |
117 | 116 | headers=self._headers({'X-AIO-Key': self.key}),
|
118 | 117 | proxies=self.proxies)
|
119 | 118 | self._handle_error(response)
|
120 |
| - if not is_time: |
121 |
| - return response.json() |
122 |
| - return response.text |
| 119 | + return response.json() |
123 | 120 |
|
124 | 121 | def _post(self, path, data):
|
125 | 122 | response = requests.post(self._compose_url(path),
|
@@ -180,12 +177,14 @@ def append(self, feed, value):
|
180 | 177 | """
|
181 | 178 | return self.create_data(feed, Data(value=value))
|
182 | 179 |
|
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 |
186 | 183 | """
|
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'])) |
189 | 188 |
|
190 | 189 | def receive_weather(self, weather_id=None):
|
191 | 190 | """Adafruit IO Weather Service, Powered by Dark Sky
|
|
0 commit comments