Commit 9808adcf authored by Luciano Cauzzi's avatar Luciano Cauzzi

Added existence check function

parent 044f7d31
Pipeline #62 failed with stages
...@@ -33,7 +33,7 @@ class AppPyramidManager: ...@@ -33,7 +33,7 @@ class AppPyramidManager:
self.__app_token__ = app_token_from_dict(json_repr) self.__app_token__ = app_token_from_dict(json_repr)
return self.__app_token__ return self.__app_token__
except Exception as e: except Exception as e:
logger.error("\n[NETWORK]: Error while fetching police report list: {} \n".format(e)) logger.error("Error while authenticating into pyramid {} \n".format(e))
return None return None
def save(self, doc) -> bool: def save(self, doc) -> bool:
...@@ -55,7 +55,36 @@ class AppPyramidManager: ...@@ -55,7 +55,36 @@ class AppPyramidManager:
if response.status_code != 200: if response.status_code != 200:
return False return False
except Exception as e: except Exception as e:
logger.error("Error while uploading injunctions to server") logger.error("Error while uploading document to pyramid")
logger.exception(e) logger.exception(e)
return False return False
return True return True
def exists_injunction(self, inj) -> Optional[str]:
try:
url = "{}/{}/_search".format(self.__base_url__, self.__res_url__)
headers = {
'AUTHORIZATION': self.__app_token__.token,
'Content-Type': "application/json"
}
payload = "{\n\t\"_source\": {\n " \
"\"includes\": \"*\",\n " \
"\"excludes\": \"attachments\"\n " \
"},\n \"query\": {\n \"terms\": {\n \"numero_cronologico\": [ \"" \
+ inj.progressive_number + "\"] \n }\n }\n}"
s = self.__network_manager__.get_session()
response = s.get(url, data=payload, headers=headers, timeout=AppPyramidManager.TIMEOUT)
json_obj = json.loads(response.text)
attachments = json_obj["hits"]["hits"]
if len(attachments) > 0:
doc_id = json_obj["hits"]["hits"][0]["_id"]
return doc_id
else:
return None
except Exception as e:
logger.error("Error while checking document's existence")
logger.exception(e)
return None
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