1# Copyright 2017 New Vector Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, Tuple 16 17from twisted.web.server import Request 18 19from synapse.http.server import DirectServeJsonResource 20 21if TYPE_CHECKING: 22 from synapse.server import HomeServer 23 24 25class AdditionalResource(DirectServeJsonResource): 26 """Resource wrapper for additional_resources 27 28 If the user has configured additional_resources, we need to wrap the 29 handler class with a Resource so that we can map it into the resource tree. 30 31 This class is also where we wrap the request handler with logging, metrics, 32 and exception handling. 33 """ 34 35 def __init__( 36 self, 37 hs: "HomeServer", 38 handler: Callable[[Request], Awaitable[Optional[Tuple[int, Any]]]], 39 ): 40 """Initialise AdditionalResource 41 42 The ``handler`` should return a deferred which completes when it has 43 done handling the request. It should write a response with 44 ``request.write()``, and call ``request.finish()``. 45 46 Args: 47 hs: homeserver 48 handler ((twisted.web.server.Request) -> twisted.internet.defer.Deferred): 49 function to be called to handle the request. 50 """ 51 super().__init__() 52 self._handler = handler 53 54 async def _async_render(self, request: Request) -> Optional[Tuple[int, Any]]: 55 # Cheekily pass the result straight through, so we don't need to worry 56 # if its an awaitable or not. 57 return await self._handler(request) 58