Skip to content

Adafruit_Hue-Circuitpython_Requests - File "adafruit_requests.py", line 419, in json #125

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

Closed
cn-schuh opened this issue Jan 11, 2023 · 9 comments

Comments

@cn-schuh
Copy link

cn-schuh commented Jan 11, 2023

Hi, i am not sure this is the correct way.

Currently i am experiencing with the adafruit_hue Circuitpython lib

I am using a Adafruit Feather RP2040 with a AirLift FeatherWing – ESP32 WiFi Co-Processor. (optianally i did a test with an EdgeBadge and and a Airlift Breakout).
I tested Circuitpython 7.3.3 and 8.0.0 - Beta 6 and the controller should be compatible, i guess.

Following this guide:
https://learn.adafruit.com/pyportal-phi ... controller
or other available guides regarding Adafruit_Hue.

My code fails always with the same error:

Traceback (most recent call last):
File "code.py", line 49, in
File "/lib/adafruit_hue.py", line 109, in discover_bridge
File "/lib/adafruit_requests.py", line 419, in json
ValueError: syntax error in JSON

during executing of functions like
ip = my_bridge.discover_bridge()
or
my_bridge.get_groups()
and all other HUE functions.

The JSON file of my HUE Box is arround 30 KB. i guess it must be something related on the format. The JSON itself is valid.

To be sure the problem is not related on the Adafruit_HUE lib, I did another test, without Adafruit_HUE:

Usage of the test was the guide below:
https://learn.adafruit.com/adafruit-air ... et-connect

Result:
The first json cames from the guide above, the second and third JSON from my HUE bridge.

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json

{'time': {'updated': 'Jan 11, 2023 08:28:00 UTC', 'updatedISO': '2023-01-11T08:28:00+00:00', 'updateduk': 'Jan 11, 2023 at 08:28 GMT'}, 'disclaimer': 'This data was produced from the CoinDesk BANNED Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'description': 'United States Dollar', 'rate_float': 17428.1, 'rate': '17,428.0921'}}}

Fetching json from http://192.168.178.26/api/-lxlSLEO1If8k ... ZdrJJNdU4/

Traceback (most recent call last):
File "code.py", line 71, in
File "adafruit_requests.py", line 419, in json
ValueError: syntax error in JSON

Fetching json from http://192.168.178.26/api/-lxlSLEO1If8k ... 4/lights/7

Traceback (most recent call last):
File "code.py", line 71, in
File "adafruit_requests.py", line 419, in json
ValueError: syntax error in JSON

Code done running.

As you can see, the error File "adafruit_requests.py", line 419, in json ValueError: syntax error in JSON is always identical.

@anecdata
Copy link
Member

anecdata commented Jan 11, 2023

You may be experiencing #38. Can you print the headers that come back in the response to the request (print(r.headers))? It would also be helpful for test purposes to print the response content (or text) instead of the JSON (print(r.content)), you may see partial (or no) data.

@cn-schuh
Copy link
Author

From Python:
Fetching JSON data from http://192.168.178.26/api/-lxlSLEO1If8kEjry2ar2jrv6c4WhZEZdrJJNdU4
{'date': 'Wed, 11 Jan 2023 15:02:18 GMT', 'pragma': 'no-cache', 'expires': 'Mon, 1 Aug 2011 09:00:00 GMT', 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'server': 'nginx', 'connection': 'close', 'access-control-allow-methods': 'POST, GET, OPTIONS, PUT, DELETE, HEAD', 'access-control-allow-headers': 'Content-Type', 'content-security-policy': "default-src 'self'", 'access-control-allow-credentials': 'true', 'access-control-max-age': '3600', 'cache-control': 'no-store', 'access-control-allow-origin': '*', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'referrer-policy': 'no-referrer'}

From Hue Bridge:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, HEAD
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, no-store
Connection: close
Content-Security-Policy: default-src 'self'
Content-Type: application/json
Date: Wed, 11 Jan 2023 14:56:16 GMT
Expires: Mon, 1 Aug 2011 09:00:00 GMT
Pragma: no-cache, no-cache
Referrer-Policy: no-referrer
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: de,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Host: 192.168.178.26
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

@cn-schuh
Copy link
Author

One further info. I can fire a event once a time before the syntax error occurs.
For example: my_bridge.set_light(48, on=True) is turning light 48 on, but than code crashed.

@anecdata
Copy link
Member

anecdata commented Jan 11, 2023

Those seem to be OK, except that there's no content-length or chunked encoding. There is an open PR #123 to handle that case. You could test the library code in that PR.

What do you get when you print(r.content) instead of print(r.json)?

@cn-schuh
Copy link
Author

response.json():
Traceback (most recent call last):
File "code.py", line 54, in
File "adafruit_requests.py", line 419, in json
ValueError: syntax error in JSON

response.content:
b''

I guess both contents are empty

@anecdata
Copy link
Member

anecdata commented Jan 11, 2023

You've verified that the JSON is valid, so I'd suggest downloading the requests library code from the PR above and seeing if that helps.

(wait a full minute or more for the response, or put in a smaller timeout in requests, e.g., r = requests.get(TEXT_URL, timeout=10))

@cn-schuh
Copy link
Author

Thanks, i will run some tests with PR #123 and reply with an update.

@cn-schuh
Copy link
Author

Currently the test seems to be stable. The error can be reproduced only when request.get will be executed quite often or during or the json file is huge. But this can be handled. Definetely the Timeout is an improvement.
Thanks.

@anecdata
Copy link
Member

Closing as fixed by #123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants