Skip to content

Add create_and_get_feed function #81

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 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
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: 18 additions & 0 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,24 @@ def create_new_feed(self, feed_key, feed_desc=None, feed_license=None):
payload = {"name": feed_key, "description": feed_desc, "license": feed_license}
return self._post(path, payload)

def create_and_get_feed(
self, feed_key, detailed=False, feed_desc=None, feed_license=None
):
"""
Attempts to return a feed; if the feed does not exist, it is created, and then returned.
:param str feed_key: Adafruit IO Feed Key
:param bool detailed: Returns a more verbose existing feed record
:param str feed_desc: Optional description of feed to be created
:param str feed_license: Optional feed license to be created
"""
try:
return self.get_feed(feed_key, detailed=detailed)
except AdafruitIO_RequestError:
self.create_new_feed(
feed_key, feed_desc=feed_desc, feed_license=feed_license
)
return self.get_feed(feed_key, detailed=detailed)

def delete_feed(self, feed_key):
"""
Deletes an existing feed.
Expand Down
47 changes: 47 additions & 0 deletions examples/adafruit_io_http/adafruit_io_create_and_get_feed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
Example using create_and_get_feed. Creates a new feed if it does not exist and sends to it, or
sends to an existing feed once it has been created.
"""
import ssl
import adafruit_requests
import socketpool
import wifi
import microcontroller
from adafruit_io.adafruit_io import IO_HTTP

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials, and "aio_username" and "aio_key" keys with your
# Adafruit IO credentials, DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print(
"WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!"
)
raise

# Connect to Wi-Fi using credentials from secrets.py
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to {}!".format(secrets["ssid"]))
print("IP:", wifi.radio.ipv4_address)

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# Obtain Adafruit IO credentials from secrets.py
aio_username = secrets["aio_username"]
aio_key = secrets["aio_key"]

# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)

# Create temperature variable using the CPU temperature and print the current value.
temperature = microcontroller.cpu.temperature
print("Current CPU temperature: {0} C".format(temperature))

# Create and get feed.
io.send_data(io.create_and_get_feed("cpu-temperature-feed")["key"], temperature)