X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/0c25e80be0868ff26e11f04298967af5f5e5adc3..e1dc4379e0cd100200ac53752442143ca4846fc5:/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 84d73aec..77c50f31 100644 --- a/src/nominatim_api/server/asgi_adaptor.py +++ b/src/nominatim_api/server/asgi_adaptor.py @@ -12,16 +12,10 @@ import abc import math from ..config import Configuration -from .. import logging as loglib from ..core import NominatimAPIAsync from ..result_formatting import FormatDispatcher +from .content_types import CONTENT_TEXT -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} class ASGIAdaptor(abc.ABC): """ Adapter class for the different ASGI frameworks. @@ -29,7 +23,6 @@ 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 @@ -42,14 +35,12 @@ class ASGIAdaptor(abc.ABC): not provided, return the 'default' value. """ - @abc.abstractmethod def error(self, msg: str, status: int = 400) -> Exception: """ Construct an appropriate exception from the given error message. The exception must result in a HTTP error with the given status. """ - @abc.abstractmethod def create_response(self, status: int, output: str, num_results: int) -> Any: """ Create a response from the given parameters. The result will @@ -62,25 +53,21 @@ class ASGIAdaptor(abc.ABC): body of the response to 'output'. """ - @abc.abstractmethod def base_uri(self) -> str: """ Return the URI of the original request. """ - @abc.abstractmethod def config(self) -> Configuration: """ Return the current configuration object. """ - @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. @@ -104,7 +91,6 @@ class ASGIAdaptor(abc.ABC): return intval - def get_float(self, name: str, default: Optional[float] = None) -> float: """ Return an input parameter as a flaoting-point number. Raises an exception if the parameter is given but not in an float format. @@ -131,7 +117,6 @@ class ASGIAdaptor(abc.ABC): return fval - def get_bool(self, name: str, default: Optional[bool] = None) -> bool: """ Return an input parameter as bool. Only '0' is accepted as an input for 'false' all other inputs will be interpreted as 'true'. @@ -150,28 +135,13 @@ class ASGIAdaptor(abc.ABC): return value != '0' - def raise_error(self, msg: str, status: int = 400) -> NoReturn: """ Raise an exception resulting in the given HTTP status and 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]