import requests


def test_server(n=3):
    """
    Checks server status at datagovin.
    Returns list of n working-apis if server is functional.
    """

    server_response = {}
    working_api_url = "https://api.data.gov.in/lists?format=json&notfilters[source]=visualize.data.gov.in&filters[active]=1&offset=0&sort[updated]=desc&limit={}".format(
        n
    )
    working_api_response = requests.get(working_api_url, timeout=30)
    working_api_content = working_api_response.json()

    if working_api_content["status"] == "ok":
        records = working_api_content["records"]
        working_apis = [record.get("index_name", "") for record in records]
        working_apis = [w for w in working_apis if len(w) > 0]
        server_response["working_apis"] = working_apis
        server_response["status"] = True
    else:
        server_response["working_apis"] = []
        server_response["status"] = False
    return server_response