X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/4e0602919cfdad274a6d04c9798f2d61f1b03cf3..b54ff7d7664eadf3fdf018622540d6fce835502e:/src/nominatim_api/server/asgi_adaptor.py diff --git a/src/nominatim_api/server/asgi_adaptor.py b/src/nominatim_api/server/asgi_adaptor.py index 9558fbd3..49fe288f 100644 --- a/src/nominatim_api/server/asgi_adaptor.py +++ b/src/nominatim_api/server/asgi_adaptor.py @@ -12,15 +12,9 @@ import abc import math from ..config import Configuration -from .. import logging as loglib from ..core import NominatimAPIAsync - -CONTENT_TEXT = 'text/plain; charset=utf-8' -CONTENT_XML = 'text/xml; charset=utf-8' -CONTENT_HTML = 'text/html; charset=utf-8' -CONTENT_JSON = 'application/json; charset=utf-8' - -CONTENT_TYPE = {'text': CONTENT_TEXT, 'xml': CONTENT_XML, 'debug': CONTENT_HTML} +from ..result_formatting import FormatDispatcher +from .content_types import CONTENT_TEXT class ASGIAdaptor(abc.ABC): """ Adapter class for the different ASGI frameworks. @@ -28,6 +22,7 @@ class ASGIAdaptor(abc.ABC): """ content_type: str = CONTENT_TEXT + @abc.abstractmethod def get(self, name: str, default: Optional[str] = None) -> Optional[str]: """ Return an input parameter as a string. If the parameter was @@ -60,6 +55,7 @@ class ASGIAdaptor(abc.ABC): body of the response to 'output'. """ + @abc.abstractmethod def base_uri(self) -> str: """ Return the URI of the original request. @@ -72,6 +68,12 @@ class ASGIAdaptor(abc.ABC): """ + @abc.abstractmethod + def formatting(self) -> FormatDispatcher: + """ Return the formatting object to use. + """ + + def get_int(self, name: str, default: Optional[int] = None) -> int: """ Return an input parameter as an int. Raises an exception if the parameter is given but not in an integer format. @@ -147,22 +149,8 @@ class ASGIAdaptor(abc.ABC): message. The message will be formatted according to the output format chosen by the request. """ - if self.content_type == CONTENT_XML: - msg = f""" - - {status} - {msg} - - """ - elif self.content_type == CONTENT_JSON: - msg = f"""{{"error":{{"code":{status},"message":"{msg}"}}}}""" - elif self.content_type == CONTENT_HTML: - loglib.log().section('Execution error') - loglib.log().var_dump('Status', status) - loglib.log().var_dump('Message', msg) - msg = loglib.get_and_disable() - - raise self.error(msg, status) + raise self.error(self.formatting().format_error(self.content_type, msg, status), + status) EndpointFunc = Callable[[NominatimAPIAsync, ASGIAdaptor], Any]