58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
"""
|
|
api.mebboat.it/marine/datasets/*
|
|
"""
|
|
import os
|
|
from typing import Optional
|
|
|
|
import httpx
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from middleware.auth import require_auth
|
|
|
|
router = APIRouter(prefix="/datasets", tags=["datasets"])
|
|
|
|
API_URL = os.getenv("API_SERVICE_URL", "http://api-service:3003")
|
|
|
|
|
|
def _auth_headers(user: dict) -> dict:
|
|
return {"Authorization": f"Bearer {user['token']}"}
|
|
|
|
|
|
@router.get("")
|
|
async def list_datasets(
|
|
tags: Optional[str] = Query(None),
|
|
user=Depends(require_auth)
|
|
):
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
|
r = await client.get(
|
|
f"{API_URL}/marine/datasets",
|
|
params={"tags": tags} if tags else {},
|
|
headers=_auth_headers(user),
|
|
)
|
|
if not r.is_success:
|
|
raise HTTPException(status_code=r.status_code, detail=r.json().get("error", "Upstream error"))
|
|
return r.json()
|
|
|
|
|
|
@router.get("/{dataset_id}/download")
|
|
async def download_dataset(dataset_id: str, user=Depends(require_auth)):
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
|
r = await client.get(
|
|
f"{API_URL}/marine/datasets/{dataset_id}/download",
|
|
headers=_auth_headers(user),
|
|
)
|
|
if not r.is_success:
|
|
raise HTTPException(status_code=r.status_code, detail=r.json().get("error", "Upstream error"))
|
|
return r.json()
|
|
|
|
|
|
@router.delete("/{dataset_id}")
|
|
async def delete_dataset(dataset_id: str, user=Depends(require_auth)):
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
|
r = await client.delete(
|
|
f"{API_URL}/marine/datasets/{dataset_id}",
|
|
headers=_auth_headers(user),
|
|
)
|
|
if not r.is_success:
|
|
raise HTTPException(status_code=r.status_code, detail=r.json().get("error", "Upstream error"))
|
|
return r.json()
|