set -euo pipefail
API="${API:-http://localhost:${SERVICE_PORT:-8080}/api}"
RESPUESTA="$(mktemp)"
trap 'rm -f "$RESPUESTA"' EXIT
peticion() {
local metodo=$1 ruta=$2 token=${3:-} cuerpo=${4:-}
local args=(-s -o "$RESPUESTA" -w '%{http_code}' -X "$metodo" "$API$ruta")
if [[ -n $token ]]; then args+=(-H "Authorization: Bearer $token"); fi
if [[ -n $cuerpo ]]; then args+=(-H 'Content-Type: application/json' -d "$cuerpo"); fi
local estado
estado=$(curl "${args[@]}")
echo "$estado $(cat "$RESPUESTA")"
}
crear() {
local respuesta
respuesta=$(peticion POST "$1" "$2" "$3")
if [[ ${respuesta%% *} != 201 ]]; then
echo "POST $1 → $respuesta" >&2
exit 1
fi
jq -r .id <<<"${respuesta#* }"
}
{%- if auth == "local" %}
CONTRASENA="${CONTRASENA:-sembrar-2026}"
sesion() {
peticion POST /auth/registro "" "{\"email\":\"$1\",\"contrasena\":\"$CONTRASENA\",\"nombre\":\"$2\"}" >/dev/null
peticion POST /auth/login "" "{\"email\":\"$1\",\"contrasena\":\"$CONTRASENA\"}" | cut -d' ' -f2- | jq -r .token
}
conceder() {
local salida
if ! salida=$(task -s auth:conceder EMAIL="$1" ROL="$2" 2>&1); then
echo "$salida" >&2
exit 1
fi
}
{%- elif auth == "oidc" %}
sesion() {
task auth:token -- "$1"
}
{%- endif %}
{%- if auth == "local" %}
{%- elif auth == "oidc" %}
{%- else %}
{%- endif %}
echo "Nada que sembrar todavía: escribe tus filas en scripts/sembrar.sh"