-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
46 lines (40 loc) · 1.36 KB
/
Copy pathapi.py
File metadata and controls
46 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
import scrape
import logging
logger = logging.getLogger("uvicorn.error")
app = FastAPI(
title="Broxtowe Bin Collection API",
description="API for retrieving bin collection data from Broxtowe Borough Council",
version="1.0.0"
)
class BinData(BaseModel):
type: str
next_collection_raw: str
next_collection_iso: str
class Address(BaseModel):
uprn: str
address: str
class BinResponse(BaseModel):
bin_collections: Optional[List[BinData]]
address: Address
@app.get("/bins", response_model=BinResponse)
async def get_bins(postcode: str, uprn: str):
try:
return scrape.get_bin_data(postcode, uprn)
except scrape.ClientError as e:
logging.exception(e)
raise HTTPException(status_code=400, detail=str(e))
except scrape.UpstreamError as e:
logging.exception(e)
raise HTTPException(status_code=503, detail=str(e))
except scrape.ServiceUnavailableError as e:
logging.exception(e)
raise HTTPException(status_code=503, detail=str(e))
except scrape.InvalidResponseError as e:
logging.exception(e)
raise HTTPException(status_code=503, detail=str(e))
except Exception as e:
logging.exception(e)
raise HTTPException(status_code=500, detail=str(e))