mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-07 02:24:08 +00:00
fix liniting issues and add type annotations
This commit is contained in:
@@ -7,8 +7,10 @@
|
||||
"""
|
||||
Server implementation using the falcon webserver framework.
|
||||
"""
|
||||
from typing import Type, Any
|
||||
from pathlib import Path
|
||||
|
||||
import falcon
|
||||
import falcon.asgi
|
||||
|
||||
from nominatim.api import NominatimAPIAsync
|
||||
@@ -21,15 +23,22 @@ CONTENT_TYPE = {
|
||||
}
|
||||
|
||||
class NominatimV1:
|
||||
""" Implementation of V1 version of the Nominatim API.
|
||||
"""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
def __init__(self, project_dir: Path) -> None:
|
||||
self.api = NominatimAPIAsync(project_dir)
|
||||
self.formatters = {}
|
||||
|
||||
for rtype in (StatusResult, ):
|
||||
self.formatters[rtype] = formatting.create(rtype)
|
||||
|
||||
def parse_format(self, req, rtype, default):
|
||||
|
||||
def parse_format(self, req: falcon.asgi.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.
|
||||
"""
|
||||
req.context.format = req.get_param('format', default=default)
|
||||
req.context.formatter = self.formatters[rtype]
|
||||
|
||||
@@ -39,12 +48,18 @@ class NominatimV1:
|
||||
', '.join(req.context.formatter.list_formats()))
|
||||
|
||||
|
||||
def format_response(self, req, resp, result):
|
||||
def format_response(self, req: falcon.asgi.Request, resp: falcon.asgi.Response,
|
||||
result: Any) -> None:
|
||||
""" Render response into a string according to the formatter
|
||||
set in `parse_format()`.
|
||||
"""
|
||||
resp.text = req.context.formatter.format(result, req.context.format)
|
||||
resp.content_type = CONTENT_TYPE.get(req.context.format, falcon.MEDIA_JSON)
|
||||
|
||||
|
||||
async def on_get_status(self, req, resp):
|
||||
async def on_get_status(self, req: falcon.asgi.Request, resp: falcon.asgi.Response) -> None:
|
||||
""" Implementation of status endpoint.
|
||||
"""
|
||||
self.parse_format(req, StatusResult, 'text')
|
||||
|
||||
result = await self.api.status()
|
||||
@@ -53,6 +68,8 @@ class NominatimV1:
|
||||
|
||||
|
||||
def get_application(project_dir: Path) -> falcon.asgi.App:
|
||||
""" Create a Nominatim falcon ASGI application.
|
||||
"""
|
||||
app = falcon.asgi.App()
|
||||
|
||||
api = NominatimV1(project_dir)
|
||||
|
||||
Reference in New Issue
Block a user