import requests
from requests.auth import HTTPBasicAuth
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
HOST = ''
# in case of basic auth set to HTTPBasicAuth('user', 'password')
AUTH = None
# API TOKEN
TOKEN = ""
q = {
'datalayer_key': 'dhs_media_male',
# Optional filters to narrow down the returned data:
#'shape_id': '',
#'shape_type': '',
#'start_date': '',
#'end_date': '',
}
response = requests.get(
f"{HOST}/api/datalayers/data/?{urlencode(q)}",
headers={"Authorization": f"Bearer {TOKEN}"},
auth=AUTH,
)
if response.status_code == 200:
d = response.json()
else:
raise Exception(f"Request failed ({response.status_code}): {response.text}")
dhs_media_male_df = pd.DataFrame(d['data'])
dhs_media_male_df['year'] = pd.to_datetime(dhs_media_male_df['year'], format="%Y")
There is no ready use snippet for API usage in R yet, though you can adapt the Python example to your R needs.