Skip to content
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

Merged
merged 2 commits into from
Feb 11, 2025

Conversation

stevekeay
Copy link
Contributor

@stevekeay stevekeay commented Feb 11, 2025

  • 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
    image

@stevekeay stevekeay force-pushed the refactor-nautobot-logging-exceptions branch from c2c7f21 to dcd16f6 Compare February 11, 2025 14:16
response = self.make_api_request(url, "get", params=params)
if not response:
return False
params = {"interface_id": interface_id, "vlan_tag": str(vlan_tag)}
Copy link
Collaborator

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

Copy link
Contributor Author

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 :)

Copy link
Contributor

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.

Steve Keay added 2 commits February 11, 2025 17:13
- 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
@stevekeay stevekeay force-pushed the refactor-nautobot-logging-exceptions branch from dcd16f6 to 9456ad4 Compare February 11, 2025 17:14

if response.status_code >= 300:
raise NautobotRequestError(
code=response.status_code, url=full_url, body=response.content
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@stevekeay stevekeay added this pull request to the merge queue Feb 11, 2025
Merged via the queue into main with commit 21d7863 Feb 11, 2025
26 checks passed
@stevekeay stevekeay deleted the refactor-nautobot-logging-exceptions branch February 11, 2025 17:22
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

Successfully merging this pull request may close these issues.

3 participants