import uuid
from srht.config import get_api
from srht.graphql import InternalAuth
from srht.rid import to_rid
from srht.services.hub import HubClient
from srht.services.hub.exceptions import GraphQLClientHttpError

def get_projects(owner, rid):
    """
    Given a resource (e.g. a git repo) and its owner, fetch the project(s) its
    associated with and details about its sibling resources (e.g. associated
    mailing lists). Returns an empty list if the hub is disabled or there is no
    project associated with the resource.
    """
    origin = get_api("hub.sr.ht", default=None)
    if not origin:
        return []
    auth = InternalAuth(owner)
    client = HubClient(auth)
    if isinstance(rid, uuid.UUID):
        rid = to_rid(rid)
    try:
        resource = client.get_resource_projects(rid).resource
    except:
        return [] # If the project hub is down, degrade gracefully
    if resource is None:
        return []
    return resource.projects.results
