1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of classes for API access via libraries.
10 from typing import Mapping, Optional, TypeVar, Callable, Any
13 from pathlib import Path
15 from sqlalchemy.engine.url import URL
16 from sqlalchemy.ext.asyncio import create_async_engine
18 from nominatim.typing import StrPath
19 from nominatim.config import Configuration
20 from nominatim.apicmd.status import get_status, StatusResult
22 class NominatimAPIAsync:
23 """ API loader asynchornous version.
25 def __init__(self, project_dir: Path,
26 environ: Optional[Mapping[str, str]] = None) -> None:
27 self.config = Configuration(project_dir, environ)
29 dsn = self.config.get_database_params()
33 database=dsn.get('dbname'),
34 username=dsn.get('user'), password=dsn.get('password'),
35 host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
36 query={k: v for k, v in dsn.items()
37 if k not in ('user', 'password', 'dbname', 'host', 'port')})
38 self.engine = create_async_engine(dburl,
39 connect_args={"server_settings": {"jit": "off"}},
43 async def status(self) -> StatusResult:
44 """ Return the status of the database.
46 return await get_status(self.engine)
50 """ API loader, synchronous version.
53 def __init__(self, project_dir: Path,
54 environ: Optional[Mapping[str, str]] = None) -> None:
55 self.async_api = NominatimAPIAsync(project_dir, environ)
58 def status(self) -> StatusResult:
59 return asyncio.get_event_loop().run_until_complete(self.async_api.status())