import requests


def request_waterinfo_timeseries(station_id, period="P3D"):
    """Request time series data from VMM Waterinfo

    Parameters
    ----------
    station_id : string
        ts_id as defined by Waterinfo/Kisters/dbase
    period : string
        define the timeframe to request data from. e.g. 1 day: P1D or 10 days: P10D,
        default to 3 days period

    Returns
    -------
    dict with the data and metadata fields from the request
    """
    returnfields = ["Timestamp", "Value", "Quality Code"]
    r = requests.get("http://download.waterinfo.be/tsmdownload/KiWIS/KiWIS", 
                    params={"datasource": 0, "type": "queryServices", 
                            "service": "kisters", 
                            "request": "getTimeseriesValues",
                            "ts_id": station_id, "format": "json", 
                            "period": period, "metadata": "true", 
                            "returnfields": ','.join(returnfields)})

    return r.json()[0]