mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-12 22:04:07 +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)
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from pathlib import Path
|
||||
import sanic
|
||||
|
||||
from nominatim.api import NominatimAPIAsync, StatusResult
|
||||
import nominatim.result_formatter.v1 as formatting
|
||||
import nominatim.api.v1 as api_impl
|
||||
|
||||
api = sanic.Blueprint('NominatimAPI')
|
||||
|
||||
@@ -32,7 +32,7 @@ def api_response(request: sanic.Request, result: Any) -> sanic.HTTPResponse:
|
||||
""" Render a response from the query results using the configured
|
||||
formatter.
|
||||
"""
|
||||
body = request.ctx.formatter.format(result, request.ctx.format)
|
||||
body = api_impl.format_result(result, request.ctx.format)
|
||||
return sanic.response.text(body,
|
||||
content_type=CONTENT_TYPE.get(request.ctx.format,
|
||||
'application/json'))
|
||||
@@ -46,12 +46,11 @@ async def extract_format(request: sanic.Request) -> Optional[sanic.HTTPResponse]
|
||||
is present.
|
||||
"""
|
||||
assert request.route is not None
|
||||
request.ctx.formatter = request.app.ctx.formatters[request.route.ctx.result_type]
|
||||
|
||||
request.ctx.format = request.args.get('format', request.route.ctx.default_format)
|
||||
if not request.ctx.formatter.supports_format(request.ctx.format):
|
||||
if not api_impl.supports_format(request.route.ctx.result_type, request.ctx.format):
|
||||
return usage_error("Parameter 'format' must be one of: " +
|
||||
', '.join(request.ctx.formatter.list_formats()))
|
||||
', '.join(api_impl.list_formats(request.route.ctx.result_type)))
|
||||
|
||||
return None
|
||||
|
||||
@@ -76,9 +75,6 @@ def get_application(project_dir: Path,
|
||||
app = sanic.Sanic("NominatimInstance")
|
||||
|
||||
app.ctx.api = NominatimAPIAsync(project_dir, environ)
|
||||
app.ctx.formatters = {}
|
||||
for rtype in (StatusResult, ):
|
||||
app.ctx.formatters[rtype] = formatting.create(rtype)
|
||||
|
||||
app.blueprint(api)
|
||||
|
||||
|
||||
@@ -17,40 +17,32 @@ from starlette.responses import Response
|
||||
from starlette.requests import Request
|
||||
|
||||
from nominatim.api import NominatimAPIAsync, StatusResult
|
||||
import nominatim.result_formatter.v1 as formatting
|
||||
import nominatim.api.v1 as api_impl
|
||||
|
||||
CONTENT_TYPE = {
|
||||
'text': 'text/plain; charset=utf-8',
|
||||
'xml': 'text/xml; charset=utf-8'
|
||||
}
|
||||
|
||||
FORMATTERS = {
|
||||
StatusResult: formatting.create(StatusResult)
|
||||
}
|
||||
|
||||
|
||||
def parse_format(request: 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.
|
||||
"""
|
||||
fmt = request.query_params.get('format', default=default)
|
||||
fmtter = FORMATTERS[rtype]
|
||||
|
||||
if not fmtter.supports_format(fmt):
|
||||
if not api_impl.supports_format(rtype, fmt):
|
||||
raise HTTPException(400, detail="Parameter 'format' must be one of: " +
|
||||
', '.join(fmtter.list_formats()))
|
||||
', '.join(api_impl.list_formats(rtype)))
|
||||
|
||||
request.state.format = fmt
|
||||
request.state.formatter = fmtter
|
||||
|
||||
|
||||
def format_response(request: Request, result: Any) -> Response:
|
||||
""" Render response into a string according to the formatter
|
||||
set in `parse_format()`.
|
||||
""" Render response into a string according.
|
||||
"""
|
||||
fmt = request.state.format
|
||||
return Response(request.state.formatter.format(result, fmt),
|
||||
return Response(api_impl.format_result(result, fmt),
|
||||
media_type=CONTENT_TYPE.get(fmt, 'application/json'))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user