import requests


def get_latest_image(registry_domain='registry.opensource.zalan.do', team='acid', artifact='spilo-9.5'):
    """
    Gets the full name of latest image for an artifact
    """
    try:
        url = 'https://{0}/teams/{1}/artifacts/{2}/tags'.format(registry_domain,
                                                                team,
                                                                artifact)
        response = requests.get(url)
        if response.ok:
            # sort the tags by creation date
            latest = None
            for entry in sorted(response.json(), key=lambda t: t['created'], reverse=True):
                tag = entry['name']
                # try to avoid snapshots if possible
                if 'SNAPSHOT' not in tag:
                    latest = tag
                    break
                latest = latest or tag
            return "{0}/{1}/{2}:{3}".format(registry_domain, team, artifact, latest)
    except Exception:
        pass
    return ""