您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

57 行
2.0 KiB

  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. from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, Tuple
  15. from twisted.web.server import Request
  16. from synapse.http.server import DirectServeJsonResource
  17. if TYPE_CHECKING:
  18. from synapse.server import HomeServer
  19. class AdditionalResource(DirectServeJsonResource):
  20. """Resource wrapper for additional_resources
  21. If the user has configured additional_resources, we need to wrap the
  22. handler class with a Resource so that we can map it into the resource tree.
  23. This class is also where we wrap the request handler with logging, metrics,
  24. and exception handling.
  25. """
  26. def __init__(
  27. self,
  28. hs: "HomeServer",
  29. handler: Callable[[Request], Awaitable[Optional[Tuple[int, Any]]]],
  30. ):
  31. """Initialise AdditionalResource
  32. The ``handler`` should return a deferred which completes when it has
  33. done handling the request. It should write a response with
  34. ``request.write()``, and call ``request.finish()``.
  35. Args:
  36. hs: homeserver
  37. handler: function to be called to handle the request.
  38. """
  39. super().__init__()
  40. self._handler = handler
  41. async def _async_render(self, request: Request) -> Optional[Tuple[int, Any]]:
  42. # Cheekily pass the result straight through, so we don't need to worry
  43. # if its an awaitable or not.
  44. return await self._handler(request)