Skip to content

Commit 184e47e

Browse files
feat: make geodatabase client optionally connect to ohsomedb
instead of oqpapi postgres database
1 parent 635742c commit 184e47e

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

ohsome_quality_api/geodatabase/client.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import os
1818
from contextlib import asynccontextmanager
19+
from typing import Literal
1920

2021
import asyncpg
2122
import geojson
@@ -28,24 +29,40 @@
2829

2930

3031
@asynccontextmanager
31-
async def get_connection():
32+
async def get_connection(database: Literal["oqapidb", "ohsomedb"] = "oqapidb"):
3233
# DNS in libpq connection URI format
33-
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
34-
host=get_config_value("postgres_host"),
35-
port=get_config_value("postgres_port"),
36-
database=get_config_value("postgres_db"),
37-
user=get_config_value("postgres_user"),
38-
password=get_config_value("postgres_password"),
39-
)
34+
match database:
35+
case "oqapidb":
36+
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
37+
host=get_config_value("postgres_host"),
38+
port=get_config_value("postgres_port"),
39+
database=get_config_value("postgres_db"),
40+
user=get_config_value("postgres_user"),
41+
password=get_config_value("postgres_password"),
42+
)
43+
case "ohsomedb":
44+
dns = "postgres://{user}:{password}@{host}:{port}/{database}".format(
45+
host=get_config_value("ohsomedb_host"),
46+
port=get_config_value("ohsomedb_port"),
47+
database=get_config_value("ohsomedb_db"),
48+
user=get_config_value("ohsomedb_user"),
49+
password=get_config_value("ohsomedb_password"),
50+
)
51+
case _:
52+
raise ValueError()
4053
conn = await asyncpg.connect(dns)
4154
try:
4255
yield conn
4356
finally:
4457
await conn.close()
4558

4659

47-
async def fetch(query: str, *args) -> list:
48-
async with get_connection() as conn:
60+
async def fetch(
61+
query: str,
62+
*args,
63+
database: Literal["oqapidb", "ohsomedb"] = "oqapidb",
64+
) -> list:
65+
async with get_connection(database) as conn:
4966
return await conn.fetch(query, *args)
5067

5168

ohsome_quality_api/indicators/currentness/indicator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async def preprocess_ohsomedb(self):
134134
"filter": where,
135135
}
136136
)
137-
results = await client.fetch(query)
137+
results = await client.fetch(query, database="ohsomedb")
138138
if len(results) == 0:
139139
# no data
140140
self.contrib_sum = 0

0 commit comments

Comments
 (0)