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

Commit

Permalink
Add type hints to device and event report admin API (#9519)
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel authored Mar 2, 2021
1 parent ad8589d commit f00c4e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.d/9519.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to device and event report admin API.
33 changes: 24 additions & 9 deletions synapse/rest/admin/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
# 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 NotFoundError, SynapseError
from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
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.types import UserID
from synapse.types import JsonDict, UserID

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)

Expand All @@ -35,14 +40,16 @@ class DeviceRestServlet(RestServlet):
"/users/(?P<user_id>[^/]*)/devices/(?P<device_id>[^/]*)$", "v2"
)

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
super().__init__()
self.hs = hs
self.auth = hs.get_auth()
self.device_handler = hs.get_device_handler()
self.store = hs.get_datastore()

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

target_user = UserID.from_string(user_id)
Expand All @@ -58,7 +65,9 @@ async def on_GET(self, request, user_id, device_id):
)
return 200, device

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

target_user = UserID.from_string(user_id)
Expand All @@ -72,7 +81,9 @@ async def on_DELETE(self, request, user_id, device_id):
await self.device_handler.delete_device(target_user.to_string(), device_id)
return 200, {}

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

target_user = UserID.from_string(user_id)
Expand All @@ -97,7 +108,7 @@ class DevicesRestServlet(RestServlet):

PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/devices$", "v2")

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
"""
Args:
hs (synapse.server.HomeServer): server
Expand All @@ -107,7 +118,9 @@ def __init__(self, hs):
self.device_handler = hs.get_device_handler()
self.store = hs.get_datastore()

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

target_user = UserID.from_string(user_id)
Expand All @@ -130,13 +143,15 @@ class DeleteDevicesRestServlet(RestServlet):

PATTERNS = admin_patterns("/users/(?P<user_id>[^/]*)/delete_devices$", "v2")

def __init__(self, hs):
def __init__(self, hs: "HomeServer"):
self.hs = hs
self.auth = hs.get_auth()
self.device_handler = hs.get_device_handler()
self.store = hs.get_datastore()

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

target_user = UserID.from_string(user_id)
Expand Down
22 changes: 15 additions & 7 deletions synapse/rest/admin/event_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
# limitations under the License.

import logging
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import Codes, NotFoundError, SynapseError
from synapse.http.servlet import RestServlet, parse_integer, parse_string
from synapse.http.site import SynapseRequest
from synapse.rest.admin._base import admin_patterns, assert_requester_is_admin
from synapse.types import JsonDict

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,12 +51,12 @@ class EventReportsRestServlet(RestServlet):

PATTERNS = admin_patterns("/event_reports$")

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

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

start = parse_integer(request, "from", default=0)
Expand Down Expand Up @@ -106,26 +112,28 @@ class EventReportDetailRestServlet(RestServlet):

PATTERNS = admin_patterns("/event_reports/(?P<report_id>[^/]*)$")

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

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

message = (
"The report_id parameter must be a string representing a positive integer."
)
try:
report_id = int(report_id)
resolved_report_id = int(report_id)
except ValueError:
raise SynapseError(400, message, errcode=Codes.INVALID_PARAM)

if report_id < 0:
if resolved_report_id < 0:
raise SynapseError(400, message, errcode=Codes.INVALID_PARAM)

ret = await self.store.get_event_report(report_id)
ret = await self.store.get_event_report(resolved_report_id)
if not ret:
raise NotFoundError("Event report not found")

Expand Down

0 comments on commit f00c4e7

Please sign in to comment.