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

Commit

Permalink
Add missing type hints to the admin API servlets
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel committed Jun 2, 2021
1 parent 3ff6fe2 commit ce5150d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
43 changes: 22 additions & 21 deletions synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import logging
import platform
from typing import TYPE_CHECKING, Tuple

import synapse
from synapse.api.errors import Codes, NotFoundError, SynapseError
from synapse.http.server import JsonResource
from synapse.http.server import HttpServer, JsonResource
from synapse.http.servlet import RestServlet, parse_json_object_from_request
from synapse.http.site import SynapseRequest
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
from synapse.rest.admin.devices import (
DeleteDevicesRestServlet,
Expand Down Expand Up @@ -66,22 +68,25 @@
UserTokenRestServlet,
WhoisRestServlet,
)
from synapse.types import RoomStreamToken
from synapse.types import JsonDict, RoomStreamToken
from synapse.util.versionstring import get_version_string

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


class VersionServlet(RestServlet):
PATTERNS = admin_patterns("/server_version$")

def __init__(self, hs):
def __init__(self, hs: "HomeServer") -> None:
self.res = {
"server_version": get_version_string(synapse),
"python_version": platform.python_version(),
}

def on_GET(self, request):
def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
return 200, self.res


Expand All @@ -90,17 +95,14 @@ class PurgeHistoryRestServlet(RestServlet):
"/purge_history/(?P<room_id>[^/]*)(/(?P<event_id>[^/]+))?"
)

def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer)
"""
def __init__(self, hs: "HomeServer") -> None:
self.pagination_handler = hs.get_pagination_handler()
self.store = hs.get_datastore()
self.auth = hs.get_auth()

async def on_POST(self, request, room_id, event_id):
async def on_POST(
self, request: SynapseRequest, room_id: str, event_id: str
) -> Tuple[int, JsonDict]:
await assert_requester_is_admin(self.auth, request)

body = parse_json_object_from_request(request, allow_empty_body=True)
Expand Down Expand Up @@ -173,16 +175,13 @@ async def on_POST(self, request, room_id, event_id):
class PurgeHistoryStatusRestServlet(RestServlet):
PATTERNS = admin_patterns("/purge_history_status/(?P<purge_id>[^/]+)")

def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer)
"""
def __init__(self, hs: "HomeServer") -> None:
self.pagination_handler = hs.get_pagination_handler()
self.auth = hs.get_auth()

async def on_GET(self, request, purge_id):
async def on_GET(
self, request: SynapseRequest, purge_id: str
) -> Tuple[int, JsonDict]:
await assert_requester_is_admin(self.auth, request)

purge_status = self.pagination_handler.get_purge_status(purge_id)
Expand All @@ -203,12 +202,12 @@ async def on_GET(self, request, purge_id):
class AdminRestResource(JsonResource):
"""The REST resource which gets mounted at /_synapse/admin"""

def __init__(self, hs):
def __init__(self, hs: "HomeServer") -> None:
JsonResource.__init__(self, hs, canonical_json=False)
register_servlets(hs, self)


def register_servlets(hs, http_server):
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
"""
Register all the admin servlets.
"""
Expand Down Expand Up @@ -242,7 +241,9 @@ def register_servlets(hs, http_server):
RateLimitRestServlet(hs).register(http_server)


def register_servlets_for_client_rest_resource(hs, http_server):
def register_servlets_for_client_rest_resource(
hs: "HomeServer", http_server: HttpServer
) -> None:
"""Register only the servlets which need to be exposed on /_matrix/client/xxx"""
WhoisRestServlet(hs).register(http_server)
PurgeHistoryStatusRestServlet(hs).register(http_server)
Expand Down
3 changes: 2 additions & 1 deletion synapse/rest/admin/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.

import re
from typing import Pattern

from synapse.api.auth import Auth
from synapse.api.errors import AuthError
from synapse.http.site import SynapseRequest
from synapse.types import UserID


def admin_patterns(path_regex: str, version: str = "v1"):
def admin_patterns(path_regex: str, version: str = "v1") -> Pattern[str]:
"""Returns the list of patterns for an admin endpoint
Args:
Expand Down
12 changes: 10 additions & 2 deletions synapse/rest/admin/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import SynapseError
from synapse.http.servlet import RestServlet
from synapse.http.site import SynapseRequest
from synapse.rest.admin._base import admin_patterns, assert_user_is_admin
from synapse.types import JsonDict

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)

Expand All @@ -25,12 +31,14 @@ class DeleteGroupAdminRestServlet(RestServlet):

PATTERNS = admin_patterns("/delete_group/(?P<group_id>[^/]*)")

def __init__(self, hs):
def __init__(self, hs: "HomeServer") -> None:
self.group_server = hs.get_groups_server_handler()
self.is_mine_id = hs.is_mine_id
self.auth = hs.get_auth()

async def on_POST(self, request, group_id):
async def on_POST(
self, request: SynapseRequest, group_id: str
) -> Tuple[int, JsonDict]:
requester = await self.auth.get_user_by_req(request)
await assert_user_is_admin(self.auth, requester.user)

Expand Down
21 changes: 11 additions & 10 deletions synapse/rest/admin/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
from synapse.http.server import HttpServer
from synapse.http.servlet import RestServlet, parse_boolean, parse_integer
from synapse.http.site import SynapseRequest
from synapse.rest.admin._base import (
Expand Down Expand Up @@ -44,7 +45,7 @@ class QuarantineMediaInRoom(RestServlet):
admin_patterns("/quarantine_media/(?P<room_id>[^/]+)")
)

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand All @@ -71,7 +72,7 @@ class QuarantineMediaByUser(RestServlet):

PATTERNS = admin_patterns("/user/(?P<user_id>[^/]+)/media/quarantine")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand Down Expand Up @@ -100,7 +101,7 @@ class QuarantineMediaByID(RestServlet):
"/media/quarantine/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)"
)

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand All @@ -125,7 +126,7 @@ class ProtectMediaByID(RestServlet):

PATTERNS = admin_patterns("/media/protect/(?P<media_id>[^/]+)")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand All @@ -148,7 +149,7 @@ class UnprotectMediaByID(RestServlet):

PATTERNS = admin_patterns("/media/unprotect/(?P<media_id>[^/]+)")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand All @@ -171,7 +172,7 @@ class ListMediaInRoom(RestServlet):

PATTERNS = admin_patterns("/room/(?P<room_id>[^/]+)/media")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()

Expand All @@ -191,7 +192,7 @@ async def on_GET(
class PurgeMediaCacheRestServlet(RestServlet):
PATTERNS = admin_patterns("/purge_media_cache")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.media_repository = hs.get_media_repository()
self.auth = hs.get_auth()

Expand All @@ -211,7 +212,7 @@ class DeleteMediaByID(RestServlet):

PATTERNS = admin_patterns("/media/(?P<server_name>[^/]+)/(?P<media_id>[^/]+)")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.server_name = hs.hostname
Expand Down Expand Up @@ -241,7 +242,7 @@ class DeleteMediaByDateSize(RestServlet):

PATTERNS = admin_patterns("/media/(?P<server_name>[^/]+)/delete")

def __init__(self, hs: "HomeServer"):
def __init__(self, hs: "HomeServer") -> None:
self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.server_name = hs.hostname
Expand Down Expand Up @@ -283,7 +284,7 @@ async def on_POST(
return 200, {"deleted_media": deleted_media, "total": total}


def register_servlets_for_media_repo(hs: "HomeServer", http_server):
def register_servlets_for_media_repo(hs: "HomeServer", http_server: HttpServer) -> None:
"""
Media repo specific APIs.
"""
Expand Down
6 changes: 1 addition & 5 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,7 @@ async def on_POST(
class AccountValidityRenewServlet(RestServlet):
PATTERNS = admin_patterns("/account_validity/validity$")

def __init__(self, hs):
"""
Args:
hs (synapse.server.HomeServer): server
"""
def __init__(self, hs: "HomeServer") -> None:
self.hs = hs
self.account_activity_handler = hs.get_account_validity_handler()
self.auth = hs.get_auth()
Expand Down

0 comments on commit ce5150d

Please sign in to comment.