Add GET endpoint for /countries
This commit is contained in:
parent
1a2d0e3fcb
commit
e61be20c8e
@ -1,21 +1,18 @@
|
|||||||
# Entity: Country
|
# Entity: Country
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
from typing import List
|
from typing import List
|
||||||
import httpx
|
from core.database import get_db
|
||||||
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", status_code=200, response_model=List[CountrySchema])
|
||||||
async def get_countries():
|
async def get_countries(
|
||||||
"""Get list of countries from external API"""
|
db: Session = Depends(get_db)
|
||||||
async with httpx.AsyncClient() as client:
|
):
|
||||||
response = await client.get("https://restcountries.com/v3.1/all")
|
"""Get all countries"""
|
||||||
if response.status_code != 200:
|
countries = db.query(Country).all()
|
||||||
raise HTTPException(
|
return countries
|
||||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
||||||
detail="Unable to fetch countries from external API"
|
|
||||||
)
|
|
||||||
return response.json()
|
|
Loading…
x
Reference in New Issue
Block a user