import logging import os import boto3 import requests from pandas.io import json from requests_aws4auth import AWS4Auth from service.conf import settings logger = logging.getLogger(__name__) REGION = os.getenv("AWS_REGION", "eu-west-1") API_URL_DEVEL = ( f"https://rqdq6pkcijbrpdqi5egplq4rvu.appsync-api.{REGION}.amazonaws.com/graphql" ) API_URL_TEST = ( f"https://bnpdfzkrn5edra4qf4wqec3qcq.appsync-api.{REGION}.amazonaws.com/graphql" ) API_URL_LIVE = ( f"https://mm43jchdrjatljpoe3hjvotfjq.appsync-api.{REGION}.amazonaws.com/graphql" ) profile_api_map = { "devel": API_URL_DEVEL, "test": API_URL_TEST, "live": API_URL_LIVE, "local": "", } APPSYNC_URL = ( profile_api_map[settings.PROFILE] if settings.PROFILE in profile_api_map else profile_api_map["devel"] ) if not APPSYNC_URL: logger.warning( f"Appcync subscription notifications disabled for PROFILE='{settings.PROFILE}'" ) def appsync_query_iam(query, variables=None): if APPSYNC_URL: method = "POST" headers = {} body_json = {"query": query} if variables is not None: body_json["variables"] = variables session = boto3.session.Session() c = session.get_credentials() body = json.dumps(body_json) auth = AWS4Auth( c.access_key, c.secret_key, REGION, "appsync", session_token=c.token ) response = requests.request( method, APPSYNC_URL, auth=auth, data=body, headers=headers ) return response else: return None