Skip to content

Commit 4b4db6f

Browse files
authored
Merge pull request #81 from kattni/create-and-get-feed
Add create_and_get_feed function
2 parents d2c5154 + 5c356b1 commit 4b4db6f

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

adafruit_io/adafruit_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,24 @@ def create_new_feed(self, feed_key, feed_desc=None, feed_license=None):
686686
payload = {"name": feed_key, "description": feed_desc, "license": feed_license}
687687
return self._post(path, payload)
688688

689+
def create_and_get_feed(
690+
self, feed_key, detailed=False, feed_desc=None, feed_license=None
691+
):
692+
"""
693+
Attempts to return a feed; if the feed does not exist, it is created, and then returned.
694+
:param str feed_key: Adafruit IO Feed Key
695+
:param bool detailed: Returns a more verbose existing feed record
696+
:param str feed_desc: Optional description of feed to be created
697+
:param str feed_license: Optional feed license to be created
698+
"""
699+
try:
700+
return self.get_feed(feed_key, detailed=detailed)
701+
except AdafruitIO_RequestError:
702+
self.create_new_feed(
703+
feed_key, feed_desc=feed_desc, feed_license=feed_license
704+
)
705+
return self.get_feed(feed_key, detailed=detailed)
706+
689707
def delete_feed(self, feed_key):
690708
"""
691709
Deletes an existing feed.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
Example using create_and_get_feed. Creates a new feed if it does not exist and sends to it, or
5+
sends to an existing feed once it has been created.
6+
"""
7+
import ssl
8+
import adafruit_requests
9+
import socketpool
10+
import wifi
11+
import microcontroller
12+
from adafruit_io.adafruit_io import IO_HTTP
13+
14+
# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
15+
# "password" keys with your WiFi credentials, and "aio_username" and "aio_key" keys with your
16+
# Adafruit IO credentials, DO NOT share that file or commit it into Git or other
17+
# source control.
18+
# pylint: disable=no-name-in-module,wrong-import-order
19+
try:
20+
from secrets import secrets
21+
except ImportError:
22+
print(
23+
"WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!"
24+
)
25+
raise
26+
27+
# Connect to Wi-Fi using credentials from secrets.py
28+
wifi.radio.connect(secrets["ssid"], secrets["password"])
29+
print("Connected to {}!".format(secrets["ssid"]))
30+
print("IP:", wifi.radio.ipv4_address)
31+
32+
pool = socketpool.SocketPool(wifi.radio)
33+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
34+
35+
# Obtain Adafruit IO credentials from secrets.py
36+
aio_username = secrets["aio_username"]
37+
aio_key = secrets["aio_key"]
38+
39+
# Initialize an Adafruit IO HTTP API object
40+
io = IO_HTTP(aio_username, aio_key, requests)
41+
42+
# Create temperature variable using the CPU temperature and print the current value.
43+
temperature = microcontroller.cpu.temperature
44+
print("Current CPU temperature: {0} C".format(temperature))
45+
46+
# Create and get feed.
47+
io.send_data(io.create_and_get_feed("cpu-temperature-feed")["key"], temperature)

0 commit comments

Comments
 (0)