Commit 13a320d8 authored by Morten Brekkevold's avatar Morten Brekkevold
Browse files

Translate read timeouts to regular TimeoutErrors

Because:
- simeple_rest_client raises generic "ClientConnectionErrors" on read
  timeouts. The real reason is only discoverable by checking the
  exception's __cause__.
parent b09a548e
"""Very simple interface against the NAV API"""
import functools
from requests.exceptions import ReadTimeout
from simple_rest_client.api import API
from simple_rest_client.resource import Resource
from simple_rest_client.exceptions import NotFoundError, ClientError
from simple_rest_client.exceptions import (
NotFoundError, ClientError, ClientConnectionError
)
def _translate_notfounderror(func):
"""Decorator for standardized handling of NotFoundError"""
def _translate_exceptions(func):
"""Decorator for standardized handling/translation of various exceptiosn that
might be raised by the underlying libraries.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
......@@ -15,6 +21,11 @@ def _translate_notfounderror(func):
return func(*args, **kwargs)
except NotFoundError as error:
raise ObjectNotFound(error.response.body)
except ClientConnectionError as error:
if isinstance(error.__cause__, ReadTimeout):
raise TimeoutError("Timed out reading NAV API")
else:
raise
return wrapper
......@@ -38,32 +49,37 @@ class NAVAPI(object):
resource_name="management_profile", resource_class=ManagementProfileResource
)
@_translate_exceptions
def list_netboxes(self):
response = self.api.netbox.list(params={"page_size": 1000})
return response.body["results"]
@_translate_notfounderror
@_translate_exceptions
def get_netbox(self, netbox_id):
return self.api.netbox.retrieve(netbox_id)
@_translate_exceptions
def post_netbox(self, netbox):
return self.api.netbox.create(body=netbox)
@_translate_notfounderror
@_translate_exceptions
def patch_netbox(self, netbox_id, patch):
return self.api.netbox.partial_update(netbox_id, body=patch)
@_translate_exceptions
def delete_netbox(self, netbox_id):
return self.api.netbox.destroy(netbox_id)
@_translate_notfounderror
@_translate_exceptions
def get_room(self, room_id):
return self.api.room.retrieve(room_id)
@_translate_exceptions
def post_room(self, room):
return self.api.room.create(body=room)
@functools.lru_cache(1)
@_translate_exceptions
def get_version(self):
try:
response = self.api.version.list()
......@@ -73,14 +89,16 @@ class NAVAPI(object):
version = response.body.get("version")
return tuple(int(v) if v.isdigit() else v for v in version.split("."))
@_translate_exceptions
def list_management_profiles(self):
response = self.api.management_profile.list(params={"page_size": 1000})
return response.body["results"]
@_translate_notfounderror
@_translate_exceptions
def get_management_profile(self, profile_id):
return self.api.management_profile.retrieve(profile_id)
@_translate_exceptions
def post_management_profile(self, profile):
return self.api.management_profile.create(body=profile)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment