import aiohttp
import json


async def to_uuid(name: str):
    """Converts a username to a UUID by querying the mojang api
    
    Parameters
    ----------
    name: str
        Username you would like to convert
    
    Returns
    -------
    UUID: str
        The UUID of the player
    
    Raises
    ------
    ValueError
        Entered name is invalid
    """
    async with aiohttp.ClientSession() as session:
        async with session.get("https://api.mojang.com/users/profiles/minecraft/{}".format(name)) as resp:
            uuid_dict = json.loads(await resp.text())
            try:
                uuid = uuid_dict["id"]
                return uuid
            except KeyError:
                raise ValueError("Name {} is invalid".format(name))