import requests
import argparse


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("INPUT_URL", help="the edg instance host")
    parser.add_argument("ASSET_COLLECTION", help="the asset collection to query")
    parser.add_argument("QUERY", help="the query string")
    args = parser.parse_args()

    graphql_query(args.INPUT_URL, args.ASSET_COLLECTION, args.QUERY)


def graphql_query(input_url, asset_collection, query):
    url = f"{input_url}/tbl/graphql/{asset_collection}"
    headers = {
        "accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded",
    }
    payload = {"query": query, "variables": "{}"}

    response = requests.post(url, headers=headers, data=payload)

    if response.status_code == 200:
        return response.json()
    else:
        error_message = (
            f"Request failed with status code {response.status_code}: {response.text}"
        )
        raise Exception(error_message)


if __name__ == "__main__":
    main()
