Skip to content

Commit 5491580

Browse files
iwpndalxwrd
andcommitted
refactor: ♻️ set Tile38 output to JSON on_connect
instead of maintaining a `__format` state that needs to be validated and updated on every state of the connection. Co-authored-by: Alex Ward <[email protected]>
1 parent 4014663 commit 5491580

File tree

1 file changed

+3
-27
lines changed

1 file changed

+3
-27
lines changed

pyle38/client.py

+3-27
Original file line numberDiff line numberDiff line change
@@ -119,38 +119,16 @@ class Format(str, Enum):
119119

120120
class Client:
121121
__redis = None
122-
__format: str = Format.RESP.value
123122

124123
def __init__(self, url: str) -> None:
125124
self.url = url
126125
self.__redis = None
127-
self.__format = Format.RESP.value
128-
129-
async def force_json(self) -> None:
130-
"""Force the OUTPUT to JSON
131-
132-
When a new connection is established
133-
the on_connect callback makes sure to reset the
134-
OUTPUT to RESP.
135-
This method makes sure to enforce the OUTPUT to
136-
JSON on any consecutive command using the connection.
137-
"""
138-
if self.__format == Format.JSON.value:
139-
return
140-
141-
await self.__execute_and_read_response(
142-
Command.OUTPUT.value, [Format.JSON.value]
143-
)
144-
self.__format = Format.JSON.value
145126

146127
async def __on_connect(self, connection: redis.Connection):
147-
"""On connect callback to set OUTPUT to RESP
148-
149-
That way we can keep track of the OUTPUT set
150-
for the connection.
151-
"""
128+
"""On connect callback to set OUTPUT to JSON"""
152129
await connection.on_connect()
153-
self.__format = Format.RESP.value
130+
await connection.send_command(Command.OUTPUT, Format.JSON)
131+
await connection.read_response()
154132

155133
async def __delete_response_callbacks(self):
156134
"""Delete response callbacks in redis-py
@@ -194,7 +172,6 @@ async def __execute_and_read_response(
194172
return await r.execute_command(command, *command_args)
195173

196174
async def command(self, command: str, command_args: CommandArgs = []) -> Dict:
197-
await self.force_json()
198175
response = await self.__execute_and_read_response(command, command_args)
199176

200177
return parse_response(response)
@@ -210,6 +187,5 @@ async def quit(self) -> str:
210187
await c.connection_pool.disconnect()
211188

212189
self.__redis = None
213-
self.__format = Format.RESP.value
214190

215191
return "OK"

0 commit comments

Comments
 (0)