import requests
import tempfile


def get_dtr_ca(server):
    """
    Retrieves the CA cert for DTR and saves it to a named temporary file,
    returning the filename. This function will close the file before returning
    to ensure Notary can open it when invoked.
    """
    dtr_ca_url = server + "/ca"
    print("Getting DTR ca cert from URL:", dtr_ca_url)
    resp = requests.get(dtr_ca_url, verify=False)
    if resp.status_code != 200:
        raise Exception("Could not retrieve DTR CA")
    print("Found cert:")
    print(resp.content)
    f = tempfile.NamedTemporaryFile(delete=False)
    f.write(resp.content)
    f.close()
    return f.name