mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-13 14:24:08 +00:00
reorganize code around result formatting
Code is now organized by api version. So formatting has moved to the api.v1 module. Instead of holding a separate ResultFormatter object per result format, simply move the functions to the formater collector and hand in the requested format as a parameter. Thus reorganized, the api.v1 module can export three simple functions for result formatting which in turn makes the code that uses the formatters much simpler.
This commit is contained in:
@@ -14,7 +14,7 @@ import falcon
|
||||
import falcon.asgi
|
||||
|
||||
from nominatim.api import NominatimAPIAsync, StatusResult
|
||||
import nominatim.result_formatter.v1 as formatting
|
||||
import nominatim.api.v1 as api_impl
|
||||
|
||||
CONTENT_TYPE = {
|
||||
'text': falcon.MEDIA_TEXT,
|
||||
@@ -27,10 +27,6 @@ class NominatimV1:
|
||||
|
||||
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: falcon.asgi.Request, rtype: Type[Any], default: str) -> None:
|
||||
@@ -39,12 +35,11 @@ class NominatimV1:
|
||||
format value to assume when no parameter is present.
|
||||
"""
|
||||
req.context.format = req.get_param('format', default=default)
|
||||
req.context.formatter = self.formatters[rtype]
|
||||
|
||||
if not req.context.formatter.supports_format(req.context.format):
|
||||
if not api_impl.supports_format(rtype, req.context.format):
|
||||
raise falcon.HTTPBadRequest(
|
||||
description="Parameter 'format' must be one of: " +
|
||||
', '.join(req.context.formatter.list_formats()))
|
||||
', '.join(api_impl.list_formats(rtype)))
|
||||
|
||||
|
||||
def format_response(self, req: falcon.asgi.Request, resp: falcon.asgi.Response,
|
||||
@@ -52,7 +47,7 @@ class NominatimV1:
|
||||
""" Render response into a string according to the formatter
|
||||
set in `parse_format()`.
|
||||
"""
|
||||
resp.text = req.context.formatter.format(result, req.context.format)
|
||||
resp.text = api_impl.format_result(result, req.context.format)
|
||||
resp.content_type = CONTENT_TYPE.get(req.context.format, falcon.MEDIA_JSON)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user