Add GET endpoint for /countries

This commit is contained in:
Backend IM Bot 2025-03-26 13:01:22 +00:00
parent 61445abf30
commit 25f265fa10

View File

@ -1,18 +1,21 @@
# Entity: Country # Entity: Country
from fastapi import APIRouter, Depends, HTTPException, status from fastapi import APIRouter, HTTPException, status
from sqlalchemy.orm import Session
from typing import List from typing import List
from core.database import get_db import httpx
from core.models.country import Country from core.models.country import Country
from core.schemas.country import CountrySchema from core.schemas.country import CountrySchema
router = APIRouter() router = APIRouter()
@router.get("/countries", status_code=200, response_model=List[CountrySchema]) @router.get("/countries", response_model=List[CountrySchema], status_code=status.HTTP_200_OK)
async def get_countries( async def get_countries():
db: Session = Depends(get_db) """Get list of countries from external API"""
): async with httpx.AsyncClient() as client:
"""Get all countries""" response = await client.get("https://restcountries.com/v3.1/all")
countries = db.query(Country).all() if response.status_code != 200:
return countries raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="External API service unavailable"
)
return response.json()