Skip to content

Commit 917f4bc

Browse files
authored
Add User-Agent header to Wikidata API requests (#693)
Fixes #692 This will send a user agent header of the form ``` OpenRailwayMap API (https://openrailwaymap.app), httpx 0.28.1, Python 3.13.9 (main, Oct 15 2025, 16:47:14) [GCC 14.2.0] ``` Now it works ```shell http http://localhost:8000/api/wikidata/Q567336 HTTP/1.1 307 Temporary Redirect Cache-Control: public, max-age=0, stale-if-error=0 Connection: keep-alive Content-Length: 0 Date: Sun, 30 Nov 2025 14:46:38 GMT Server: nginx/1.29.3 location: https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Bahnhof_Berlin-Wannsee_Empfangsgebaeude_04-2015.jpg/330px-Bahnhof_Berlin-Wannsee_Empfangsgebaeude_04-2015.jpg ``` Popup also shows images again <img width="657" height="543" alt="image" src="https://github.com/user-attachments/assets/79ccb99d-8de1-4591-9aba-27bda2cfa23a" />
1 parent 4e13e82 commit 917f4bc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

api/api.py

Lines changed: 5 additions & 1 deletion
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

0 commit comments

Comments
 (0)