AsyncRestSession: Fix handling JSONDecodeError for 404 #319
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The API returns HTML instead of JSON for 404 responses for nonexistent endpoints,
e.g.,
GET "/networks/{networkId}/appliance/firewall/multicastForwarding".(Note: the correct endpoint is
"/organizations/{organizationId}/appliance/firewall/multicastForwarding/byNetwork".)meraki.rest_session.RestSessionraises anAPIErrorwith.status = 404and
messageset to the first few bytes of the HTML and"please wait a minute if the key or org was just newly created."appended:"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtmplease wait a minute if the key or org was just newly created.".meraki.aio.rest_session.AsyncRestSessioninstead tries to parse the message as JSON
and does not handle the resulting
JSONDecodeError. That exception does not include the status code so it's not useful.Fix the typo in handling JSON decode errors for 4xx responses.
To clarify the usecase:
nac-collectoraims to collect all of the (Meraki in this case) configuration from the API. Endpoint URLs are taken from Terraform provider definitions which are themselves based on the OpenAPI spec (POST/PUT endpoints) (e.g. https://github.com/CiscoDevNet/terraform-provider-meraki/blob/main/gen/definitions/appliance_firewall_multicast_forwarding.yaml#L4). All the responses, including error messages, are collected in a JSON usingAsyncRestSession.get_pages()directly to request them: https://github.com/netascode/nac-collector/pull/186/changes#diff-1772d211aa8f1f65aa88019578f0f6f43ea10bbe13127726378c0e284e33b097R413.Not requesting the nonexistent endpoints is taken care of in netascode/nac-collector@41c590a, but handling the errors is still useful for any future cases (which will have to be worked around in the same way manually after the fact, at least for now).