From fbbfe8ffae49fec6b7f7850eae2d09ff4752d99a Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 8 Jan 2019 12:49:40 -0500 Subject: [PATCH] add darkskies api to io python client --- Adafruit_IO/client.py | 13 ++++++++++--- examples/basics/darkskies.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 examples/basics/darkskies.py diff --git a/Adafruit_IO/client.py b/Adafruit_IO/client.py index 76d603b..6809371 100644 --- a/Adafruit_IO/client.py +++ b/Adafruit_IO/client.py @@ -61,10 +61,10 @@ class Client(object): self.base_url = base_url.rstrip('/') def _compose_url(self, path, is_time=None): - if not is_time: + if is_time: # return a call to https://io.adafruit.com/api/v2/time/{unit} + return '{0}/api/{1}/{2}'.format(self.base_url, self.api_version, path) + else: return '{0}/api/{1}/{2}/{3}'.format(self.base_url, self.api_version, self.username, path) - else: # return a call to https://io.adafruit.com/api/v2/time/{unit} - return '{0}/api/{1}/{2}'.format(self.base_url, self.api_version, path) def _handle_error(self, response): @@ -161,6 +161,13 @@ class Client(object): """ timepath = "time/{0}".format(time) return self._get(timepath, is_time=True) + + def receive_weather(self, weather_id): + """Get the specified weather record with current weather and all available forecast information. + :param int id: ID for forecast + """ + weatherpath = "integrations/weather/{0}".format(weather_id) + return self._get(weatherpath) def receive(self, feed): """Retrieve the most recent value for the specified feed. Returns a Data diff --git a/examples/basics/darkskies.py b/examples/basics/darkskies.py new file mode 100644 index 0000000..0fa5b8c --- /dev/null +++ b/examples/basics/darkskies.py @@ -0,0 +1,24 @@ +""" +Example of getting a darkskies forecast +from Adafruit IO. +""" +# import Adafruit IO REST client. +from Adafruit_IO import Client, Feed, RequestError + +# Set to your Adafruit IO key. +# Remember, your key is a secret, +# so make sure not to publish it when you publish this code! +ADAFRUIT_IO_KEY = 'PASS' + +# Set to your Adafruit IO username. +# (go to https://accounts.adafruit.com to find your username) +ADAFRUIT_IO_USERNAME = 'USER' + +# Create an instance of the REST client. +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) + + + +forecast = aio.receive_weather(2153) + +print(forecast) \ No newline at end of file