X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/4e0602919cfdad274a6d04c9798f2d61f1b03cf3..83013f819b463b14332854dd80cc47629bea0d1a:/src/nominatim_api/v1/server_glue.py?ds=sidebyside diff --git a/src/nominatim_api/v1/server_glue.py b/src/nominatim_api/v1/server_glue.py index 5f9212e1..a9d30842 100644 --- a/src/nominatim_api/v1/server_glue.py +++ b/src/nominatim_api/v1/server_glue.py @@ -18,21 +18,21 @@ import sqlalchemy as sa from ..errors import UsageError from .. import logging as loglib from ..core import NominatimAPIAsync -from .format import dispatch as formatting from .format import RawDataList from ..types import DataLayer, GeometryFormat, PlaceRef, PlaceID, OsmID, Point from ..status import StatusResult from ..results import DetailedResult, ReverseResults, SearchResult, SearchResults from ..localization import Locales from . import helpers -from ..server.asgi_adaptor import CONTENT_HTML, CONTENT_JSON, CONTENT_TYPE, ASGIAdaptor +from ..server import content_types as ct +from ..server.asgi_adaptor import ASGIAdaptor def build_response(adaptor: ASGIAdaptor, output: str, status: int = 200, num_results: int = 0) -> Any: """ Create a response from the given output. Wraps a JSONP function around the response, if necessary. """ - if adaptor.content_type == CONTENT_JSON and status == 200: + if adaptor.content_type == ct.CONTENT_JSON and status == 200: jsonp = adaptor.get('json_callback') if jsonp is not None: if any(not part.isidentifier() for part in jsonp.split('.')): @@ -58,7 +58,7 @@ def setup_debugging(adaptor: ASGIAdaptor) -> bool: """ if adaptor.get_bool('debug', False): loglib.set_log_output('html') - adaptor.content_type = CONTENT_HTML + adaptor.content_type = ct.CONTENT_HTML return True return False @@ -84,11 +84,13 @@ def parse_format(adaptor: ASGIAdaptor, result_type: Type[Any], default: str) -> fmt = adaptor.get('format', default=default) assert fmt is not None + formatting = adaptor.formatting() + if not formatting.supports_format(result_type, fmt): adaptor.raise_error("Parameter 'format' must be one of: " + ', '.join(formatting.list_formats(result_type))) - adaptor.content_type = CONTENT_TYPE.get(fmt, CONTENT_JSON) + adaptor.content_type = formatting.get_content_type(fmt) return fmt @@ -132,7 +134,7 @@ async def status_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: else: status_code = 200 - return build_response(params, formatting.format_result(result, fmt, {}), + return build_response(params, params.formatting().format_result(result, fmt, {}), status=status_code) @@ -171,7 +173,7 @@ async def details_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: if result is None: params.raise_error('No place with that OSM ID found.', status=404) - output = formatting.format_result(result, fmt, + output = params.formatting().format_result(result, fmt, {'locales': locales, 'group_hierarchy': params.get_bool('group_hierarchy', False), 'icon_base_url': params.config().MAPICON_URL}) @@ -210,8 +212,8 @@ async def reverse_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: 'namedetails': params.get_bool('namedetails', False), 'addressdetails': params.get_bool('addressdetails', True)} - output = formatting.format_result(ReverseResults([result] if result else []), - fmt, fmt_options) + output = params.formatting().format_result(ReverseResults([result] if result else []), + fmt, fmt_options) return build_response(params, output, num_results=1 if result else 0) @@ -245,7 +247,7 @@ async def lookup_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: 'namedetails': params.get_bool('namedetails', False), 'addressdetails': params.get_bool('addressdetails', True)} - output = formatting.format_result(results, fmt, fmt_options) + output = params.formatting().format_result(results, fmt, fmt_options) return build_response(params, output, num_results=len(results)) @@ -356,7 +358,7 @@ async def search_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: 'namedetails': params.get_bool('namedetails', False), 'addressdetails': params.get_bool('addressdetails', False)} - output = formatting.format_result(results, fmt, fmt_options) + output = params.formatting().format_result(results, fmt, fmt_options) return build_response(params, output, num_results=len(results)) @@ -378,7 +380,7 @@ async def deletable_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any """) results = RawDataList(r._asdict() for r in await conn.execute(sql)) - return build_response(params, formatting.format_result(results, fmt, {})) + return build_response(params, params.formatting().format_result(results, fmt, {})) async def polygons_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: @@ -410,7 +412,7 @@ async def polygons_endpoint(api: NominatimAPIAsync, params: ASGIAdaptor) -> Any: results = RawDataList(r._asdict() for r in await conn.execute(sql, sql_params)) - return build_response(params, formatting.format_result(results, fmt, {})) + return build_response(params, params.formatting().format_result(results, fmt, {})) ROUTES = [