adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 37 forks source link

Issue with API request on PyPortal #120

Open ysiegel29 opened 1 year ago

ysiegel29 commented 1 year ago

Hello, I am trying to download a json from an API on a pyportal Titano, the response is around 11k and work no problem in my desktop python I get a "sending request failed" other api work, wifi connection is ok

I have updated firmware to 1.7.4 I have tried circuit python 7.3.3 and 8.0.0b updating the lib accordingly

the API is "https://prim.iledefrance-mobilites.fr/marketplace/stop-monitoring?MonitoringRef=STIF:StopPoint:Q:43198:" but you need a key

I have read in a previous issue where @anecdata commented that there could be certificat issue (not sure if it should be solved by latest firmware). But did not managed to provide a certificate manually.

Could someone help? Many thanks!

dhalbert commented 1 year ago

That's a pretty large return value. Can you make a query to the same server with the same authentication that returns a much smaller result and see if it works?

ysiegel29 commented 1 year ago

I have just tried without supplying the header, a browser or python code return {'message': 'No API key found in request'}

and the circuit python code (pasted below) still return sending request failed

The code is below.

Many thanks!

import time
import board
import busio
import adafruit_requests as requests
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as socket

try:
    from secrets import secrets
except ImportError:
    print('Secrets key such as WIFI are kept in secrets.py, please add them there!')
    raise

esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
requests.set_socket(socket, esp)

if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
    print('\nPYPORTAL found and in idle mode')

# CONNECTING TO WIFI
print('-' * 40, 'Connecting to WIFI...','-' * 40, '\n')
while not esp.is_connected:
    try:
        time.sleep(1)
        esp.connect_AP(secrets['ssid'], secrets['password'])
    except RuntimeError as e:
        print('Could not connect to WIFI, retrying: ', e)
        continue
print('Connected to', str(esp.ssid, 'utf-8'), '\tRSSI:', esp.rssi, '   IP address is', esp.pretty_ip(esp.ip_address), '\n')

# TEST WIFI - OK
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
print("Fetching text from", TEXT_URL)
r = requests.get(TEXT_URL)
print(r.text)
r.close()

# API CALL - not OK
url = 'https://prim.iledefrance-mobilites.fr/marketplace/stop-monitoring?MonitoringRef=STIF:StopPoint:Q:43198:'
req = requests.get(url, headers="")
req.json()
anecdata commented 1 year ago

By adding some debug prints to requests... it's failing here with OSError: Failed to establish connection. Different than the OSError: Failed SSL handshake in #114 so at first glance seems like a more fundamental issue.

curl -v indicates it's the same SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256 as in the other issue (114), not sure if that's relevant to either issue at this point.

ysiegel29 commented 1 year ago

Thanks for looking into it... I hope it can be solved!

anecdata commented 1 year ago

Ah... trying this on ESP32-S2, it does indeed get the same OSError: Failed SSL handshake as the other issue (114). It could be that ECDHE-RSA-AES128-GCM-SHA256 is not supported, but I'm not sure.

anecdata commented 1 year ago

Turning debug modes on doesn't yield anything useful, we just don't have access to the low-level handshake details currently from the NINA firmware.

esp._debug = val  # ESP32SPI debug mode: 0, 1, 2, or 3
esp.set_esp_debug(co_val)  # NINA debug mode: True or False

(btw, testing using CircuitPython 7.3.3, NINA 1.7.4, and adafruit_requests 1.12.10)

ysiegel29 commented 1 year ago

Thanks, do you think it can be/will be solved? Beyond my weather station project with openweather my 2 other projects could not be completed due to the request lib. First one was using google calendar and faced redirect issues, I understand they are solved now but went for raspberry pi at the time. And now this one does not work either... Thanks for your help!

anecdata commented 1 year ago

@ysiegel29 You may want to try the solution from: https://github.com/adafruit/circuitpython/issues/7429#event-8392828172