From 90782c8ebe5608393ac7d5ddc7d08e680b9c43c0 Mon Sep 17 00:00:00 2001 From: Jim Abernathy Date: Mon, 1 Feb 2021 08:12:30 -0500 Subject: [PATCH 1/5] Adding new example for circuitpython requests_https --- examples/requests_https_circuitpython.py | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 examples/requests_https_circuitpython.py diff --git a/examples/requests_https_circuitpython.py b/examples/requests_https_circuitpython.py new file mode 100755 index 0000000..f08ec33 --- /dev/null +++ b/examples/requests_https_circuitpython.py @@ -0,0 +1,61 @@ +# adafruit_requests usage with a CircuitPython socket +# this has been tested with Adafruit Metro ESP32-S2 Express + +import socketpool +import ssl +import adafruit_requests as requests +import wifi +import secrets + +# Get wifi details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("WiFi secrets are kept in secrets.py, please add them there!") + raise + +print("Connecting to %s"%secrets["ssid"]) +wifi.radio.connect(secrets["ssid"], secrets["password"]) +print("Connected to %s!"%secrets["ssid"]) +print("My IP address is", wifi.radio.ipv4_address) + +socket = socketpool.SocketPool(wifi.radio) +https = requests.Session(socket, ssl.create_default_context()) + +TEXT_URL = "https://httpbin.org/get" +JSON_GET_URL = "https://httpbin.org/get" +JSON_POST_URL = "https://httpbin.org/post" + +print("Fetching text from %s" % TEXT_URL) +response = https.get(TEXT_URL) +print("-" * 40) +print("Text Response: ", response.text) +print("-" * 40) +response.close() + +print("Fetching JSON data from %s" % JSON_GET_URL) +response = https.get(JSON_GET_URL) +print("-" * 40) + +print("JSON Response: ", response.json()) +print("-" * 40) + +data = "31F" +print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) +response = https.post(JSON_POST_URL, data=data) +print("-" * 40) + +json_resp = response.json() +# Parse out the 'data' key from json_resp dict. +print("Data received from server:", json_resp["data"]) +print("-" * 40) + +json_data = {"Date": "July 25, 2019"} +print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) +response = https.post(JSON_POST_URL, json=json_data) +print("-" * 40) + +json_resp = response.json() +# Parse out the 'json' key from json_resp dict. +print("JSON Data received from server:", json_resp["json"]) +print("-" * 40) \ No newline at end of file From 7de8f29b1dc0f0ca2a4c136c7c392907894213ba Mon Sep 17 00:00:00 2001 From: Jim Abernathy Date: Tue, 2 Feb 2021 17:29:16 -0500 Subject: [PATCH 2/5] fixed some formating --- examples/requests_https_circuitpython.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/requests_https_circuitpython.py b/examples/requests_https_circuitpython.py index f08ec33..3cb269b 100755 --- a/examples/requests_https_circuitpython.py +++ b/examples/requests_https_circuitpython.py @@ -14,9 +14,9 @@ print("WiFi secrets are kept in secrets.py, please add them there!") raise -print("Connecting to %s"%secrets["ssid"]) +print("Connecting to %s" % secrets["ssid"]) wifi.radio.connect(secrets["ssid"], secrets["password"]) -print("Connected to %s!"%secrets["ssid"]) +print("Connected to %s!" % secrets["ssid"]) print("My IP address is", wifi.radio.ipv4_address) socket = socketpool.SocketPool(wifi.radio) @@ -58,4 +58,4 @@ json_resp = response.json() # Parse out the 'json' key from json_resp dict. print("JSON Data received from server:", json_resp["json"]) -print("-" * 40) \ No newline at end of file +print("-" * 40) From c6b60d9c48530a2a28ef18c271658b1be27a28f1 Mon Sep 17 00:00:00 2001 From: Jim Abernathy Date: Wed, 3 Feb 2021 16:41:18 -0500 Subject: [PATCH 3/5] added license statement --- examples/requests_https_circuitpython.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/requests_https_circuitpython.py b/examples/requests_https_circuitpython.py index 3cb269b..e63790f 100755 --- a/examples/requests_https_circuitpython.py +++ b/examples/requests_https_circuitpython.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2021 jfabernathy for Adafruit Industries +# SPDX-License-Identifier: MIT + # adafruit_requests usage with a CircuitPython socket # this has been tested with Adafruit Metro ESP32-S2 Express From d672c7a8d1d7c9b284c9bff1857ff30af2f9448a Mon Sep 17 00:00:00 2001 From: Jim Abernathy Date: Wed, 3 Feb 2021 17:30:35 -0500 Subject: [PATCH 4/5] fix misc formatting --- examples/requests_https_circuitpython.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/requests_https_circuitpython.py b/examples/requests_https_circuitpython.py index e63790f..31b7e12 100755 --- a/examples/requests_https_circuitpython.py +++ b/examples/requests_https_circuitpython.py @@ -4,11 +4,12 @@ # adafruit_requests usage with a CircuitPython socket # this has been tested with Adafruit Metro ESP32-S2 Express -import socketpool import ssl -import adafruit_requests as requests import wifi -import secrets +import socketpool + +import adafruit_requests as requests + # Get wifi details and more from a secrets.py file try: @@ -61,4 +62,4 @@ json_resp = response.json() # Parse out the 'json' key from json_resp dict. print("JSON Data received from server:", json_resp["json"]) -print("-" * 40) +print("-" * 40) \ No newline at end of file From 5bc5ddd8cf29f82f190ee6ab9f2513459cfb6813 Mon Sep 17 00:00:00 2001 From: Jim Abernathy Date: Thu, 4 Feb 2021 06:03:39 -0500 Subject: [PATCH 5/5] formating change --- examples/requests_https_circuitpython.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/requests_https_circuitpython.py b/examples/requests_https_circuitpython.py index 31b7e12..026aa2d 100755 --- a/examples/requests_https_circuitpython.py +++ b/examples/requests_https_circuitpython.py @@ -62,4 +62,4 @@ json_resp = response.json() # Parse out the 'json' key from json_resp dict. print("JSON Data received from server:", json_resp["json"]) -print("-" * 40) \ No newline at end of file +print("-" * 40)