1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Server implementation using the falcon webserver framework.
10 from typing import Type, Any, Optional, Mapping
11 from pathlib import Path
16 from nominatim.api import NominatimAPIAsync, StatusResult
17 import nominatim.api.v1 as api_impl
20 'text': falcon.MEDIA_TEXT,
21 'xml': falcon.MEDIA_XML
25 """ Implementation of V1 version of the Nominatim API.
28 def __init__(self, project_dir: Path, environ: Optional[Mapping[str, str]]) -> None:
29 self.api = NominatimAPIAsync(project_dir, environ)
32 def parse_format(self, req: falcon.asgi.Request, rtype: Type[Any], default: str) -> None:
33 """ Get and check the 'format' parameter and prepare the formatter.
34 `rtype` describes the expected return type and `default` the
35 format value to assume when no parameter is present.
37 req.context.format = req.get_param('format', default=default)
39 if not api_impl.supports_format(rtype, req.context.format):
40 raise falcon.HTTPBadRequest(
41 description="Parameter 'format' must be one of: " +
42 ', '.join(api_impl.list_formats(rtype)))
45 def format_response(self, req: falcon.asgi.Request, resp: falcon.asgi.Response,
47 """ Render response into a string according to the formatter
48 set in `parse_format()`.
50 resp.text = api_impl.format_result(result, req.context.format)
51 resp.content_type = CONTENT_TYPE.get(req.context.format, falcon.MEDIA_JSON)
54 async def on_get_status(self, req: falcon.asgi.Request, resp: falcon.asgi.Response) -> None:
55 """ Implementation of status endpoint.
57 self.parse_format(req, StatusResult, 'text')
59 result = await self.api.status()
61 self.format_response(req, resp, result)
62 if result.status and req.context.format == 'text':
66 def get_application(project_dir: Path,
67 environ: Optional[Mapping[str, str]] = None) -> falcon.asgi.App:
68 """ Create a Nominatim falcon ASGI application.
70 app = falcon.asgi.App()
72 api = NominatimV1(project_dir, environ)
74 app.add_route('/status', api, suffix='status')