|
| 1 | +import httpx |
1 | 2 | import logging |
| 3 | + |
2 | 4 | from datetime import datetime, timezone |
| 5 | +from typing import List |
3 | 6 |
|
4 | 7 | from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException |
5 | 8 | from httpx import HTTPStatusError |
6 | 9 | from sqlmodel import Session |
| 10 | +from starlette import status |
7 | 11 | from starlette.responses import JSONResponse |
8 | 12 |
|
9 | 13 | from auth.ses import EmailService |
|
13 | 17 | from db.setup import get_db_session |
14 | 18 | from routers.errors import RegistrationRoute |
15 | 19 | from schemas.biocommons import Auth0UserData, BiocommonsRegisterData |
16 | | -from schemas.bpa import BPARegistrationRequest |
| 20 | +from schemas.bpa import BPARegistrationRequest, OrgOut |
17 | 21 | from schemas.responses import RegistrationErrorResponse, RegistrationResponse |
18 | 22 | from schemas.service import Resource, Service |
| 23 | +from services.ckan_client import CKANClient, get_ckan_client |
19 | 24 |
|
20 | 25 | logger = logging.getLogger(__name__) |
21 | 26 |
|
@@ -142,3 +147,36 @@ def _create_bpa_user_record(auth0_user_data: Auth0UserData, session: Session) -> |
142 | 147 | session.add(bpa_membership) |
143 | 148 | session.commit() |
144 | 149 | return db_user |
| 150 | + |
| 151 | +@router.get( |
| 152 | + "/organizations/autoregister", |
| 153 | + response_model=List[OrgOut], |
| 154 | + summary="List CKAN organizations eligible for auto-registration", |
| 155 | +) |
| 156 | +def list_autoregister_organizations( |
| 157 | + ckan: CKANClient = Depends(get_ckan_client), |
| 158 | +) -> List[OrgOut]: |
| 159 | + """ |
| 160 | + Returns the minimal set of CKAN organizations eligible for auto-registration, |
| 161 | + suitable for populating the portal dropdown. |
| 162 | + """ |
| 163 | + try: |
| 164 | + return ckan.get_autoregister_organizations() |
| 165 | + except httpx.HTTPError as e: |
| 166 | + # Network / HTTP errors talking to CKAN |
| 167 | + raise HTTPException( |
| 168 | + status_code=status.HTTP_502_BAD_GATEWAY, |
| 169 | + detail=f"Upstream CKAN error: {str(e)}", |
| 170 | + ) |
| 171 | + except ValueError as e: |
| 172 | + # CKAN Action API returned success=false |
| 173 | + raise HTTPException( |
| 174 | + status_code=status.HTTP_502_BAD_GATEWAY, |
| 175 | + detail=str(e), |
| 176 | + ) |
| 177 | + except Exception: |
| 178 | + # Unknown error |
| 179 | + raise HTTPException( |
| 180 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
| 181 | + detail="Unexpected server error.", |
| 182 | + ) |
0 commit comments