Skip to content

Commit d7c799d

Browse files
committed
Add legacy API
1 parent 8318245 commit d7c799d

File tree

19 files changed

+202
-60
lines changed

19 files changed

+202
-60
lines changed

findthatpostcode/blueprints/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import FastAPI, Request
1+
from fastapi import APIRouter, FastAPI, Request
22
from fastapi.middleware.cors import CORSMiddleware
33

44
from findthatpostcode.blueprints import (
@@ -65,3 +65,13 @@ def about(request: Request):
6565
app.include_router(reconcile.bp)
6666
app.include_router(search.bp)
6767
app.include_router(tools.bp)
68+
69+
70+
api = APIRouter(tags=["Legacy"])
71+
72+
api.include_router(areas.api)
73+
api.include_router(areatypes.api)
74+
api.include_router(places.api)
75+
api.include_router(points.api)
76+
api.include_router(postcodes.api)
77+
api.include_router(reconcile.api)

findthatpostcode/blueprints/areas.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
bp = APIRouter(prefix="/areas")
1515

16+
api = APIRouter(prefix="/areas")
17+
1618

1719
@bp.get("/search")
1820
@bp.get("/search.<filetype>")
@@ -52,6 +54,7 @@ def areas_csv(areas: Iterator[dict], filename: str) -> Response:
5254

5355

5456
@bp.get("/{areacodes}.geojson")
57+
@api.get("/{areacodes}.geojson")
5558
def get_area_boundary(areacodes: str, es: ElasticsearchDep, s3_client: S3Dep):
5659
areacode_list: list[str] = areacodes.split("+")
5760
features = []
@@ -66,6 +69,7 @@ def get_area_boundary(areacodes: str, es: ElasticsearchDep, s3_client: S3Dep):
6669

6770

6871
@bp.get("/{areacode}/children/{areatype}.geojson")
72+
@api.get("/{areacode}/children/{areatype}.geojson")
6973
def get_area_children_boundary(
7074
areacode: str, areatype: str, es: ElasticsearchDep, s3_client: S3Dep
7175
):
@@ -88,6 +92,7 @@ def get_area_children_boundary(
8892

8993
@bp.get("/{areacode}")
9094
@bp.get("/{areacode}.{filetype}")
95+
@api.get("/{areacode}")
9196
def get_area(
9297
areacode: str,
9398
request: Request,

findthatpostcode/blueprints/areatypes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
bp = APIRouter(prefix="/areatypes")
1515

16+
api = APIRouter(prefix="/areatypes")
17+
1618

1719
@bp.get("/")
1820
def all_areatypes(es: ElasticsearchDep, request: Request) -> Response:
@@ -27,6 +29,7 @@ def all_areatypes(es: ElasticsearchDep, request: Request) -> Response:
2729

2830
@bp.get("/{areacode}")
2931
@bp.get("/{areacode}.{filetype}")
32+
@api.get("/{areacode}")
3033
def get_areatype(
3134
areacode: str,
3235
request: Request,

findthatpostcode/blueprints/places.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
bp = APIRouter(prefix="/places")
99

10+
api = APIRouter(prefix="/places")
11+
1012

1113
@bp.get("/redirect")
1214
def point_redirect(lat: float, lon: float, request: Request) -> Response:
@@ -18,6 +20,7 @@ def point_redirect(lat: float, lon: float, request: Request) -> Response:
1820

1921
@bp.get("/nearest/{lat},{lon}")
2022
@bp.get("/nearest/{lat},{lon}.{filetype}")
23+
@api.get("/nearest/{lat},{lon}")
2124
def nearest(
2225
lat: float, lon: float, es: ElasticsearchDep, filetype: str = "json"
2326
) -> Response:
@@ -40,6 +43,7 @@ def nearest(
4043

4144
@bp.get("/{areacode}")
4245
@bp.get("/{areacode}.{filetype}")
46+
@api.get("/{areacode}")
4347
def get_place(
4448
areacode: str, es: ElasticsearchDep, request: Request, filetype: str = "json"
4549
):

findthatpostcode/blueprints/points.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
bp = APIRouter(prefix="/points")
1010

11+
api = APIRouter(prefix="/points")
12+
1113

1214
@bp.get("/redirect")
1315
def point_redirect(lat: float, lon: float, request: Request) -> Response:
@@ -20,6 +22,7 @@ def point_redirect(lat: float, lon: float, request: Request) -> Response:
2022

2123

2224
@bp.get("/{latlon}")
25+
@api.get("/{latlon}")
2326
def get_point(latlon: str, es: ElasticsearchDep, request: Request):
2427
filetype = "json"
2528
if latlon.endswith(".json"):

findthatpostcode/blueprints/postcodes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
bp = APIRouter(prefix="/postcodes")
1919

20+
api = APIRouter(prefix="/postcodes")
21+
2022

2123
@bp.get("/redirect")
2224
def postcode_redirect(postcode: str, request: Request) -> Response:
@@ -28,6 +30,7 @@ def postcode_redirect(postcode: str, request: Request) -> Response:
2830

2931
@bp.get("/{postcode}")
3032
@bp.get("/{postcode}.{filetype}")
33+
@api.get("/{postcode}")
3134
def get_postcode(
3235
postcode: str, request: Request, es: ElasticsearchDep, filetype: str = "json"
3336
):
@@ -39,6 +42,7 @@ def get_postcode(
3942

4043
@bp.get("/hash/{hash_}")
4144
@bp.get("/hash/{hash_}.json")
45+
@api.get("/hash/{hash_}")
4246
def single_hash(
4347
hash_: str,
4448
properties: Annotated[list[str], Query()],
@@ -48,7 +52,9 @@ def single_hash(
4852
return JSONResponse({"data": get_postcode_by_hash(es, hash_, properties)})
4953

5054

55+
@bp.get("/hashes")
5156
@bp.get("/hashes.json")
57+
@api.get("/hashes")
5258
def multi_hash_get(
5359
hash: Annotated[str | list[str], Query()],
5460
properties: Annotated[list[str], Query()],
@@ -58,7 +64,9 @@ def multi_hash_get(
5864
return JSONResponse({"data": get_postcode_by_hash(es, hash, properties)})
5965

6066

67+
@bp.post("/hashes")
6168
@bp.post("/hashes.json")
69+
@api.post("/hashes")
6270
def multi_hash_post(
6371
hash: Annotated[str | list[str], Form()],
6472
properties: Annotated[list[str], Form()],

findthatpostcode/blueprints/reconcile.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
bp = APIRouter(prefix="/reconcile")
1616

17+
api = APIRouter(prefix="/reconcile")
18+
1719

1820
def recon_query(q, es, p: int = 1, size: int = 10):
1921
result = []
@@ -33,7 +35,7 @@ def recon_query(q, es, p: int = 1, size: int = 10):
3335
elif query:
3436
pagination = Pagination(page=p, size=size)
3537
areas = search_areas(query, es, pagination=pagination)
36-
for a, score in zip(areas["result"], areas["scores"]):
38+
for a, score in zip(areas["result"], areas["scores"]): # type: ignore
3739
result.append(
3840
{
3941
"id": a.id,
@@ -52,19 +54,21 @@ class ReconcileRequest(BaseModel):
5254

5355

5456
@bp.get("/")
57+
@api.get("/")
5558
def reconcile_get(
5659
es: ElasticsearchDep,
57-
extend: Annotated[str, Query()] = None,
60+
extend: Annotated[str | None, Query()] = None,
5861
queries: Annotated[str | None, Query()] = None,
5962
callback: str | None = None,
6063
):
6164
return do_reconcile(es, extend, queries, callback)
6265

6366

6467
@bp.post("/")
68+
@api.post("/")
6569
def reconcile_post(
6670
es: ElasticsearchDep,
67-
extend: Annotated[str, Form()] = None,
71+
extend: Annotated[str | None, Form()] = None,
6872
queries: Annotated[str | None, Form()] = None,
6973
callback: Annotated[str | None, Query()] = None,
7074
):

findthatpostcode/blueprints/search.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def search_index(
4949
if latlon:
5050
return RedirectResponse(
5151
request.url_for(
52-
"get_point", latlon="{},{}.html".format(latlon["lat"], latlon["lon"])
52+
"get_point",
53+
latlon="{},{}.html".format(latlon["lat"], latlon["lon"]), # type: ignore
5354
),
5455
status_code=303,
5556
)
@@ -66,8 +67,8 @@ def search_index(
6667
es,
6768
pagination=pagination,
6869
)
69-
result = zip(areas["result"], areas["scores"])
70-
pagination.set_pagination(areas["result_count"])
70+
result = zip(areas["result"], areas["scores"]) # type: ignore
71+
pagination.set_pagination(areas["result_count"]) # type: ignore
7172
nav = {
7273
p: request.url_for("search_index").include_query_params(q=q, **args)
7374
if isinstance(args, dict)

findthatpostcode/main.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from fastapi.staticfiles import StaticFiles
55
from sentry_sdk.integrations.fastapi import FastApiIntegration
66

7+
from findthatpostcode.blueprints import api as legacy_router
78
from findthatpostcode.blueprints import app as legacy_app
89
from findthatpostcode.routers import router as api_router
9-
from findthatpostcode.routers.legacy import router as legacy_router
1010
from findthatpostcode.settings import ENVIRONMENT, SENTRY_DSN, STATIC_DIR
1111

1212
if SENTRY_DSN:
@@ -19,7 +19,15 @@
1919
)
2020

2121

22-
app = FastAPI()
22+
app = FastAPI(
23+
title="Find that Postcode",
24+
version="2.0",
25+
description="""
26+
This site presents data on UK postcodes and geographical areas, based on open data released by
27+
the [Office for National Statistics](https://geoportal.statistics.gov.uk/) and
28+
[Ordnance Survey](https://osdatahub.os.uk/).
29+
""",
30+
)
2331

2432

2533
app.mount(
@@ -35,8 +43,9 @@
3543

3644
app.add_middleware(
3745
CORSMiddleware,
38-
# allow_origins=["*"],
39-
# allow_credentials=True,
40-
# allow_methods=["*"],
41-
# allow_headers=["*"],
46+
allow_origins=["*"],
47+
allow_credentials=True,
48+
allow_methods=["*"],
49+
allow_headers=["*"],
50+
expose_headers=["Content-Disposition"],
4251
)

findthatpostcode/routers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
router = APIRouter()
66

7-
router.include_router(postcodes_router, prefix="/postcodes", tags=["postcodes"])
7+
router.include_router(postcodes_router, prefix="/postcodes")

0 commit comments

Comments
 (0)