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):
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:
"""
......@@ -42,7 +61,9 @@ class BuildingGeneric:
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._uid = uid
......@@ -64,16 +85,15 @@ class BuildingGeneric:
id=self._building["id"],
address=self._building["address"]["unstructured_query"],
country_code=self._building["address"]["country_code"],
already_exists=self._building["already_exists"]
already_exists=self._building["already_exists"],
)
def __repr__(self):
return "id: {id}, address: {address}, country_code: {country_code}, already_exists: {already_exists}".format(
id=self._building["id"],
address=self._building["address"]["unstructured_query"],
country_code=self._building["address"]["country_code"],
already_exists=self._building["already_exists"]
already_exists=self._building["already_exists"],
)
# ----------------- Building level ---------------------
......@@ -103,7 +123,7 @@ class BuildingGeneric:
Update ES doc with information at building level
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:
"""
......@@ -113,7 +133,7 @@ class BuildingGeneric:
payload = {"new_entry": status}
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):
"""
......@@ -123,7 +143,7 @@ class BuildingGeneric:
url = "/".join([self.settings.base_url, self._uid, "errors"])
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):
"""Upload the facade metadata to Hbase"""
......@@ -134,11 +154,15 @@ class BuildingGeneric:
def get_altitudes(self):
"""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):
"""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 ---------------------
def get_roof_image_by_name(self, image_name: str) -> str:
......@@ -213,22 +237,31 @@ class BuildingGeneric:
def upload_metadata_for_facade(self, facade_id, name: str, payload: dict):
"""Upload the facade metadata to Hbase"""
body = {"metadata": payload}
self._req_with_retries(
"post",
"/".join([self.settings.base_url, self._uid, "facade", facade_id, "metadata", name]),
json=body,
)
url = "/".join([self.settings.base_url, self._uid, "facade", facade_id, "metadata", name])
self._req_with_retries("post", url=url, json=body)
def register_error_for_facade(self, error_code: str, facade_id: str):
"""Add an error at facade level to ES doc"""
url = "/".join([self.settings.base_url, self._uid, "facade", facade_id, "errors"])
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:
"""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()
for f_index, facade in enumerate(self._building["facade"]):
for v_index, visible_facade in enumerate(facade["visible_facades"]):
......@@ -275,7 +308,11 @@ class BuildingGeneric:
self.fetch()
# ----------------- 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(
self,
method: str,
......@@ -284,7 +321,7 @@ class BuildingGeneric:
headers: Optional[dict] = None,
**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)
return res
......@@ -340,4 +377,3 @@ class BuildingGeneric:
"""
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