storage: Migrate to typed_endpoint.

This commit is contained in:
Kenneth Rodrigues 2024-06-06 18:08:28 +05:30 committed by Tim Abbott
parent 199d641fae
commit 07d0854ed3
2 changed files with 13 additions and 10 deletions

View File

@ -299,7 +299,7 @@ class TestServiceBotStateHandler(ZulipTestCase):
"keys": ["This is a list, but should be a serialized string."],
}
result = self.client_get("/json/bot_storage", invalid_params)
self.assert_json_error(result, 'Argument "keys" is not valid JSON.')
self.assert_json_error(result, "keys is not valid JSON")
params = {
"keys": orjson.dumps(["key 1", "nonexistent key"]).decode(),
@ -311,7 +311,7 @@ class TestServiceBotStateHandler(ZulipTestCase):
"storage": orjson.dumps({"foo": [1, 2, 3]}).decode(),
}
result = self.client_put("/json/bot_storage", params)
self.assert_json_error(result, "storage contains a value that is not a string")
self.assert_json_error(result, 'storage["foo"] is not a string')
# Remove some entries.
keys_to_remove = ["key 1", "key 2"]

View File

@ -1,6 +1,7 @@
from typing import Dict, List, Optional
from django.http import HttpRequest, HttpResponse
from pydantic import Json
from zerver.lib.bot_storage import (
StateError,
@ -10,17 +11,17 @@ from zerver.lib.bot_storage import (
set_bot_storage,
)
from zerver.lib.exceptions import JsonableError
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
from zerver.lib.validator import check_dict, check_list, check_string
from zerver.lib.typed_endpoint import typed_endpoint
from zerver.models import UserProfile
@has_request_variables
@typed_endpoint
def update_storage(
request: HttpRequest,
user_profile: UserProfile,
storage: Dict[str, str] = REQ(json_validator=check_dict([], value_validator=check_string)),
*,
storage: Json[Dict[str, str]],
) -> HttpResponse:
try:
set_bot_storage(user_profile, list(storage.items()))
@ -29,11 +30,12 @@ def update_storage(
return json_success(request)
@has_request_variables
@typed_endpoint
def get_storage(
request: HttpRequest,
user_profile: UserProfile,
keys: Optional[List[str]] = REQ(json_validator=check_list(check_string), default=None),
*,
keys: Json[Optional[List[str]]] = None,
) -> HttpResponse:
if keys is None:
keys = get_keys_in_bot_storage(user_profile)
@ -44,11 +46,12 @@ def get_storage(
return json_success(request, data={"storage": storage})
@has_request_variables
@typed_endpoint
def remove_storage(
request: HttpRequest,
user_profile: UserProfile,
keys: Optional[List[str]] = REQ(json_validator=check_list(check_string), default=None),
*,
keys: Json[Optional[List[str]]] = None,
) -> HttpResponse:
if keys is None:
keys = get_keys_in_bot_storage(user_profile)