services/ghg-orgbook-issuer-controller/app/credential.py
Identical blocks of code found in 2 locations. Consider refactoring.import threading, time, os, json, requestsIdentical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.from app import loggingIdentical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.USE_LOCK = os.getenv('USE_LOCK', 'True').lower() == 'true'Identical blocks of code found in 2 locations. Consider refactoring.# max 15 second wait for a credential response (prevents blocking forever)Identical blocks of code found in 2 locations. Consider refactoring.MAX_CRED_RESPONSE_TIMEOUT = int(os.getenv('MAX_CRED_RESPONSE_TIMEOUT', '120'))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.credential_lock = threading.Lock()Identical blocks of code found in 2 locations. Consider refactoring.credential_requests = {}Identical blocks of code found in 2 locations. Consider refactoring.credential_responses = {}Identical blocks of code found in 2 locations. Consider refactoring.credential_threads = {}Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def set_credential_thread_id(cred_exch_id, thread_id):Identical blocks of code found in 2 locations. Consider refactoring. start_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.acquire()Identical blocks of code found in 2 locations. Consider refactoring. try:Identical blocks of code found in 2 locations. Consider refactoring. # add 2 records so we can x-refIdentical blocks of code found in 2 locations. Consider refactoring. credential_threads[thread_id] = cred_exch_idIdentical blocks of code found in 2 locations. Consider refactoring. credential_threads[cred_exch_id] = thread_idIdentical blocks of code found in 2 locations. Consider refactoring. finally:Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.release()Identical blocks of code found in 2 locations. Consider refactoring. processing_time = time.perf_counter() - start_timeIdentical blocks of code found in 2 locations. Consider refactoring. if processing_time > 0.001:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.warning(">>> lock time = %s", str(processing_time))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def add_credential_request(cred_exch_id):Identical blocks of code found in 2 locations. Consider refactoring. start_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.acquire()Identical blocks of code found in 2 locations. Consider refactoring. try:Identical blocks of code found in 2 locations. Consider refactoring. # short circuit if we already have the responseIdentical blocks of code found in 2 locations. Consider refactoring. if cred_exch_id in credential_responses:Identical blocks of code found in 2 locations. Consider refactoring. return NoneIdentical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. result_available = threading.Event()Identical blocks of code found in 2 locations. Consider refactoring. credential_requests[cred_exch_id] = result_availableIdentical blocks of code found in 2 locations. Consider refactoring. return result_availableIdentical blocks of code found in 2 locations. Consider refactoring. finally:Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.release()Identical blocks of code found in 2 locations. Consider refactoring. processing_time = time.perf_counter() - start_timeIdentical blocks of code found in 2 locations. Consider refactoring. if processing_time > 0.001:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.warning(">>> lock time = %s", str(processing_time))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def add_credential_response(cred_exch_id, response):Identical blocks of code found in 2 locations. Consider refactoring. start_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.acquire()Identical blocks of code found in 2 locations. Consider refactoring. try:Identical blocks of code found in 2 locations. Consider refactoring. credential_responses[cred_exch_id] = responseIdentical blocks of code found in 2 locations. Consider refactoring. if cred_exch_id in credential_requests:Identical blocks of code found in 2 locations. Consider refactoring. result_available = credential_requests[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. result_available.set()Identical blocks of code found in 2 locations. Consider refactoring. del credential_requests[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. finally:Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.release()Identical blocks of code found in 2 locations. Consider refactoring. processing_time = time.perf_counter() - start_timeIdentical blocks of code found in 2 locations. Consider refactoring. if processing_time > 0.001:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.warning(">>> lock time = %s", str(processing_time))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def add_credential_problem_report(thread_id, response):Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("get problem report for thread %s %s", thread_id, str(len(credential_requests)))Identical blocks of code found in 2 locations. Consider refactoring. if thread_id in credential_threads:Identical blocks of code found in 2 locations. Consider refactoring. cred_exch_id = credential_threads[thread_id]Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error(" ... cred_exch_id is %s: %s", cred_exch_id, str(response))Identical blocks of code found in 2 locations. Consider refactoring. add_credential_response(cred_exch_id, response)Identical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("thread_id not found %s", thread_id)Identical blocks of code found in 2 locations. Consider refactoring. # hack for nowIdentical blocks of code found in 2 locations. Consider refactoring. if 1 == len(list(credential_requests.keys())):Identical blocks of code found in 2 locations. Consider refactoring. cred_exch_id = list(credential_requests.keys())[0]Identical blocks of code found in 2 locations. Consider refactoring. add_credential_response(cred_exch_id, response)Identical blocks of code found in 2 locations. Consider refactoring. elif 0 == len(list(credential_requests.keys())):Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("NO outstanding requests, can't map problem report to request :-(")Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error(credential_requests)Identical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("Too many outstanding requests, can't map problem report to request :-(")Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error(credential_requests)Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def add_credential_timeout_report(cred_exch_id, thread_id):Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("add timeout report for cred %s %s", thread_id, cred_exch_id)Identical blocks of code found in 2 locations. Consider refactoring. response = {"success": False, "result": thread_id + "::Error thread timeout"}Identical blocks of code found in 2 locations. Consider refactoring. add_credential_response(cred_exch_id, response)Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def add_credential_exception_report(cred_exch_id, exc):Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("add exception report for cred %s", cred_exch_id)Identical blocks of code found in 2 locations. Consider refactoring. response = {"success": False, "result": cred_exch_id + "::" + str(exc)}Identical blocks of code found in 2 locations. Consider refactoring. add_credential_response(cred_exch_id, response)Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.def get_credential_response(cred_exch_id):Identical blocks of code found in 2 locations. Consider refactoring. start_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.acquire()Identical blocks of code found in 2 locations. Consider refactoring. try:Identical blocks of code found in 2 locations. Consider refactoring. if cred_exch_id in credential_responses:Identical blocks of code found in 2 locations. Consider refactoring. thread_id = NoneIdentical blocks of code found in 2 locations. Consider refactoring. response = credential_responses[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. del credential_responses[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. if cred_exch_id in credential_threads:Identical blocks of code found in 2 locations. Consider refactoring. thread_id = credential_threads[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. del credential_threads[cred_exch_id]Identical blocks of code found in 2 locations. Consider refactoring. del credential_threads[thread_id]Identical blocks of code found in 2 locations. Consider refactoring. # override returned id with thread_id, if we have it (unless we have received a problem report)Identical blocks of code found in 2 locations. Consider refactoring. if not "::" in response["result"]:Identical blocks of code found in 2 locations. Consider refactoring. response["result"] = thread_idIdentical blocks of code found in 2 locations. Consider refactoring. return responseIdentical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. return NoneIdentical blocks of code found in 2 locations. Consider refactoring. finally:Identical blocks of code found in 2 locations. Consider refactoring. if USE_LOCK:Identical blocks of code found in 2 locations. Consider refactoring. credential_lock.release()Identical blocks of code found in 2 locations. Consider refactoring. processing_time = time.perf_counter() - start_timeIdentical blocks of code found in 2 locations. Consider refactoring. if processing_time > 0.001:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.warning(">>> lock time = %s", str(processing_time))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring.class SendCredentialThread(threading.Thread):Identical blocks of code found in 2 locations. Consider refactoring. def __init__(self, credential_definition_id, cred_offer, url, headers):Identical blocks of code found in 2 locations. Consider refactoring. threading.Thread.__init__(self)Identical blocks of code found in 2 locations. Consider refactoring. self.credential_definition_id = credential_definition_idIdentical blocks of code found in 2 locations. Consider refactoring. self.cred_offer = cred_offerIdentical blocks of code found in 2 locations. Consider refactoring. self.url = urlIdentical blocks of code found in 2 locations. Consider refactoring. self.headers = headersIdentical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. def run(self):Identical blocks of code found in 2 locations. Consider refactoring. start_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. method = "submit_credential.credential"Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. logging.log_timing_event("issue_credential", {}, start_time, None, False)Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.info("Sending credential offer: %s", json.dumps(self.cred_offer))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. cred_data = NoneIdentical blocks of code found in 2 locations. Consider refactoring. try:Identical blocks of code found in 2 locations. Consider refactoring. response = requests.post(Identical blocks of code found in 2 locations. Consider refactoring. self.url, json.dumps(self.cred_offer), headers=self.headersIdentical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. response.raise_for_status()Identical blocks of code found in 2 locations. Consider refactoring. cred_data = response.json()Identical blocks of code found in 2 locations. Consider refactoring. if "credential_exchange_id" in cred_data:Identical blocks of code found in 2 locations. Consider refactoring. result_available = add_credential_request(Identical blocks of code found in 2 locations. Consider refactoring. cred_data["credential_exchange_id"]Identical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. raise Exception(json.dumps(cred_data))Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. # wait for confirmation from the agent, which will include the credential exchange idIdentical blocks of code found in 2 locations. Consider refactoring. if result_available and not result_available.wait(Identical blocks of code found in 2 locations. Consider refactoring. MAX_CRED_RESPONSE_TIMEOUTIdentical blocks of code found in 2 locations. Consider refactoring. ):Identical blocks of code found in 2 locations. Consider refactoring. add_credential_timeout_report(cred_data["credential_exchange_id"], cred_data["thread_id"])Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error(Identical blocks of code found in 2 locations. Consider refactoring. "Got credential TIMEOUT: %s %s %s",Identical blocks of code found in 2 locations. Consider refactoring. cred_data["thread_id"],Identical blocks of code found in 2 locations. Consider refactoring. cred_data["credential_exchange_id"],Identical blocks of code found in 2 locations. Consider refactoring. cred_data["connection_id"],Identical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. end_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. logging.log_timing_method(method, start_time, end_time, False, Identical blocks of code found in 2 locations. Consider refactoring. data={Identical blocks of code found in 2 locations. Consider refactoring. 'thread_id':cred_data["thread_id"], Identical blocks of code found in 2 locations. Consider refactoring. 'credential_exchange_id':cred_data["credential_exchange_id"], Identical blocks of code found in 2 locations. Consider refactoring. 'Error': 'Timeout',Identical blocks of code found in 2 locations. Consider refactoring. 'elapsed_time': (end_time-start_time)Identical blocks of code found in 2 locations. Consider refactoring. }Identical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. success = FalseIdentical blocks of code found in 2 locations. Consider refactoring. outcome = "timeout"Identical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. # response was received for this cred exchange via a web hookIdentical blocks of code found in 2 locations. Consider refactoring. end_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. logging.log_timing_method(method, start_time, end_time, True)Identical blocks of code found in 2 locations. Consider refactoring. success = TrueIdentical blocks of code found in 2 locations. Consider refactoring. outcome = "success"Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. # there should be some form of response availableIdentical blocks of code found in 2 locations. Consider refactoring. self.cred_response = get_credential_response(Identical blocks of code found in 2 locations. Consider refactoring. cred_data["credential_exchange_id"]Identical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. except Exception as exc:Identical blocks of code found in 2 locations. Consider refactoring. logging.LOGGER.error("got credential exception: %s", str(exc))Identical blocks of code found in 2 locations. Consider refactoring. # if cred_data is not set we don't have a credential to set status forIdentical blocks of code found in 2 locations. Consider refactoring. end_time = time.perf_counter()Identical blocks of code found in 2 locations. Consider refactoring. success = FalseIdentical blocks of code found in 2 locations. Consider refactoring. outcome = str(exc)Identical blocks of code found in 2 locations. Consider refactoring. if cred_data:Identical blocks of code found in 2 locations. Consider refactoring. add_credential_exception_report(Identical blocks of code found in 2 locations. Consider refactoring. cred_data["credential_exchange_id"], excIdentical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. data = {Identical blocks of code found in 2 locations. Consider refactoring. "thread_id": cred_data["thread_id"],Identical blocks of code found in 2 locations. Consider refactoring. "credential_exchange_id": cred_data["credential_exchange_id"],Identical blocks of code found in 2 locations. Consider refactoring. "Error": str(exc),Identical blocks of code found in 2 locations. Consider refactoring. "elapsed_time": (end_time - start_time),Identical blocks of code found in 2 locations. Consider refactoring. }Identical blocks of code found in 2 locations. Consider refactoring. else:Identical blocks of code found in 2 locations. Consider refactoring. data = {Identical blocks of code found in 2 locations. Consider refactoring. "Error": str(exc),Identical blocks of code found in 2 locations. Consider refactoring. "elapsed_time": (end_time - start_time)Identical blocks of code found in 2 locations. Consider refactoring. }Identical blocks of code found in 2 locations. Consider refactoring. logging.log_timing_method(method, start_time, end_time, False,Identical blocks of code found in 2 locations. Consider refactoring. data=dataIdentical blocks of code found in 2 locations. Consider refactoring. )Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. # don't re-raise; we want to log the exception as the credential error responseIdentical blocks of code found in 2 locations. Consider refactoring. self.cred_response = {"success": False, "result": str(exc)}Identical blocks of code found in 2 locations. Consider refactoring. Identical blocks of code found in 2 locations. Consider refactoring. processing_time = end_time - start_timeIdentical blocks of code found in 2 locations. Consider refactoring. message = {"thread_id": self.cred_response["result"]}Identical blocks of code found in 2 locations. Consider refactoring. logging.log_timing_event("issue_credential", message, start_time, end_time, success, outcome=outcome)