make formatting module non-static

This commit is contained in:
Sarah Hoffmann
2024-08-13 22:39:43 +02:00
parent 4e0602919c
commit 0c25e80be0
5 changed files with 37 additions and 14 deletions

View File

@@ -14,6 +14,7 @@ import math
from ..config import Configuration
from .. import logging as loglib
from ..core import NominatimAPIAsync
from ..result_formatting import FormatDispatcher
CONTENT_TEXT = 'text/plain; charset=utf-8'
CONTENT_XML = 'text/xml; charset=utf-8'
@@ -28,6 +29,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 +62,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 +75,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.

View File

@@ -17,6 +17,8 @@ from falcon.asgi import App, Request, Response
from ...config import Configuration
from ...core import NominatimAPIAsync
from ... import v1 as api_impl
from ...result_formatting import FormatDispatcher
from ...v1.format import dispatch as formatting
from ... import logging as loglib
from ..asgi_adaptor import ASGIAdaptor, EndpointFunc
@@ -62,8 +64,7 @@ class ParamWrapper(ASGIAdaptor):
""" Adaptor class for server glue to Falcon framework.
"""
def __init__(self, req: Request, resp: Response,
config: Configuration) -> None:
def __init__(self, req: Request, resp: Response, config: Configuration) -> None:
self.request = req
self.response = resp
self._config = config
@@ -94,6 +95,9 @@ class ParamWrapper(ASGIAdaptor):
def config(self) -> Configuration:
return self._config
def formatting(self) -> FormatDispatcher:
return formatting
class EndpointWrapper:
""" Converter for server glue endpoint functions to Falcon request handlers.

View File

@@ -24,6 +24,8 @@ from starlette.middleware.cors import CORSMiddleware
from ...config import Configuration
from ...core import NominatimAPIAsync
from ... import v1 as api_impl
from ...result_formatting import FormatDispatcher
from ...v1.format import dispatch as formatting
from ..asgi_adaptor import ASGIAdaptor, EndpointFunc
from ... import logging as loglib
@@ -70,6 +72,10 @@ class ParamWrapper(ASGIAdaptor):
return cast(Configuration, self.request.app.state.API.config)
def formatting(self) -> FormatDispatcher:
return formatting
def _wrap_endpoint(func: EndpointFunc)\
-> Callable[[Request], Coroutine[Any, Any, Response]]:
async def _callback(request: Request) -> Response: