Skip to content

Commit

Permalink
Fix error handling and remove old function
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyMcCormick committed May 9, 2024
1 parent 8fda295 commit 96a6ef8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/jeremymfastapiexample/handlers/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ async def get_greeting() -> str:
return "Hello, SQuaRE Services Bootcamp!"


async def fetch_data(schema_url: str) -> str:
async with httpx.AsyncClient() as client:
try:
response = await client.get(schema_url)
except httpx.RequestError as e:
raise HTTPException(
status_code=200, detail=f"URL not found\n{schema_url}"
) from e
return response.text


@external_router.get(
"/schema",
summary="Get an SDM schema by name.",
Expand All @@ -88,9 +77,20 @@ async def get_schema(
logger.info("Request for SDM schema", schema_url=schema_url)
try:
response = await http_client.get(schema_url)
if response.status_code == 404:
raise HTTPException(
status_code=404, detail=f"URL not found: {schema_url}"
)
if response.status_code != 200:
raise HTTPException(
status_code=500,
detail="Github returned an error while fetching schema"
f" {name}",
)
except httpx.RequestError as e:
raise HTTPException(
status_code=404, detail=f"URL not found\n{schema_url}"
status_code=500,
detail=f"Unknown error occurred while fetching schema\n{name}",
) from e
data: dict[str, Any] = yaml.safe_load(response.text)
return Schema(**data)

0 comments on commit 96a6ef8

Please sign in to comment.