Skip to content

Commit 7c45cdd

Browse files
committed
merge main. remove single request before status codes loop. Use with context inside status codes loop. Add expected code to print out.
1 parent 8aa608f commit 7c45cdd

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

examples/wifi/requests_wifi_status_codes.py renamed to examples/wifi/expanded/requests_wifi_status_codes.py

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
""" WiFi Status Codes Example """
77

88
import os
9+
import time
910

1011
import adafruit_connection_manager
1112
import wifi
@@ -23,18 +24,29 @@
2324
rssi = wifi.radio.ap_info.rssi
2425

2526

26-
def print_http_status(code, description):
27+
def print_http_status(expected_code, actual_code, description):
2728
"""Returns HTTP status code and description"""
28-
if "100" <= code <= "103":
29-
print(f" | ✅ Status Test: {code} - {description}")
30-
elif "200" == code:
31-
print(f" | 🆗 Status Test: {code} - {description}")
32-
elif "201" <= code <= "299":
33-
print(f" | ✅ Status Test: {code} - {description}")
34-
elif "300" <= code <= "600":
35-
print(f" | ❌ Status Test: {code} - {description}")
29+
if "100" <= actual_code <= "103":
30+
print(
31+
f" | ✅ Status Test Expected: {expected_code} Actual: {actual_code} - {description}"
32+
)
33+
elif "200" == actual_code:
34+
print(
35+
f" | 🆗 Status Test Expected: {expected_code} Actual: {actual_code} - {description}"
36+
)
37+
elif "201" <= actual_code <= "299":
38+
print(
39+
f" | ✅ Status Test Expected: {expected_code} Actual: {actual_code} - {description}"
40+
)
41+
elif "300" <= actual_code <= "600":
42+
print(
43+
f" | ❌ Status Test Expected: {expected_code} Actual: {actual_code} - {description}"
44+
)
3645
else:
37-
print(f" | Unknown Response Status: {code} - {description}")
46+
print(
47+
f" | Unknown Response Status Expected: {expected_code} "
48+
+ f"Actual: {actual_code} - {description}"
49+
)
3850

3951

4052
# All HTTP Status Codes
@@ -104,7 +116,6 @@ def print_http_status(code, description):
104116
"511": "Network Authentication Required",
105117
}
106118

107-
JSON_GET_URL = "https://httpbin.org/get"
108119
STATUS_TEST_URL = "https://httpbin.org/status/"
109120

110121
print(f"\nConnecting to {ssid}...")
@@ -116,37 +127,22 @@ def print_http_status(code, description):
116127
print(f"❌ OSError: {e}")
117128
print("✅ Wifi!")
118129

119-
# Define a custom header as a dict.
120-
HEADERS = {"user-agent": "blinka/1.0.0"}
121-
122-
print(f" | GET JSON: {JSON_GET_URL}")
123-
with requests.get(JSON_GET_URL, headers=HEADERS) as response:
124-
json_data = response.json()
125-
HEADERS = json_data["headers"]
126-
print(f" | User-Agent: {HEADERS['User-Agent']}")
127130

128-
# HTTP STATUS CODE TESTING
129-
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
130-
STATUS_CODE = str(response.status_code)
131-
STATUS_DESCRIPTION = http_status_codes.get(STATUS_CODE, "Unknown Status Code")
132-
print_http_status(STATUS_CODE, STATUS_DESCRIPTION)
133-
response.close()
134-
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
135-
print(" | ")
136-
137-
print(f" | Status Code Test: {STATUS_TEST_URL}")
138-
# Some return errors then confirm the error (that's a good thing)
139-
# Demonstrates not all errors have the same behavior
140-
# 300, 304, and 306 in particular
141-
for codes in sorted(http_status_codes.keys(), key=int):
142-
header_status_test_url = STATUS_TEST_URL + codes
143-
response = requests.get(header_status_test_url, headers=HEADERS)
144-
SORT_STATUS_CODE = str(response.status_code)
131+
print(f" | Status Code Test: {STATUS_TEST_URL}")
132+
# Some return errors then confirm the error (that's a good thing)
133+
# Demonstrates not all errors have the same behavior
134+
# Some 300 level responses contain redirects that requests automatically follows
135+
# By default the response object will contain the status code from the final
136+
# response after all redirect, so it can differ from the expected status code.
137+
for current_code in sorted(http_status_codes.keys(), key=int):
138+
header_status_test_url = STATUS_TEST_URL + current_code
139+
with requests.get(header_status_test_url) as response:
140+
response_status_code = str(response.status_code)
145141
SORT_STATUS_DESC = http_status_codes.get(
146-
SORT_STATUS_CODE, "Unknown Status Code"
142+
response_status_code, "Unknown Status Code"
147143
)
148-
print_http_status(SORT_STATUS_CODE, SORT_STATUS_DESC)
149-
150-
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
144+
print_http_status(current_code, response_status_code, SORT_STATUS_DESC)
151145

152-
print("Finished!")
146+
# Rate limit ourselves a little to avoid strain on server
147+
time.sleep(0.5)
148+
print("Finished!")

0 commit comments

Comments
 (0)