satupeta-main/app/api/v1/routes/geonetwork_route.py
2026-01-27 09:11:58 +07:00

66 lines
1.9 KiB
Python

from fastapi import APIRouter, HTTPException, status
import httpx
router = APIRouter()
@router.get("/geonetwork-record")
async def get_geonetwork_record():
"""
Proxy endpoint untuk mengambil data dari GeoNetwork Jatim.
Melakukan request POST ke API GeoNetwork dengan query default.
"""
geonetwork_url = "https://geonetwork.jatimprov.go.id/geonetwork/srv/api/search/records/_search"
# Request body yang akan dikirim ke GeoNetwork API
request_body = {
"size": 0,
"track_total_hits": True,
"query": {
"bool": {
"must": {
"query_string": {
"query": "+isTemplate:n"
}
}
}
},
"aggs": {
"resourceType": {
"terms": {
"field": "resourceType",
"size": 10
}
}
}
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
geonetwork_url,
json=request_body,
headers={
"Content-Type": "application/json",
"Accept": "application/json"
}
)
response.raise_for_status()
return response.json()
except httpx.TimeoutException:
raise HTTPException(
status_code=status.HTTP_504_GATEWAY_TIMEOUT,
detail="Request to GeoNetwork API timed out"
)
except httpx.HTTPStatusError as e:
raise HTTPException(
status_code=e.response.status_code,
detail=f"GeoNetwork API returned error: {e.response.text}"
)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error connecting to GeoNetwork API: {str(e)}"
)