FHIR: every URL the API emits hangs from one base, set by FHIR_BASE_URL

The code systems, value sets and the extensions (cut-offs, report id, interval, quartiles) each wrote the domain by
hand. They now derive from webapi.fhir_base, a leaf module so metrics and durations can use it without a cycle. The
default is unchanged; the WebApp still reads the same URLs by exact match, so changing the domain goes to both.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 02:54:23 -06:00
parent 6d01742a85
commit 1d52da79e4
5 changed files with 64 additions and 9 deletions
+39
View File
@@ -0,0 +1,39 @@
"""Every FHIR URL the API emits hangs from one base, which FHIR_BASE_URL sets (issue 199)."""
import importlib
import sys
from webapi import durations, fhir_base, metrics
from webapi.fhir import common
POR_DEFECTO = "https://isssteleon.org/fhir"
def test_todas_las_urls_cuelgan_de_la_misma_base():
estructuras = [metrics.EVENT_CUTOFF, metrics.KNOWLEDGE_CUTOFF, metrics.HISTORY_ENTRY, metrics.INTERVAL_LOW, metrics.INTERVAL_HIGH]
assert all(u.startswith(f"{common.STRUCTURE}/") for u in estructuras)
assert common.STRUCTURE == f"{common.BASE}/StructureDefinition" == fhir_base.STRUCTURE
assert common.CODE_SYSTEM.startswith(common.BASE) and common.VALUE_SET.startswith(common.BASE)
def test_no_quedan_dominios_escritos_a_mano_fuera_de_la_base():
import pathlib
raiz = pathlib.Path(fhir_base.__file__).parent
escritos = [p.name for p in raiz.rglob("*.py") if "isssteleon.org" in p.read_text() and p.name != "fhir_base.py"]
assert escritos == []
def test_la_base_se_puede_cambiar_por_ambiente(monkeypatch):
monkeypatch.setenv("FHIR_BASE_URL", "https://fhir.ejemplo.test/base/")
try:
importlib.reload(fhir_base)
assert fhir_base.BASE == "https://fhir.ejemplo.test/base"
assert fhir_base.STRUCTURE == "https://fhir.ejemplo.test/base/StructureDefinition"
finally:
monkeypatch.delenv("FHIR_BASE_URL")
importlib.reload(fhir_base)
assert fhir_base.BASE == POR_DEFECTO
def test_los_q1_y_q3_de_los_tiempos_usan_la_misma_base():
assert durations.STRUCTURE == metrics.INTERVAL_LOW.rsplit("/", 1)[0]
+3 -2
View File
@@ -20,6 +20,7 @@ from pathlib import Path
from sqlalchemy import Integer, Select, String, and_, case, cast, column, func, literal, not_, null, select, table, true
from webapi.fhir_base import STRUCTURE
from webapi.metrics import (
AGGREGATION_VERSION,
EVENT_CUTOFF,
@@ -278,9 +279,9 @@ def duration_report(definition: DurationDefinition, question: Question, result:
]}
if result.median is not None:
group["measureScore"] = {"value": result.median, "unit": "d",
"extension": [{"url": "https://isssteleon.org/fhir/StructureDefinition/q1",
"extension": [{"url": f"{STRUCTURE}/q1",
"valueDecimal": result.q1},
{"url": "https://isssteleon.org/fhir/StructureDefinition/q3",
{"url": f"{STRUCTURE}/q3",
"valueDecimal": result.q3}]}
extensions = [{"url": HISTORY_ENTRY, "valueInteger": result.report_id},
{"url": EVENT_CUTOFF, "valueDate": (result.event_cutoff or question.event_cutoff).isoformat()}]
+4 -2
View File
@@ -9,12 +9,14 @@ from urllib.parse import urlencode
from flask import Response, request
from webapi.fhir_base import BASE as _BASE, STRUCTURE as _STRUCTURE
FHIR_VERSION = "4.0.1"
MEDIA_TYPE = "application/fhir+json"
BASE = "https://isssteleon.org/fhir"
BASE = _BASE
CODE_SYSTEM = f"{BASE}/CodeSystem"
VALUE_SET = f"{BASE}/ValueSet"
STRUCTURE = f"{BASE}/StructureDefinition"
STRUCTURE = _STRUCTURE
SIMULATED = {"system": f"{CODE_SYSTEM}/etiqueta", "code": "simulado", "display": "Datos simulados"}
MAX_COUNT = 200
DEFAULT_COUNT = 50
+12
View File
@@ -0,0 +1,12 @@
"""The base of every FHIR URL this API emits: code systems, value sets and extensions.
It is a name we mint, not an address that has to resolve. `FHIR_BASE_URL` sets it. The WebApp reads these URLs by
exact match (`fhir.ts` CODE_SYSTEM and `paciente-riesgo.validation.ts` INTERVAL), so a change of domain goes to both.
A module of its own, with no imports from the package, so `metrics` and `durations` can use it without a cycle.
"""
import os
BASE = os.environ.get("FHIR_BASE_URL", "https://isssteleon.org/fhir").rstrip("/")
STRUCTURE = f"{BASE}/StructureDefinition"
+6 -5
View File
@@ -42,6 +42,7 @@ from sqlalchemy import (
)
from webapi.cama_analysis.stats import wilson
from webapi.fhir_base import STRUCTURE
AGGREGATION_VERSION = "md19.1" # the unit-row contract the publication views read
TOTAL = "total"
@@ -665,11 +666,11 @@ def _from_counts(definition: MeasureDefinition, report_id: int, counts: dict, by
# --- the contract --------------------------------------------------------------------------------------------------
POPULATION_SYSTEM = "http://terminology.hl7.org/CodeSystem/measure-population"
EVENT_CUTOFF = "https://isssteleon.org/fhir/StructureDefinition/corte-evento"
KNOWLEDGE_CUTOFF = "https://isssteleon.org/fhir/StructureDefinition/corte-conocimiento"
HISTORY_ENTRY = "https://isssteleon.org/fhir/StructureDefinition/reporte-id"
INTERVAL_LOW = "https://isssteleon.org/fhir/StructureDefinition/ic-bajo"
INTERVAL_HIGH = "https://isssteleon.org/fhir/StructureDefinition/ic-alto"
EVENT_CUTOFF = f"{STRUCTURE}/corte-evento"
KNOWLEDGE_CUTOFF = f"{STRUCTURE}/corte-conocimiento"
HISTORY_ENTRY = f"{STRUCTURE}/reporte-id"
INTERVAL_LOW = f"{STRUCTURE}/ic-bajo"
INTERVAL_HIGH = f"{STRUCTURE}/ic-alto"
def _populations(definition: MeasureDefinition, counts: Counts) -> list[dict]: