def _get_first_endpoint(endpoints, region):
    """Find the first suitable endpoint in endpoints.

    If there is only one endpoint, return it. If there is more than
    one endpoint, return the first one with the given region. If there
    are no endpoints, or there is more than one endpoint but none of
    them match the given region, raise KeyError.

    """
    if len(endpoints) == 1:
        return endpoints[0]
    else:
        for candidate_endpoint in endpoints:
            if candidate_endpoint["region"] == region:
                return candidate_endpoint

    raise LookupError("No suitable endpoint found")