Skip to content

Commit a763a61

Browse files
authored
Merge branch 'master' into kh/compose-AU
2 parents ef29fb9 + 6bac9be commit a763a61

File tree

326 files changed

+1472
-3014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

326 files changed

+1472
-3014
lines changed

api/api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import contextlib
22
import os
33
from typing import Annotated
4+
import sys
45

56
import asyncpg
67
from fastapi import FastAPI
@@ -14,6 +15,9 @@
1415
from openrailwaymap_api.replication_api import ReplicationAPI
1516
from openrailwaymap_api.wikidata_api import WikidataAPI
1617

18+
DEFAULT_HTTP_HEADERS = {
19+
'User-Agent': f'OpenRailwayMap API (https://openrailwaymap.app), httpx {httpx.__version__}, Python {sys.version}'
20+
}
1721

1822
@contextlib.asynccontextmanager
1923
async def lifespan(app):
@@ -28,7 +32,7 @@ async def lifespan(app):
2832
print('Connected to database')
2933
app.state.database = pool
3034

31-
async with httpx.AsyncClient(timeout=3.0) as http_client:
35+
async with httpx.AsyncClient(timeout=3.0, headers=DEFAULT_HTTP_HEADERS) as http_client:
3236
print('Created HTTP client')
3337
app.state.http_client = http_client
3438

@@ -72,10 +76,11 @@ async def facility(
7276
name: Annotated[str | None, Query()] = None,
7377
ref: Annotated[str | None, Query()] = None,
7478
uic_ref: Annotated[str | None, Query()] = None,
79+
lang: Annotated[str | None, Query()] = None,
7580
limit: Annotated[int, Query(ge=MIN_LIMIT, le=MAX_LIMIT)] = DEFAULT_FACILITY_LIMIT,
7681
):
7782
api = FacilityAPI(app.state.database)
78-
return await api(q=q, name=name, ref=ref, uic_ref=uic_ref, limit=limit)
83+
return await api(q=q, name=name, ref=ref, uic_ref=uic_ref, limit=limit, language=lang)
7984

8085

8186
@app.get("/api/milestone")

api/openrailwaymap_api/facility_api.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def eliminate_duplicates(self, data, limit):
2121

2222
return results
2323

24-
async def __call__(self, *, q, name, ref, uic_ref, limit):
24+
async def __call__(self, *, q, name, ref, uic_ref, language, limit):
2525
# Validate search arguments
2626
search_args_count = sum(1 for search_arg in [q, name, ref, uic_ref] if search_arg)
2727

@@ -39,42 +39,42 @@ async def __call__(self, *, q, name, ref, uic_ref, limit):
3939
)
4040

4141
if name:
42-
return await self.search_by_name(name, limit)
42+
return await self.search_by_name(name, language, limit)
4343
if ref:
44-
return await self.search_by_ref(ref, limit)
44+
return await self.search_by_ref(ref, language, limit)
4545
if uic_ref:
46-
return await self.search_by_uic_ref(uic_ref, limit)
46+
return await self.search_by_uic_ref(uic_ref, language, limit)
4747
if q:
48-
return self.eliminate_duplicates((await self.search_by_name(q, limit)) + (await self.search_by_ref(q, limit)) + (await self.search_by_uic_ref(q, limit)), limit)
48+
return self.eliminate_duplicates((await self.search_by_name(q, language, limit)) + (await self.search_by_ref(q, language, limit)) + (await self.search_by_uic_ref(q, language, limit)), limit)
4949

5050
def query_has_no_wildcards(self, q):
5151
if '%' in q or '_' in q:
5252
return False
5353
return True
5454

55-
async def search_by_name(self, q, limit):
55+
async def search_by_name(self, q, language, limit):
5656
if not self.query_has_no_wildcards(q):
5757
raise HTTPException(
5858
HTTP_400_BAD_REQUEST,
5959
{'type': 'wildcard_in_query', 'error': 'Wildcard in query.', 'detail': 'Query contains any of the wildcard characters: %_'}
6060
)
6161

6262
sql_query = """
63-
SELECT * FROM query_facilities_by_name($1, $2)
63+
SELECT * FROM query_facilities_by_name($1, $2, $3)
6464
"""
65-
return await self.query_result(sql_query, (q, limit))
65+
return await self.query_result(sql_query, (q, language, limit))
6666

67-
async def _search_by_ref(self, search_function, ref, limit):
67+
async def _search_by_ref(self, search_function, ref, language, limit):
6868
sql_query = f"""
69-
SELECT * FROM {search_function}($1, $2)
69+
SELECT * FROM {search_function}($1, $2, $3)
7070
"""
71-
return await self.query_result(sql_query, (ref, limit))
71+
return await self.query_result(sql_query, (ref, language, limit))
7272

73-
async def search_by_ref(self, ref, limit):
74-
return await self._search_by_ref("query_facilities_by_ref", ref, limit)
73+
async def search_by_ref(self, ref, language, limit):
74+
return await self._search_by_ref("query_facilities_by_ref", ref, language, limit)
7575

76-
async def search_by_uic_ref(self, ref, limit):
77-
return await self._search_by_ref("query_facilities_by_uic_ref", ref, limit)
76+
async def search_by_uic_ref(self, ref, language, limit):
77+
return await self._search_by_ref("query_facilities_by_uic_ref", ref, language, limit)
7878

7979
async def query_result(self, sql_query, parameters):
8080
async with self.database.acquire() as connection:

api/test/api.hurl

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,35 @@ jsonpath "$[1].name" == "S+U Hauptbahnhof"
1919
jsonpath "$[2].name" == "Hauptbahnhof"
2020
jsonpath "$[3].name" == "Berlin Hauptbahnhof"
2121
jsonpath "$[4].name" == "Berlin Südkreuz"
22-
jsonpath "$[5].name" == "Südkreuz (Nord-Süd)"
23-
jsonpath "$[6].name" == "Südkreuz (Ringbahn)"
24-
jsonpath "$[7].name" == "Berlin-Spandau"
25-
jsonpath "$[8].name" == "Spandau"
26-
jsonpath "$[9].name" == "Berlin Ostbahnhof"
27-
jsonpath "$[10].name" == "Ostbahnhof"
28-
jsonpath "$[11].name" == "Gesundbrunnen"
29-
jsonpath "$[12].name" == "Ostkreuz (Ringbahn)"
30-
jsonpath "$[13].name" == "Berlin Ostkreuz (Stadtbahn-F)"
31-
jsonpath "$[14].name" == "Berlin Ostkreuz (Ringbahn-F)"
32-
jsonpath "$[15].name" == "Ostkreuz"
33-
jsonpath "$[16].name" == "Berlin Gesundbrunnen"
34-
jsonpath "$[17].name" == "Berlin Ostkreuz (Ostbahn)"
35-
jsonpath "$[18].name" == "Alexanderplatz"
36-
jsonpath "$[19].name" == "Berlin Alexanderplatz"
22+
jsonpath "$[5].name" == "Südkreuz (Ringbahn)"
23+
jsonpath "$[6].name" == "Südkreuz (Nord-Süd)"
24+
jsonpath "$[7].name" == "Berlin Nordost"
25+
jsonpath "$[8].name" == "Berlin Greifswalder Straße"
26+
jsonpath "$[9].name" == "Berlin-Grünau"
27+
jsonpath "$[10].name" == "Berlin-Spandau"
28+
jsonpath "$[11].name" == "Berlin-Marienfelde Gbf"
29+
jsonpath "$[12].name" == "Spandau"
30+
jsonpath "$[13].name" == "Berlin-Tempelhof"
31+
jsonpath "$[14].name" == "Berlin-Schönholz"
32+
jsonpath "$[15].name" == "Berlin Ostbahnhof"
33+
jsonpath "$[16].name" == "Ostbahnhof"
34+
jsonpath "$[17].name" == "Gesundbrunnen"
35+
jsonpath "$[18].name" == "Berlin Gesundbrunnen"
36+
jsonpath "$[19].name" == "Berlin-Neukölln"
37+
38+
# Facility request with language
39+
GET {{base_url}}/facility
40+
[QueryStringParams]
41+
name: Berlin Ostbahnhof
42+
lang: en
43+
HTTP 200
44+
Content-Type: application/json
45+
[Asserts]
46+
jsonpath "$" count == 2
47+
jsonpath "$[0].name" == "Ostbahnhof"
48+
jsonpath "$[0].localized_name" == "Ostbahnhof"
49+
jsonpath "$[1].name" == "Berlin Ostbahnhof"
50+
jsonpath "$[1].localized_name" == "Berlin Ostbahnhof railway station"
3751

3852
# Facility request with limit
3953
GET {{base_url}}/facility
@@ -80,11 +94,11 @@ HTTP 200
8094
jsonpath "$" count == 7
8195
jsonpath "$[0].name" == "Berlin-Spandau"
8296
jsonpath "$[1].name" == "Spandau"
83-
jsonpath "$[2].name" == "Berlin-Spandau Gbf"
84-
jsonpath "$[3].name" == "Berlin-Spandau West"
85-
jsonpath "$[4].name" == "Berlin-Spandau Mitte"
86-
jsonpath "$[5].name" == "Berlin-Spandau Ost"
87-
jsonpath "$[6].name" == "Berlin-Spandau Johannesstift"
97+
jsonpath "$[2].name" == "Berlin-Spandau Johannesstift"
98+
jsonpath "$[3].name" == "Berlin-Spandau Gbf"
99+
jsonpath "$[4].name" == "Berlin-Spandau West"
100+
jsonpath "$[5].name" == "Berlin-Spandau Mitte"
101+
jsonpath "$[6].name" == "Berlin-Spandau Ost"
88102

89103
# Facility request with space
90104
GET {{base_url}}/facility
@@ -95,11 +109,11 @@ HTTP 200
95109
jsonpath "$" count == 7
96110
jsonpath "$[0].name" == "Berlin-Spandau"
97111
jsonpath "$[1].name" == "Spandau"
98-
jsonpath "$[2].name" == "Berlin-Spandau Gbf"
99-
jsonpath "$[3].name" == "Berlin-Spandau West"
100-
jsonpath "$[4].name" == "Berlin-Spandau Mitte"
101-
jsonpath "$[5].name" == "Berlin-Spandau Ost"
102-
jsonpath "$[6].name" == "Berlin-Spandau Johannesstift"
112+
jsonpath "$[2].name" == "Berlin-Spandau Johannesstift"
113+
jsonpath "$[3].name" == "Berlin-Spandau Gbf"
114+
jsonpath "$[4].name" == "Berlin-Spandau West"
115+
jsonpath "$[5].name" == "Berlin-Spandau Mitte"
116+
jsonpath "$[6].name" == "Berlin-Spandau Ost"
103117

104118
# Facility request for Spandau
105119
GET {{base_url}}/facility
@@ -110,13 +124,13 @@ HTTP 200
110124
jsonpath "$" count == 9
111125
jsonpath "$[0].name" == "Berlin-Spandau"
112126
jsonpath "$[1].name" == "Spandau"
113-
jsonpath "$[2].name" == "Altstadt Spandau"
114-
jsonpath "$[3].name" == "Rathaus Spandau"
115-
jsonpath "$[4].name" == "Berlin-Spandau Gbf"
116-
jsonpath "$[5].name" == "Berlin-Spandau West"
117-
jsonpath "$[6].name" == "Berlin-Spandau Mitte"
118-
jsonpath "$[7].name" == "Berlin-Spandau Ost"
119-
jsonpath "$[8].name" == "Berlin-Spandau Johannesstift"
127+
jsonpath "$[2].name" == "Berlin-Spandau Johannesstift"
128+
jsonpath "$[3].name" == "Berlin-Spandau Gbf"
129+
jsonpath "$[4].name" == "Rathaus Spandau"
130+
jsonpath "$[5].name" == "Altstadt Spandau"
131+
jsonpath "$[6].name" == "Berlin-Spandau West"
132+
jsonpath "$[7].name" == "Berlin-Spandau Mitte"
133+
jsonpath "$[8].name" == "Berlin-Spandau Ost"
120134

121135
# Facility request with with diacritics
122136
GET {{base_url}}/facility
@@ -201,7 +215,7 @@ jsonpath "$" count == 3
201215
jsonpath "$[0].name" == "Landsberger Allee/Petersburger Straße"
202216
jsonpath "$[0].feature" == "tram_stop"
203217
jsonpath "$[0].state" == "present"
204-
jsonpath "$[0].rank" == 32
218+
jsonpath "$[0].rank" == 34
205219
jsonpath "$[0].osm_ids" count == 4
206220
jsonpath "$[0].osm_ids[0]" == 244129991
207221
jsonpath "$[0].osm_ids[1]" == 271777826

0 commit comments

Comments
 (0)