import requests
from urllib.parse import urlencode
import pandas as pd
import matplotlib.pyplot as plt
# set host, like http://localhost:8000, to where your Data Hub is running
DH_HOST = ""
# Your personal API authentication token
# ATTENTION: tokens should not be included in production code!
# I.e, use a environment variable to store the token and set it like this:
# DH_TOKEN = os.environ["DH_TOKEN"]
DH_TOKEN = ""
def dh_data(query: dict) -> pd.DataFrame:
response = requests.get(
f"{DH_HOST}/api/datalayers/data?{urlencode(query)}", headers={"X-API-Key": DH_TOKEN}
)
if response.status_code == 200:
d = response.json()
else:
raise Exception(f"Request failed ({response.status_code}): {response.text}")
df = pd.DataFrame(d["data"])
df[d["temporal_column"]] = pd.to_datetime(df[d["temporal_column"]], format="ISO8601")
return df
meteo_tmax_df = dh_data({
'datalayer_key': 'meteo_tmax',
# Optional filters to narrow down the returned data:
#'shape_id': '',
#'shape_type': '',
#'start_date': '',
#'end_date': '',
})
# ===== LOAD LIBRARIES =====
library(httr)
library(jsonlite)
# ===== CONFIGURATION =====
# set host, like http://localhost:8000, to where your Data Hub is running
DH_HOST <- ""
# Your personal API authentication token
# ATTENTION: tokens should not be added to production code!
# I.e, use a .Renviron file to store the token and set it like this:
# DH_TOKEN <- Sys.getenv("DH_TOKEN")
DH_TOKEN <- ""
# ===== MAIN FUNCTION =====
dh_data <- function(query) {
url <- paste0(DH_HOST, "/api/datalayers/data")
response <- GET(
url,
query = query,
add_headers("X-API-Key" = DH_TOKEN)
)
# Check if the request was successful (status code 200)
if (status_code(response) == 200) {
d <- fromJSON(content(response, "text", flatten = TRUE, encoding = "UTF-8"))
} else {
stop(sprintf("Request failed (%d): %s",
status_code(response),
content(response, as = "text")))
}
df <- as.data.frame(d$data)
# Convert the time column to proper date type
temporal_column <- d$temporal_column
temporal_format <- d$temporal_format
# R would fill in missing date parts with the current date.
# as.Date("2020",format="%Y") -> 2020-- and not 2020-01-01
if (temporal_column == "year") {
df[[temporal_column]] <- as.Date(paste0(as.character(df[[temporal_column]]), "-01-01"))
temporal_format <- "%Y-%m-%d"
}
df[[temporal_column]] <- as.Date(as.character(df[[temporal_column]]), format = temporal_format)
return(df)
}
# ===== EXAMPLE USAGE =====
meteo_tmax_df <- dh_data(list(
datalayer_key = "meteo_tmax"
# Optional filters:
# shape_id = "",
# shape_type = "",
# start_date = "",
# end_date = ""
))