Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add type hints to REST servlets. #10665

Merged
merged 14 commits into from
Aug 23, 2021
Prev Previous commit
Next Next commit
Add type hints to account-validity.
  • Loading branch information
clokep committed Aug 20, 2021
commit df8537ad594a39807f21fd1b194cac33eeca657c
39 changes: 17 additions & 22 deletions synapse/rest/client/account_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@
# limitations under the License.

import logging
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import SynapseError
from synapse.http.server import respond_with_html
from synapse.http.servlet import RestServlet
from twisted.web.server import Request

from synapse.http.server import HttpServer, respond_with_html
from synapse.http.servlet import RestServlet, parse_string
from synapse.http.site import SynapseRequest
from synapse.types import JsonDict

from ._base import client_patterns

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


class AccountValidityRenewServlet(RestServlet):
PATTERNS = client_patterns("/account_validity/renew$")

def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer): server
"""
def __init__(self, hs: "HomeServer"):
super().__init__()

self.hs = hs
Expand All @@ -46,18 +49,14 @@ def __init__(self, hs):
hs.config.account_validity.account_validity_invalid_token_template
)

async def on_GET(self, request):
if b"token" not in request.args:
raise SynapseError(400, "Missing renewal token")
renewal_token = request.args[b"token"][0]
async def on_GET(self, request: Request) -> None:
renewal_token = parse_string(request, "token", required=True)

(
token_valid,
token_stale,
expiration_ts,
) = await self.account_activity_handler.renew_account(
renewal_token.decode("utf8")
)
) = await self.account_activity_handler.renew_account(renewal_token)

if token_valid:
status_code = 200
Expand All @@ -77,11 +76,7 @@ async def on_GET(self, request):
class AccountValiditySendMailServlet(RestServlet):
PATTERNS = client_patterns("/account_validity/send_mail$")

def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer): server
"""
def __init__(self, hs: "HomeServer"):
super().__init__()

self.hs = hs
Expand All @@ -91,14 +86,14 @@ def __init__(self, hs):
hs.config.account_validity.account_validity_renew_by_email_enabled
)

async def on_POST(self, request):
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
requester = await self.auth.get_user_by_req(request, allow_expired=True)
user_id = requester.user.to_string()
await self.account_activity_handler.send_renewal_email_to_user(user_id)

return 200, {}


def register_servlets(hs, http_server):
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
AccountValidityRenewServlet(hs).register(http_server)
AccountValiditySendMailServlet(hs).register(http_server)