from .appsync_query import appsync_query_iam from utils.async_task_manager import io_task import logging log = logging.getLogger().getChild("utils.appsync_communication") QUERY_send_collection = """mutation updateCollection($channelId:ID!, $collectionId: ID!, $collection:CollectionInput!) { sendCollection(channelId:$channelId, collectionId:$collectionId, collection:$collection) { channelId collectionId collection { id name status parentId dateCreated type totalProfiles } } }""" QUERY_send_alliance = """mutation updateAlliance($channelId:ID!, $alliance:AllianceInput!) { sendAlliance(channelId:$channelId, alliance:$alliance) { channelId alliance { id status } } }""" QUERY_send_process_status = """ mutation sendProcessCurrentStatus($channelId: ID!, $collectionId: ID!, $processStatus: ProcessStatusInput!) { sendProcessStatus(channelId:$channelId, collectionId:$collectionId, processStatus:$processStatus) { channelId collectionId processStatus { status tasksCompleted tasksTotal estimatedTimeRemaining } } } """ def send_collection(schema_id, collection): task = send_collection_async(schema_id, collection.copy()) task.wait_for_immediate_response() return task def send_alliance(schema_id, alliance): task = send_alliance_async(schema_id, alliance) task.wait_for_immediate_response() return task def send_process_status(schema_id, collection_id, process_status): task = send_process_status_async(schema_id, collection_id, process_status) task.wait_for_immediate_response() return task @io_task def send_collection_async(schema_id, collection, task_handle=None, **kwargs): try: task_handle.send_immediate_response(True) collection['id'] = f"{schema_id}-{collection['id']}" if 'parentId' in collection and collection['parentId']: collection['parentId'] = f"{schema_id}-{collection['parentId']}" variables = {"channelId": schema_id, "collectionId": collection['id'], "collection": collection } response = appsync_query_iam(QUERY_send_collection, variables) if response: if response.ok: return response.content else: raise RuntimeError(f"sendCollection failed[{response.status_code}]") except Exception as e: log.exception(e) raise e @io_task def send_alliance_async(schema_id, alliance, task_handle=None, **kwargs): try: task_handle.send_immediate_response(True) variables = {"channelId": schema_id, "alliance": alliance } response = appsync_query_iam(QUERY_send_alliance, variables) if response: if response.ok: log.info(f"sendAlliance: response {response.status_code} / {alliance['id']} / {alliance['status']}") return response.content else: raise RuntimeError(f"sendAlliance failed[{response.status_code}]") except Exception as e: log.exception(e) raise e @io_task def send_process_status_async(schema_id, collection_id, process_status, task_handle): try: task_handle.send_immediate_response(True) variables = { "channelId": schema_id, "collectionId": f"{schema_id}-{collection_id}", "processStatus": process_status } response = appsync_query_iam(QUERY_send_process_status, variables) if response is not None and response.ok: log.info(f"sendProcessStatus: response {response.status_code} / {schema_id} / {collection_id}") else: if response is not None: raise RuntimeError(f"sendProcessStatus failed: {response.status_code} ({response.content})") except Exception as e: log.exception(e) raise e