import requests


def getJsons(game_number, 
             codes, 
             api_version = "0.1", 
             url = "https://np.ironhelmet.com/api"):
    
    """
    Returns a list of values collected via the API
    
    :param str game_number: Number of the game you wish to visualize data from
    :param list codes: Codes of players you wish to visualize data from
    :param str api_version: 0.1 is the current version
    :param str url: API url, should stay the same and can be left empty
    """
    
    jsons = []
    
    #No codes, abort
    if len(codes) == 0:
        print("No codes have been provided")
        return False
    
    #Fetch data from the API via a loop on the codes we have
    for i in codes:
        keys = {"api_version": api_version, "game_number": game_number, "code": i}
        req = requests.post(url, data = keys)
        temp = req.json()
        
        #Errors from the api are codified, if there is an error, we raise it but continue to loop on the codes
        if 'error' in temp:
            if temp["error"] == "code not found in game":
                print("code " + i + " not found in game " + game_number)
            else:
                print(temp['error'])
        else:
            jsons.append(temp)
    
    #No data, abort
    if len(jsons) == 0:
        print('No data has been fetched')
        return False
    
    print("Raw Data fecthed")
    return jsons