@dataif/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/bin/dataif.js +623 -0
- package/package.json +26 -0
- package/scripts/build-template.mjs +72 -0
- package/templates/dataif/README.md +157 -0
- package/templates/dataif/infra/.env.example +119 -0
- package/templates/dataif/infra/.env.stg.example +119 -0
- package/templates/dataif/infra/airflow/Dockerfile +11 -0
- package/templates/dataif/infra/airflow/Dockerfile.release +17 -0
- package/templates/dataif/infra/airflow/requirements.txt +3 -0
- package/templates/dataif/infra/docker-compose.yml +306 -0
- package/templates/dataif/infra/init-db/01-init-dataif.sh +129 -0
- package/templates/dataif/infra/init-db/pnp-curated-views.sqlinc +444 -0
- package/templates/dataif/infra/init-db/pnp-raw-staging-curated.sqlinc +701 -0
- package/templates/dataif/infra/keycloak/Dockerfile +4 -0
- package/templates/dataif/infra/keycloak/realm-dataif.json +73 -0
- package/templates/dataif/infra/ollama/Dockerfile +9 -0
- package/templates/dataif/infra/ollama/bootstrap-model.sh +100 -0
- package/templates/dataif/infra/ollama/sabia-7b.Modelfile +14 -0
- package/templates/dataif/infra/postgres/Dockerfile +4 -0
- package/templates/dataif/pipelines/airflow/dags/generated/.gitkeep +1 -0
- package/templates/dataif/pipelines/airflow/dags/generated/2020_financeiro_fcc6f1f3_sync.py +9 -0
- package/templates/dataif/pipelines/dataif_pipelines/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/airflow/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/airflow/pnp_pipeline_factory.py +167 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/connector.py +28 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/base/types.py +14 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/config.py +19 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/connector.py +558 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/powerbi_microdados.py +728 -0
- package/templates/dataif/pipelines/dataif_pipelines/connectors/nilo_pecanha/transform.py +296 -0
- package/templates/dataif/pipelines/dataif_pipelines/jobs/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/jobs/nilo_pipeline.py +112 -0
- package/templates/dataif/pipelines/dataif_pipelines/orchestration/__init__.py +21 -0
- package/templates/dataif/pipelines/dataif_pipelines/orchestration/pnp_workflow.py +783 -0
- package/templates/dataif/pipelines/dataif_pipelines/repositories/__init__.py +1 -0
- package/templates/dataif/pipelines/dataif_pipelines/repositories/pnp_raw_repository.py +860 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/__init__.py +19 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_curated_service.py +66 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_download_service.py +534 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_quality_service.py +9 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_raw_ingestion_service.py +124 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/pnp_staging_service.py +271 -0
- package/templates/dataif/pipelines/dataif_pipelines/services/powerbi_catalog_service.py +159 -0
- package/templates/dataif/pipelines/sql/staging/020_pnp_matriculas.sql +112 -0
- package/templates/dataif/pipelines/sql/staging/030_pnp_eficiencia_academica.sql +83 -0
- package/templates/dataif/pipelines/sql/staging/040_pnp_servidores.sql +90 -0
- package/templates/dataif/pipelines/sql/staging/050_pnp_financeiro.sql +72 -0
- package/templates/dataif/pipelines/sql/views_curated/004_mv_pnp_dashboard_fast.sql +204 -0
- package/templates/dataif/pipelines/sql/views_curated/010_vw_pnp_admin_ingestao.sql +51 -0
- package/templates/dataif/pipelines/sql/views_curated/020_vw_pnp_qualidade_dados.sql +114 -0
- package/templates/dataif/pipelines/sql/views_curated/030_vw_pnp_matriculas.sql +67 -0
- package/templates/dataif/pipelines/sql/views_curated/040_vw_pnp_eficiencia.sql +33 -0
- package/templates/dataif/pipelines/sql/views_curated/050_vw_pnp_servidores.sql +30 -0
- package/templates/dataif/pipelines/sql/views_curated/060_vw_pnp_financeiro.sql +22 -0
- package/templates/dataif/pipelines/sql/views_curated/070_vw_pnp_vanna.sql +115 -0
- package/templates/dataif/scripts/configure-env.sh +149 -0
- package/templates/dataif/scripts/create_metabase_pnp_dashboard.py +943 -0
- package/templates/dataif/scripts/create_metabase_pnp_matriculas_dashboard.py +580 -0
- package/templates/dataif/scripts/deploy.sh +79 -0
- package/templates/dataif/scripts/fix_metabase_template_tag_ids.py +91 -0
- package/templates/dataif/scripts/pnp_powerbi_microdados_probe.py +14 -0
- package/templates/dataif/scripts/pnp_validate_raw_run.py +330 -0
- package/templates/dataif/scripts/publish-images.sh +31 -0
- package/templates/dataif/scripts/sync_metabase_dashboard_field_filters.py +241 -0
- package/templates/dataif/scripts/use-vanna-ollama.sh +139 -0
- package/templates/dataif/services/api/.dockerignore +18 -0
- package/templates/dataif/services/api/Dockerfile +12 -0
- package/templates/dataif/services/api/app/__init__.py +1 -0
- package/templates/dataif/services/api/app/auth.py +48 -0
- package/templates/dataif/services/api/app/config.py +59 -0
- package/templates/dataif/services/api/app/keycloak_admin.py +215 -0
- package/templates/dataif/services/api/app/main.py +2432 -0
- package/templates/dataif/services/api/app/metabase_admin.py +191 -0
- package/templates/dataif/services/api/app/metabase_bootstrap.py +44 -0
- package/templates/dataif/services/api/app/metabase_embed.py +15 -0
- package/templates/dataif/services/api/app/pnp_dag_provisioner.py +113 -0
- package/templates/dataif/services/api/app/pnp_instance_repository.py +951 -0
- package/templates/dataif/services/api/app/pnp_powerbi.py +438 -0
- package/templates/dataif/services/api/app/vanna_client.py +32 -0
- package/templates/dataif/services/api/requirements.txt +9 -0
- package/templates/dataif/services/vanna/.dockerignore +18 -0
- package/templates/dataif/services/vanna/Dockerfile +12 -0
- package/templates/dataif/services/vanna/app/config.py +57 -0
- package/templates/dataif/services/vanna/app/main.py +108 -0
- package/templates/dataif/services/vanna/app/runtime_config.py +114 -0
- package/templates/dataif/services/vanna/app/sql_guard.py +123 -0
- package/templates/dataif/services/vanna/app/vanna_engine.py +382 -0
- package/templates/dataif/services/vanna/requirements.txt +8 -0
- package/templates/dataif/services/web/.dockerignore +13 -0
- package/templates/dataif/services/web/Dockerfile +16 -0
- package/templates/dataif/services/web/index.html +12 -0
- package/templates/dataif/services/web/nginx.conf +74 -0
- package/templates/dataif/services/web/package-lock.json +4397 -0
- package/templates/dataif/services/web/package.json +32 -0
- package/templates/dataif/services/web/postcss.config.mjs +5 -0
- package/templates/dataif/services/web/src/App.jsx +2817 -0
- package/templates/dataif/services/web/src/adminAuth.js +245 -0
- package/templates/dataif/services/web/src/assets/avatar_placeholder.png +0 -0
- package/templates/dataif/services/web/src/assets/github_logo_icon_229278.svg +1 -0
- package/templates/dataif/services/web/src/assets/if-logo.png +0 -0
- package/templates/dataif/services/web/src/assets/if.svg +0 -0
- package/templates/dataif/services/web/src/assets/pnp-horizontal.svg +1 -0
- package/templates/dataif/services/web/src/components/AppHeader.jsx +233 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/mobile-header.tsx +56 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-account-card.tsx +209 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-item-button.tsx +67 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-item.tsx +108 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/base-components/nav-list.tsx +83 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/config.ts +23 -0
- package/templates/dataif/services/web/src/components/application/app-navigation/header-navigation.tsx +240 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-base.tsx +376 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-dot.tsx +52 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination-line.tsx +48 -0
- package/templates/dataif/services/web/src/components/application/pagination/pagination.tsx +328 -0
- package/templates/dataif/services/web/src/components/application/tabs/tabs.tsx +223 -0
- package/templates/dataif/services/web/src/components/base/avatar/avatar-label-group.tsx +28 -0
- package/templates/dataif/services/web/src/components/base/avatar/avatar.tsx +129 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-add-button.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-company-icon.tsx +24 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/avatar-online-indicator.tsx +29 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/index.tsx +4 -0
- package/templates/dataif/services/web/src/components/base/avatar/base-components/verified-tick.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/badges/badge-types.ts +264 -0
- package/templates/dataif/services/web/src/components/base/badges/badges.tsx +415 -0
- package/templates/dataif/services/web/src/components/base/button-group/button-group.tsx +104 -0
- package/templates/dataif/services/web/src/components/base/buttons/button.tsx +267 -0
- package/templates/dataif/services/web/src/components/base/input/hint-text.tsx +31 -0
- package/templates/dataif/services/web/src/components/base/input/input.tsx +269 -0
- package/templates/dataif/services/web/src/components/base/input/label.tsx +48 -0
- package/templates/dataif/services/web/src/components/base/radio-buttons/radio-buttons.tsx +127 -0
- package/templates/dataif/services/web/src/components/base/select/combobox.tsx +150 -0
- package/templates/dataif/services/web/src/components/base/select/multi-select.tsx +361 -0
- package/templates/dataif/services/web/src/components/base/select/popover.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/select/select-item.tsx +95 -0
- package/templates/dataif/services/web/src/components/base/select/select-native.tsx +67 -0
- package/templates/dataif/services/web/src/components/base/select/select.tsx +144 -0
- package/templates/dataif/services/web/src/components/base/tags/base-components/tag-close-x.tsx +32 -0
- package/templates/dataif/services/web/src/components/base/tooltip/tooltip.tsx +107 -0
- package/templates/dataif/services/web/src/components/foundations/dot-icon.tsx +22 -0
- package/templates/dataif/services/web/src/components/foundations/logo/untitledui-logo-minimal.tsx +170 -0
- package/templates/dataif/services/web/src/components/foundations/logo/untitledui-logo.tsx +58 -0
- package/templates/dataif/services/web/src/hooks/use-breakpoint.ts +34 -0
- package/templates/dataif/services/web/src/hooks/use-resize-observer.ts +67 -0
- package/templates/dataif/services/web/src/main.jsx +14 -0
- package/templates/dataif/services/web/src/providers/theme-provider.jsx +62 -0
- package/templates/dataif/services/web/src/styles/globals.css +60 -0
- package/templates/dataif/services/web/src/styles/theme.css +1326 -0
- package/templates/dataif/services/web/src/styles/typography.css +430 -0
- package/templates/dataif/services/web/src/styles.css +1287 -0
- package/templates/dataif/services/web/src/utils/cx.ts +24 -0
- package/templates/dataif/services/web/src/utils/is-react-component.ts +33 -0
- package/templates/dataif/services/web/vite.config.js +14 -0
- package/templates/dataif/sql/ddl/001_schemas.sql +6 -0
- package/templates/dataif/sql/ddl/003_pnp_raw_staging_curated.sql +699 -0
- package/templates/dataif/sql/migrations/001_pnp_phase1_backfill.sql +3 -0
- package/templates/dataif/sql/migrations/002_pnp_phase2_admin_config_backfill.sql +184 -0
- package/templates/dataif/sql/migrations/003_pnp_phase3_raw_tabular_backfill.sql +3 -0
- package/templates/dataif/sql/migrations/004_pnp_phase3_raw_backfill_support_index.sql +3 -0
- package/templates/dataif/sql/migrations/005_pnp_phase7_staging_support_indexes.sql +2 -0
- package/templates/dataif/sql/migrations/006_pnp_phase7_staging_autovacuum_tuning.sql +2 -0
- package/templates/dataif/sql/migrations/007_pnp_phase7b_run_packages.sql +20 -0
- package/templates/dataif/sql/migrations/008_pnp_phase7a_pipeline_endpoints.sql +169 -0
- package/templates/dataif/sql/migrations/009_pnp_phase8_curated.sql +35 -0
- package/templates/dataif/sql/migrations/010_pnp_phase10_staging_incremental_upsert.sql +3 -0
- package/templates/dataif/sql/migrations/010_pnp_pipeline_uuid.sql +51 -0
- package/templates/dataif/sql/migrations/011_app_settings.sql +7 -0
- package/templates/dataif/sql/staging/020_pnp_matriculas.sql +112 -0
- package/templates/dataif/sql/staging/030_pnp_eficiencia_academica.sql +83 -0
- package/templates/dataif/sql/staging/040_pnp_servidores.sql +90 -0
- package/templates/dataif/sql/staging/050_pnp_financeiro.sql +72 -0
- package/templates/dataif/sql/views_curated/003_vw_pnp_microdados_admin.sql +160 -0
- package/templates/dataif/sql/views_curated/004_mv_pnp_dashboard_fast.sql +204 -0
- package/templates/dataif/sql/views_curated/010_vw_pnp_admin_ingestao.sql +51 -0
- package/templates/dataif/sql/views_curated/020_vw_pnp_qualidade_dados.sql +114 -0
- package/templates/dataif/sql/views_curated/030_vw_pnp_matriculas.sql +67 -0
- package/templates/dataif/sql/views_curated/040_vw_pnp_eficiencia.sql +33 -0
- package/templates/dataif/sql/views_curated/050_vw_pnp_servidores.sql +30 -0
- package/templates/dataif/sql/views_curated/060_vw_pnp_financeiro.sql +22 -0
- package/templates/dataif/sql/views_curated/070_vw_pnp_vanna.sql +115 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
from fastapi import HTTPException
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class KeycloakAdminClient:
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
*,
|
|
14
|
+
base_url: str,
|
|
15
|
+
realm: str,
|
|
16
|
+
admin_realm: str,
|
|
17
|
+
admin_client_id: str,
|
|
18
|
+
admin_username: str,
|
|
19
|
+
admin_password: str,
|
|
20
|
+
timeout_seconds: float,
|
|
21
|
+
) -> None:
|
|
22
|
+
self.base_url = base_url.rstrip("/")
|
|
23
|
+
self.realm = realm
|
|
24
|
+
self.admin_realm = admin_realm
|
|
25
|
+
self.admin_client_id = admin_client_id
|
|
26
|
+
self.admin_username = admin_username
|
|
27
|
+
self.admin_password = admin_password
|
|
28
|
+
self.timeout_seconds = timeout_seconds
|
|
29
|
+
|
|
30
|
+
def list_admin_users(self) -> list[dict[str, Any]]:
|
|
31
|
+
users = self._request(
|
|
32
|
+
"GET",
|
|
33
|
+
f"/admin/realms/{quote(self.realm, safe='')}/roles/admin/users",
|
|
34
|
+
expected_status={200},
|
|
35
|
+
)
|
|
36
|
+
if not isinstance(users, list):
|
|
37
|
+
return []
|
|
38
|
+
return [
|
|
39
|
+
{
|
|
40
|
+
"id": item.get("id"),
|
|
41
|
+
"username": item.get("username"),
|
|
42
|
+
"email": item.get("email") or "",
|
|
43
|
+
"first_name": item.get("firstName") or "",
|
|
44
|
+
"last_name": item.get("lastName") or "",
|
|
45
|
+
"enabled": bool(item.get("enabled", True)),
|
|
46
|
+
"email_verified": bool(item.get("emailVerified", False)),
|
|
47
|
+
}
|
|
48
|
+
for item in users
|
|
49
|
+
if isinstance(item, dict)
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
def get_admin_user(self, user_id: str) -> dict[str, Any] | None:
|
|
53
|
+
for item in self.list_admin_users():
|
|
54
|
+
if str(item.get("id") or "") == user_id:
|
|
55
|
+
return item
|
|
56
|
+
return None
|
|
57
|
+
|
|
58
|
+
def create_admin_user(
|
|
59
|
+
self,
|
|
60
|
+
*,
|
|
61
|
+
username: str,
|
|
62
|
+
email: str,
|
|
63
|
+
password: str,
|
|
64
|
+
first_name: str,
|
|
65
|
+
last_name: str,
|
|
66
|
+
enabled: bool,
|
|
67
|
+
) -> dict[str, Any]:
|
|
68
|
+
payload = {
|
|
69
|
+
"username": username,
|
|
70
|
+
"email": email,
|
|
71
|
+
"firstName": first_name,
|
|
72
|
+
"lastName": last_name,
|
|
73
|
+
"enabled": enabled,
|
|
74
|
+
"emailVerified": False,
|
|
75
|
+
}
|
|
76
|
+
self._request(
|
|
77
|
+
"POST",
|
|
78
|
+
f"/admin/realms/{quote(self.realm, safe='')}/users",
|
|
79
|
+
json=payload,
|
|
80
|
+
expected_status={201, 204},
|
|
81
|
+
)
|
|
82
|
+
user = self._lookup_user_by_username(username)
|
|
83
|
+
if not user or not user.get("id"):
|
|
84
|
+
raise HTTPException(status_code=502, detail="Keycloak did not return the created user")
|
|
85
|
+
|
|
86
|
+
user_id = str(user["id"])
|
|
87
|
+
self._request(
|
|
88
|
+
"PUT",
|
|
89
|
+
f"/admin/realms/{quote(self.realm, safe='')}/users/{quote(user_id, safe='')}/reset-password",
|
|
90
|
+
json={"type": "password", "temporary": False, "value": password},
|
|
91
|
+
expected_status={204},
|
|
92
|
+
)
|
|
93
|
+
self._request(
|
|
94
|
+
"POST",
|
|
95
|
+
f"/admin/realms/{quote(self.realm, safe='')}/users/{quote(user_id, safe='')}/role-mappings/realm",
|
|
96
|
+
json=[self._realm_role("admin")],
|
|
97
|
+
expected_status={204},
|
|
98
|
+
)
|
|
99
|
+
return {
|
|
100
|
+
"id": user_id,
|
|
101
|
+
"username": user.get("username") or username,
|
|
102
|
+
"email": user.get("email") or email,
|
|
103
|
+
"first_name": user.get("firstName") or first_name,
|
|
104
|
+
"last_name": user.get("lastName") or last_name,
|
|
105
|
+
"enabled": bool(user.get("enabled", enabled)),
|
|
106
|
+
"email_verified": bool(user.get("emailVerified", False)),
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
def delete_user(self, user_id: str) -> None:
|
|
110
|
+
self._request(
|
|
111
|
+
"DELETE",
|
|
112
|
+
f"/admin/realms/{quote(self.realm, safe='')}/users/{quote(user_id, safe='')}",
|
|
113
|
+
expected_status={204},
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def _lookup_user_by_username(self, username: str) -> dict[str, Any] | None:
|
|
117
|
+
items = self._request(
|
|
118
|
+
"GET",
|
|
119
|
+
f"/admin/realms/{quote(self.realm, safe='')}/users",
|
|
120
|
+
params={"username": username, "exact": "true"},
|
|
121
|
+
expected_status={200},
|
|
122
|
+
)
|
|
123
|
+
if not isinstance(items, list):
|
|
124
|
+
return None
|
|
125
|
+
for item in items:
|
|
126
|
+
if isinstance(item, dict) and item.get("username") == username:
|
|
127
|
+
return item
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
def _realm_role(self, role_name: str) -> dict[str, Any]:
|
|
131
|
+
role = self._request(
|
|
132
|
+
"GET",
|
|
133
|
+
f"/admin/realms/{quote(self.realm, safe='')}/roles/{quote(role_name, safe='')}",
|
|
134
|
+
expected_status={200},
|
|
135
|
+
)
|
|
136
|
+
if not isinstance(role, dict) or not role.get("name"):
|
|
137
|
+
raise HTTPException(status_code=502, detail=f"Keycloak role {role_name} was not returned correctly")
|
|
138
|
+
return role
|
|
139
|
+
|
|
140
|
+
def _admin_token(self) -> str:
|
|
141
|
+
token_url = f"{self.base_url}/realms/{quote(self.admin_realm, safe='')}/protocol/openid-connect/token"
|
|
142
|
+
try:
|
|
143
|
+
with httpx.Client(timeout=self.timeout_seconds, follow_redirects=True) as client:
|
|
144
|
+
response = client.post(
|
|
145
|
+
token_url,
|
|
146
|
+
data={
|
|
147
|
+
"grant_type": "password",
|
|
148
|
+
"client_id": self.admin_client_id,
|
|
149
|
+
"username": self.admin_username,
|
|
150
|
+
"password": self.admin_password,
|
|
151
|
+
},
|
|
152
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
153
|
+
)
|
|
154
|
+
except httpx.RequestError as exc:
|
|
155
|
+
raise HTTPException(status_code=502, detail=f"Keycloak admin token request failed: {exc}") from exc
|
|
156
|
+
|
|
157
|
+
if response.status_code >= 400:
|
|
158
|
+
raise HTTPException(status_code=response.status_code, detail=_error_detail(response, "Keycloak admin token request failed"))
|
|
159
|
+
|
|
160
|
+
try:
|
|
161
|
+
payload = response.json()
|
|
162
|
+
except ValueError as exc:
|
|
163
|
+
raise HTTPException(status_code=502, detail="Keycloak admin token response was not valid JSON") from exc
|
|
164
|
+
|
|
165
|
+
token = payload.get("access_token")
|
|
166
|
+
if not isinstance(token, str) or not token.strip():
|
|
167
|
+
raise HTTPException(status_code=502, detail="Keycloak admin token response did not include access_token")
|
|
168
|
+
return token
|
|
169
|
+
|
|
170
|
+
def _request(
|
|
171
|
+
self,
|
|
172
|
+
method: str,
|
|
173
|
+
path: str,
|
|
174
|
+
*,
|
|
175
|
+
json: object | None = None,
|
|
176
|
+
params: dict[str, str] | None = None,
|
|
177
|
+
expected_status: set[int],
|
|
178
|
+
) -> Any:
|
|
179
|
+
token = self._admin_token()
|
|
180
|
+
url = f"{self.base_url}{path}"
|
|
181
|
+
try:
|
|
182
|
+
with httpx.Client(timeout=self.timeout_seconds, follow_redirects=True) as client:
|
|
183
|
+
response = client.request(
|
|
184
|
+
method,
|
|
185
|
+
url,
|
|
186
|
+
json=json,
|
|
187
|
+
params=params,
|
|
188
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
189
|
+
)
|
|
190
|
+
except httpx.RequestError as exc:
|
|
191
|
+
raise HTTPException(status_code=502, detail=f"Keycloak admin request failed: {exc}") from exc
|
|
192
|
+
|
|
193
|
+
if response.status_code not in expected_status:
|
|
194
|
+
raise HTTPException(status_code=response.status_code, detail=_error_detail(response, "Keycloak admin request failed"))
|
|
195
|
+
|
|
196
|
+
if response.status_code == 204 or not response.text.strip():
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
try:
|
|
200
|
+
return response.json()
|
|
201
|
+
except ValueError:
|
|
202
|
+
return response.text
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def _error_detail(response: httpx.Response, fallback: str) -> str:
|
|
206
|
+
try:
|
|
207
|
+
payload = response.json()
|
|
208
|
+
except ValueError:
|
|
209
|
+
payload = None
|
|
210
|
+
if isinstance(payload, dict):
|
|
211
|
+
detail = payload.get("error_description") or payload.get("errorMessage") or payload.get("error") or payload.get("message")
|
|
212
|
+
if isinstance(detail, str) and detail.strip():
|
|
213
|
+
return detail
|
|
214
|
+
text = response.text.strip()
|
|
215
|
+
return text or fallback
|