|
42 | 42 | Protocol, |
43 | 43 | Tuple, |
44 | 44 | Union, |
| 45 | + cast, |
45 | 46 | ) |
46 | 47 |
|
47 | 48 | import attr |
48 | 49 | import jinja2 |
49 | 50 | from canonicaljson import encode_canonical_json |
50 | 51 | from zope.interface import implementer |
51 | 52 |
|
52 | | -from twisted.internet import defer, interfaces |
| 53 | +from twisted.internet import defer, interfaces, reactor |
53 | 54 | from twisted.internet.defer import CancelledError |
| 55 | +from twisted.internet.interfaces import IReactorTime |
54 | 56 | from twisted.python import failure |
55 | 57 | from twisted.web import resource |
56 | 58 |
|
@@ -401,8 +403,15 @@ class DirectServeJsonResource(_AsyncResource): |
401 | 403 | """ |
402 | 404 |
|
403 | 405 | def __init__( |
404 | | - self, clock: Clock, canonical_json: bool = False, extract_context: bool = False |
| 406 | + self, |
| 407 | + canonical_json: bool = False, |
| 408 | + extract_context: bool = False, |
| 409 | + # Clock is optional as this class is exposed to the module API. |
| 410 | + clock: Optional[Clock] = None, |
405 | 411 | ): |
| 412 | + if clock is None: |
| 413 | + clock = Clock(cast(IReactorTime, reactor)) |
| 414 | + |
406 | 415 | super().__init__(clock, extract_context) |
407 | 416 | self.canonical_json = canonical_json |
408 | 417 |
|
@@ -460,7 +469,7 @@ def __init__( |
460 | 469 | extract_context: bool = False, |
461 | 470 | ): |
462 | 471 | self.clock = hs.get_clock() |
463 | | - super().__init__(self.clock, canonical_json, extract_context) |
| 472 | + super().__init__(canonical_json, extract_context, clock=self.clock) |
464 | 473 | # Map of path regex -> method -> callback. |
465 | 474 | self._routes: Dict[Pattern[str], Dict[bytes, _PathEntry]] = {} |
466 | 475 | self.hs = hs |
@@ -573,6 +582,17 @@ class DirectServeHtmlResource(_AsyncResource): |
573 | 582 | # The error template to use for this resource |
574 | 583 | ERROR_TEMPLATE = HTML_ERROR_TEMPLATE |
575 | 584 |
|
| 585 | + def __init__( |
| 586 | + self, |
| 587 | + extract_context: bool = False, |
| 588 | + # Clock is optional as this class is exposed to the module API. |
| 589 | + clock: Optional[Clock] = None, |
| 590 | + ): |
| 591 | + if clock is None: |
| 592 | + clock = Clock(cast(IReactorTime, reactor)) |
| 593 | + |
| 594 | + super().__init__(clock, extract_context) |
| 595 | + |
576 | 596 | def _send_response( |
577 | 597 | self, |
578 | 598 | request: "SynapseRequest", |
|
0 commit comments