API refactor
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2025-10-07 16:25:52 +09:00
parent 76d0d86211
commit 91c7e04474
1171 changed files with 81940 additions and 44117 deletions

View File

@@ -3,20 +3,26 @@ from typing import Any, Union
from ..exceptions import ConnectionError, InvalidResponse, ResponseError
from ..typing import EncodableT
from .base import _AsyncRESPBase, _RESPBase
from .base import (
AsyncPushNotificationsParser,
PushNotificationsParser,
_AsyncRESPBase,
_RESPBase,
)
from .socket import SERVER_CLOSED_CONNECTION_ERROR
class _RESP3Parser(_RESPBase):
class _RESP3Parser(_RESPBase, PushNotificationsParser):
"""RESP3 protocol implementation"""
def __init__(self, socket_read_size):
super().__init__(socket_read_size)
self.push_handler_func = self.handle_push_response
self.pubsub_push_handler_func = self.handle_pubsub_push_response
self.invalidation_push_handler_func = None
def handle_push_response(self, response):
def handle_pubsub_push_response(self, response):
logger = getLogger("push_response")
logger.info("Push response: " + str(response))
logger.debug("Push response: " + str(response))
return response
def read_response(self, disable_decoding=False, push_request=False):
@@ -85,19 +91,16 @@ class _RESP3Parser(_RESPBase):
# set response
elif byte == b"~":
# redis can return unhashable types (like dict) in a set,
# so we need to first convert to a list, and then try to convert it to a set
# so we return sets as list, all the time, for predictability
response = [
self._read_response(disable_decoding=disable_decoding)
for _ in range(int(response))
]
try:
response = set(response)
except TypeError:
pass
# map response
elif byte == b"%":
# we use this approach and not dict comprehension here
# because this dict comprehension fails in python 3.7
# We cannot use a dict-comprehension to parse stream.
# Evaluation order of key:val expression in dict comprehension only
# became defined to be left-right in version 3.8
resp_dict = {}
for _ in range(int(response)):
key = self._read_response(disable_decoding=disable_decoding)
@@ -113,13 +116,13 @@ class _RESP3Parser(_RESPBase):
)
for _ in range(int(response))
]
res = self.push_handler_func(response)
response = self.handle_push_response(response)
if not push_request:
return self._read_response(
disable_decoding=disable_decoding, push_request=push_request
)
else:
return res
return response
else:
raise InvalidResponse(f"Protocol Error: {raw!r}")
@@ -127,18 +130,16 @@ class _RESP3Parser(_RESPBase):
response = self.encoder.decode(response)
return response
def set_push_handler(self, push_handler_func):
self.push_handler_func = push_handler_func
class _AsyncRESP3Parser(_AsyncRESPBase):
class _AsyncRESP3Parser(_AsyncRESPBase, AsyncPushNotificationsParser):
def __init__(self, socket_read_size):
super().__init__(socket_read_size)
self.push_handler_func = self.handle_push_response
self.pubsub_push_handler_func = self.handle_pubsub_push_response
self.invalidation_push_handler_func = None
def handle_push_response(self, response):
async def handle_pubsub_push_response(self, response):
logger = getLogger("push_response")
logger.info("Push response: " + str(response))
logger.debug("Push response: " + str(response))
return response
async def read_response(
@@ -214,23 +215,23 @@ class _AsyncRESP3Parser(_AsyncRESPBase):
# set response
elif byte == b"~":
# redis can return unhashable types (like dict) in a set,
# so we need to first convert to a list, and then try to convert it to a set
# so we always convert to a list, to have predictable return types
response = [
(await self._read_response(disable_decoding=disable_decoding))
for _ in range(int(response))
]
try:
response = set(response)
except TypeError:
pass
# map response
elif byte == b"%":
response = {
(await self._read_response(disable_decoding=disable_decoding)): (
await self._read_response(disable_decoding=disable_decoding)
# We cannot use a dict-comprehension to parse stream.
# Evaluation order of key:val expression in dict comprehension only
# became defined to be left-right in version 3.8
resp_dict = {}
for _ in range(int(response)):
key = await self._read_response(disable_decoding=disable_decoding)
resp_dict[key] = await self._read_response(
disable_decoding=disable_decoding, push_request=push_request
)
for _ in range(int(response))
}
response = resp_dict
# push response
elif byte == b">":
response = [
@@ -241,19 +242,16 @@ class _AsyncRESP3Parser(_AsyncRESPBase):
)
for _ in range(int(response))
]
res = self.push_handler_func(response)
response = await self.handle_push_response(response)
if not push_request:
return await self._read_response(
disable_decoding=disable_decoding, push_request=push_request
)
else:
return res
return response
else:
raise InvalidResponse(f"Protocol Error: {raw!r}")
if isinstance(response, bytes) and disable_decoding is False:
response = self.encoder.decode(response)
return response
def set_push_handler(self, push_handler_func):
self.push_handler_func = push_handler_func