MTES-MCT / qualicharge

Améliorer la qualité globale du service de recharge pour véhicules électriques en analysant les données de supervision.
https://beta.gouv.fr/startups/qualicharge.html
MIT License
2 stars 1 forks source link

Add IRVE static schema and API endpoints #19

Closed jmaupetit closed 5 months ago

jmaupetit commented 5 months ago

Purpose

We need to support the official IRVE statique data schema [1] to create and fetch statique entries.

Proposal

References

  1. https://schema.data.gouv.fr/etalab/schema-irve-statique/2.3.1/documentation.html
jmaupetit commented 5 months ago

Here is an example client implementation used to test the official dataset:

#!/usr/bin/env bash

set -eo pipefail

# Command wrappers
declare CSVGREP="csvgrep"
declare CSVJSON="csvjson"
declare HTTP="http"
declare JQ="jq"

# Authentication
declare OIDC_TOKEN_URL="http://localhost:8080/realms/qualicharge/protocol/openid-connect/token"
declare OIDC_CLIENT_ID="api"
declare OIDC_CLIENT_SECRET="super-secret"
declare OIDC_USERNAME="johndoe"
declare OIDC_PASSWORD="pass"

# QualiCharge
declare QUALICHARGE_API_BASE_URL="http://localhost:8010/api/v1"
declare QUALICHARGE_STATIQUE_CSV="data/irve-statique.csv"

# Get OIDC access token
function get_token() {

  "${HTTP}" --form POST \
    "${OIDC_TOKEN_URL}" \
    client_id="${OIDC_CLIENT_ID}" \
    client_secret="${OIDC_CLIENT_SECRET}" \
    username="${OIDC_USERNAME}" \
    password="${OIDC_PASSWORD}" \
    grant_type="password" | \
      "${JQ}" -r .access_token
}

# Test /whoami endpoint
function whoami() {

  local token
  local endpoint="/auth/whoami"

  token="$(get_token)"

  "${HTTP}" \
    "${QUALICHARGE_API_BASE_URL}${endpoint}" \
    Authorization:" Bearer ${token}"
}

# Create statique entries
function statique_bulk() {

  local -i n=${1:-100}
  local token
  local endpoint="/statique/bulk"
  local pattern="Bouygues Energies et Services"
  # local pattern="TotalEnergies"
  local column="nom_operateur"

  # We should add the header row
  n=$((n+1))

  token="$(get_token)"

  ${CSVGREP} -m "${pattern}" -c ${column} ${QUALICHARGE_STATIQUE_CSV} | \
  head -n ${n} | \
    ${CSVJSON} -I | \
    "${HTTP}" \
      "${QUALICHARGE_API_BASE_URL}${endpoint}" \
      Authorization:" Bearer ${token}"
}

# Execute sub-command
"$@"