Skip to content

Commit ca5374f

Browse files
author
brentru
committed
add more robust requests test
1 parent f4e0086 commit ca5374f

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

examples/requests_simpletest.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
from adafruit_esp32spi import adafruit_esp32spi
77
import adafruit_requests as requests
88

9-
print("ESP32 SPI webclient test")
10-
11-
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
12-
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
13-
14-
159
# If you are using a board with pre-defined ESP32 Pins:
1610
esp32_cs = DigitalInOut(board.ESP_CS)
1711
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -24,7 +18,6 @@
2418

2519
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2620
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
27-
requests.set_socket(socket, esp)
2821

2922
print("Connecting to AP...")
3023
while not esp.is_connected:
@@ -35,20 +28,26 @@
3528
continue
3629
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
3730

38-
#esp._debug = True
39-
print("Fetching text from", TEXT_URL)
40-
r = requests.get(TEXT_URL)
31+
requests.set_socket(socket, esp)
32+
33+
# Initialize a requests object with a socket and esp32spi interface
34+
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
35+
36+
print("Fetching text from %s"%TEXT_URL)
37+
response = requests.get(TEXT_URL)
38+
print('-'*40)
39+
40+
print("Text Response: ", response.text)
4141
print('-'*40)
42-
print(r.text)
42+
43+
print("Raw Response, as bytes: {0}".format(response.content))
4344
print('-'*40)
44-
r.close()
4545

46-
print()
47-
print("Fetching json from", JSON_URL)
48-
r = requests.get(JSON_URL)
46+
print("Response's HTTP Status Code: %d"%response.status_code)
4947
print('-'*40)
50-
print(r.json())
48+
49+
print("Response's HTTP Headers: %s"%response.headers)
5150
print('-'*40)
52-
r.close()
5351

54-
print("Done!")
52+
# Close, delete and collect the response data
53+
response.close()

0 commit comments

Comments
 (0)