Add get endpoint for states
This commit is contained in:
parent
1a63e5aaf2
commit
0edebf0738
@ -1,21 +1,18 @@
|
|||||||
# Entity: Country
|
# Entity: State
|
||||||
|
|
||||||
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
|
||||||
from core.models.country import Country
|
from core.database import get_db
|
||||||
from core.schemas.country import CountrySchema
|
from core.models.state import State
|
||||||
import httpx
|
from core.schemas.state import StateSchema
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/states", response_model=List[CountrySchema], status_code=status.HTTP_200_OK)
|
@router.get("/states", response_model=List[StateSchema], status_code=200)
|
||||||
async def get_countries():
|
async def get_states(
|
||||||
"""Get list of countries from third party API"""
|
db: Session = Depends(get_db)
|
||||||
async with httpx.AsyncClient() as client:
|
):
|
||||||
response = await client.get("https://restcountries.com/v3.1/all")
|
"""Get all states"""
|
||||||
if response.status_code != 200:
|
states = db.query(State).all()
|
||||||
raise HTTPException(
|
return states
|
||||||
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