X-Git-Url: https://git.openstreetmap.org/nominatim.git/blobdiff_plain/8aa01c9c8e5b69e89c84423da826e328346b7fa5..e6bf45a70117d00fc6b3b1a92bdcc480a2a8ee23:/nominatim/server/falcon/server.py diff --git a/nominatim/server/falcon/server.py b/nominatim/server/falcon/server.py index a1ae9c6d..81e6ed39 100644 --- a/nominatim/server/falcon/server.py +++ b/nominatim/server/falcon/server.py @@ -7,8 +7,10 @@ """ Server implementation using the falcon webserver framework. """ +from typing import Type, Any, Optional, Mapping from pathlib import Path +import falcon import falcon.asgi from nominatim.api import NominatimAPIAsync @@ -21,15 +23,22 @@ CONTENT_TYPE = { } class NominatimV1: + """ Implementation of V1 version of the Nominatim API. + """ - def __init__(self, project_dir: Path): - self.api = NominatimAPIAsync(project_dir) + def __init__(self, project_dir: Path, environ: Optional[Mapping[str, str]]) -> None: + self.api = NominatimAPIAsync(project_dir, environ) self.formatters = {} for rtype in (StatusResult, ): self.formatters[rtype] = formatting.create(rtype) - def parse_format(self, req, rtype, default): + + def parse_format(self, req: falcon.asgi.Request, rtype: Type[Any], default: str) -> None: + """ Get and check the 'format' parameter and prepare the formatter. + `rtype` describes the expected return type and `default` the + format value to assume when no parameter is present. + """ req.context.format = req.get_param('format', default=default) req.context.formatter = self.formatters[rtype] @@ -39,23 +48,34 @@ class NominatimV1: ', '.join(req.context.formatter.list_formats())) - def format_response(self, req, resp, result): + def format_response(self, req: falcon.asgi.Request, resp: falcon.asgi.Response, + result: Any) -> None: + """ Render response into a string according to the formatter + set in `parse_format()`. + """ resp.text = req.context.formatter.format(result, req.context.format) resp.content_type = CONTENT_TYPE.get(req.context.format, falcon.MEDIA_JSON) - async def on_get_status(self, req, resp): + async def on_get_status(self, req: falcon.asgi.Request, resp: falcon.asgi.Response) -> None: + """ Implementation of status endpoint. + """ self.parse_format(req, StatusResult, 'text') result = await self.api.status() self.format_response(req, resp, result) + if result.status and req.context.format == 'text': + resp.status = 500 -def get_application(project_dir: Path) -> falcon.asgi.App: +def get_application(project_dir: Path, + environ: Optional[Mapping[str, str]] = None) -> falcon.asgi.App: + """ Create a Nominatim falcon ASGI application. + """ app = falcon.asgi.App() - api = NominatimV1(project_dir) + api = NominatimV1(project_dir, environ) app.add_route('/status', api, suffix='status')