-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: Refactor Nautobot api client and update logging / exception handling #666
Conversation
stevekeay
commented
Feb 11, 2025
•
edited
Loading
edited
- make sure exceptions have useful "message"
- consider HTTP redirect responses to be errors
- include URL and payload in exceptions on failed API request
- remove default params (all our callers pass all the args)
- don't log errors, just raise the exception
- re-order args to make_api_request to match the requests library
- combine api request and response into single log message
- pretty-print payload data when logging Nautobot API requests
c2c7f21
to
dcd16f6
Compare
response = self.make_api_request(url, "get", params=params) | ||
if not response: | ||
return False | ||
params = {"interface_id": interface_id, "vlan_tag": str(vlan_tag)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the conversion to integer was intentional here - we want to fail early if the user calls this method with a non-numeric vlan_tag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I will put it back - I naively assumed that a parameter of type "int" must contain an int value :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python's type whispering in full force. It's more like an ask instead of an insistence.
- make sure exceptions have useful "message" - consider HTTP redirect responses to be errors - include URL and payload in exceptions on failed API request - remove default params (all our callers pass all the args) - don't log errors, just raise the exception - re-order args to make_api_request to match the requests library - combine api request and response into single log message
dcd16f6
to
9456ad4
Compare
|
||
if response.status_code >= 300: | ||
raise NautobotRequestError( | ||
code=response.status_code, url=full_url, body=response.content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code=response.status_code, url=full_url, body=response.content | |
code=response.status_code, url=full_url, body=response.content |
long term you'll probably want the response.json()["error"]
by default.