import logging from service.async_task_manager import io_task from .appsync_query import appsync_query_iam logger = logging.getLogger(__name__) 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 } } }""" 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 @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: logger.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: logger.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: logger.exception(e) raise e