Commit 6f4c3f52 authored by unknown's avatar unknown
Browse files

redefine update_facade route, prevent retrying on VersionConflictError,...

redefine update_facade route, prevent retrying on VersionConflictError, convert all _req method to _req_with_retry
parent 9ef8e9a9
...@@ -23,6 +23,25 @@ class VersionConflictError(Exception): ...@@ -23,6 +23,25 @@ class VersionConflictError(Exception):
pass pass
def retry_on_exception(exception) -> bool:
"""
Returns True if the exception is in the list of allowed exceptions
Return False if VersionConflictError
Returns:
[bool]: True exception raised in in list of exceptions
"""
list_exc_to_raise = [Exception, HTTPError]
raise_error = (
True
if any([isinstance(exception, exc) for exc in list_exc_to_raise])
and not isinstance(exception, VersionConflictError)
else False
)
return raise_error
class BuildingGeneric: class BuildingGeneric:
""" """
...@@ -42,7 +61,9 @@ class BuildingGeneric: ...@@ -42,7 +61,9 @@ class BuildingGeneric:
This way, a VersionConflictError pops whenever a conflicts of versions happens This way, a VersionConflictError pops whenever a conflicts of versions happens
""" """
def __init__(self, uid: str = None, staging: bool = False, registry: MaxIcsRegistry = None, **kwargs) -> None: def __init__(
self, uid: str = None, staging: bool = False, registry: MaxIcsRegistry = None, **kwargs
) -> None:
self.logger = logging.getLogger(self.__class__.__name__) self.logger = logging.getLogger(self.__class__.__name__)
self._uid = uid self._uid = uid
...@@ -64,16 +85,15 @@ class BuildingGeneric: ...@@ -64,16 +85,15 @@ class BuildingGeneric:
id=self._building["id"], id=self._building["id"],
address=self._building["address"]["unstructured_query"], address=self._building["address"]["unstructured_query"],
country_code=self._building["address"]["country_code"], country_code=self._building["address"]["country_code"],
already_exists=self._building["already_exists"] already_exists=self._building["already_exists"],
) )
def __repr__(self): def __repr__(self):
return "id: {id}, address: {address}, country_code: {country_code}, already_exists: {already_exists}".format( return "id: {id}, address: {address}, country_code: {country_code}, already_exists: {already_exists}".format(
id=self._building["id"], id=self._building["id"],
address=self._building["address"]["unstructured_query"], address=self._building["address"]["unstructured_query"],
country_code=self._building["address"]["country_code"], country_code=self._building["address"]["country_code"],
already_exists=self._building["already_exists"] already_exists=self._building["already_exists"],
) )
# ----------------- Building level --------------------- # ----------------- Building level ---------------------
...@@ -103,7 +123,7 @@ class BuildingGeneric: ...@@ -103,7 +123,7 @@ class BuildingGeneric:
Update ES doc with information at building level Update ES doc with information at building level
Note: the retries for conflict error are managed within BKC API Note: the retries for conflict error are managed within BKC API
""" """
self._req("put", "/".join([self.settings.base_url, self._uid]), json=payload) self._req_with_retries("put", "/".join([self.settings.base_url, self._uid]), json=payload)
def update_status_history(self, status: str, **kwargs) -> None: def update_status_history(self, status: str, **kwargs) -> None:
""" """
...@@ -113,7 +133,7 @@ class BuildingGeneric: ...@@ -113,7 +133,7 @@ class BuildingGeneric:
payload = {"new_entry": status} payload = {"new_entry": status}
url = "/".join([self.settings.base_url, self._uid, "history"]) url = "/".join([self.settings.base_url, self._uid, "history"])
return self._req(method="put", url=url, json=payload, **kwargs) return self._req_with_retries(method="put", url=url, json=payload, **kwargs)
def register_error_for_building(self, error_code: str): def register_error_for_building(self, error_code: str):
""" """
...@@ -123,7 +143,7 @@ class BuildingGeneric: ...@@ -123,7 +143,7 @@ class BuildingGeneric:
url = "/".join([self.settings.base_url, self._uid, "errors"]) url = "/".join([self.settings.base_url, self._uid, "errors"])
payload = {"error_code": error_code} payload = {"error_code": error_code}
return self._req(method="put", url=url, json=payload) return self._req_with_retries(method="put", url=url, json=payload)
def upload_metadata_for_building(self, name: str, payload: dict): def upload_metadata_for_building(self, name: str, payload: dict):
"""Upload the facade metadata to Hbase""" """Upload the facade metadata to Hbase"""
...@@ -134,11 +154,15 @@ class BuildingGeneric: ...@@ -134,11 +154,15 @@ class BuildingGeneric:
def get_altitudes(self): def get_altitudes(self):
"""Retrieve the building and exterior altitudes from Hbase""" """Retrieve the building and exterior altitudes from Hbase"""
return self._req_with_retries("get", "/".join([self.settings.base_url, self._uid, "altitudes"])) return self._req_with_retries(
"get", "/".join([self.settings.base_url, self._uid, "altitudes"])
)
def upload_altitudes(self, payload: dict): def upload_altitudes(self, payload: dict):
"""Upload the building and exterior altitudes to Hbase""" """Upload the building and exterior altitudes to Hbase"""
return self._req_with_retries("put", "/".join([self.settings.base_url, self._uid, "altitudes"]), json=payload) return self._req_with_retries(
"put", "/".join([self.settings.base_url, self._uid, "altitudes"]), json=payload
)
# ----------------- Roof level --------------------- # ----------------- Roof level ---------------------
def get_roof_image_by_name(self, image_name: str) -> str: def get_roof_image_by_name(self, image_name: str) -> str:
...@@ -213,22 +237,31 @@ class BuildingGeneric: ...@@ -213,22 +237,31 @@ class BuildingGeneric:
def upload_metadata_for_facade(self, facade_id, name: str, payload: dict): def upload_metadata_for_facade(self, facade_id, name: str, payload: dict):
"""Upload the facade metadata to Hbase""" """Upload the facade metadata to Hbase"""
body = {"metadata": payload} body = {"metadata": payload}
self._req_with_retries( url = "/".join([self.settings.base_url, self._uid, "facade", facade_id, "metadata", name])
"post", self._req_with_retries("post", url=url, json=body)
"/".join([self.settings.base_url, self._uid, "facade", facade_id, "metadata", name]),
json=body,
)
def register_error_for_facade(self, error_code: str, facade_id: str): def register_error_for_facade(self, error_code: str, facade_id: str):
"""Add an error at facade level to ES doc""" """Add an error at facade level to ES doc"""
url = "/".join([self.settings.base_url, self._uid, "facade", facade_id, "errors"]) url = "/".join([self.settings.base_url, self._uid, "facade", facade_id, "errors"])
payload = {"error_code": error_code} payload = {"error_code": error_code}
return self._force_update_with_version(payload=payload, url=url) return self._req_with_retries("put", url=url, json=payload)
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=WAIT_BETWEEN_RETRIES_IN_MS)
def update_facade(self, visible_facade_id, **kwargs) -> None: def update_facade(self, visible_facade_id, **kwargs) -> None:
"""Update facade object (all the facades are fetched and uploaded)""" url = "/".join([self.settings.base_url, self._uid, "facade", visible_facade_id])
self._req_with_retries("put", url=url, json=kwargs)
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=WAIT_BETWEEN_RETRIES_IN_MS)
def update_facade_with_filtering(self, visible_facade_id, **kwargs) -> None:
"""
Update a visible facade (all the facades are fetched and uploaded)
The facade object is cleaned from null and None values
NOTE:
This method is deprecated since the visible facades now get updated from within the BKC API, not from the nodes
Now replaced by update_facade method
"""
self.fetch() self.fetch()
for f_index, facade in enumerate(self._building["facade"]): for f_index, facade in enumerate(self._building["facade"]):
for v_index, visible_facade in enumerate(facade["visible_facades"]): for v_index, visible_facade in enumerate(facade["visible_facades"]):
...@@ -275,7 +308,11 @@ class BuildingGeneric: ...@@ -275,7 +308,11 @@ class BuildingGeneric:
self.fetch() self.fetch()
# ----------------- Req methods --------------------- # ----------------- Req methods ---------------------
@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=WAIT_BETWEEN_RETRIES_IN_MS) @retry(
stop_max_attempt_number=MAX_RETRIES,
wait_fixed=WAIT_BETWEEN_RETRIES_IN_MS,
retry_on_exception=retry_on_exception,
)
def _req_with_retries( def _req_with_retries(
self, self,
method: str, method: str,
...@@ -284,7 +321,7 @@ class BuildingGeneric: ...@@ -284,7 +321,7 @@ class BuildingGeneric:
headers: Optional[dict] = None, headers: Optional[dict] = None,
**kwargs **kwargs
): ):
"""Request method. Retry if it failed""" """Request method. Retry if it failed, except in VersionConflictError"""
res = self._req(method=method, url=url, json=json, headers=headers, **kwargs) res = self._req(method=method, url=url, json=json, headers=headers, **kwargs)
return res return res
...@@ -340,4 +377,3 @@ class BuildingGeneric: ...@@ -340,4 +377,3 @@ class BuildingGeneric:
""" """
res = self._req("put", url, json={**payload, "version": str(self._building["version"])}) res = self._req("put", url, json={**payload, "version": str(self._building["version"])})
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