Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Nov 21, 2024
1 parent c45cec9 commit 106e7ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 8 additions & 0 deletions roc/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ def get_result(self) -> dict | None:

def get_error(self) -> dict | None:
return self.error


def make_response(data: dict) -> Response:
for key in ["id", "result", "error"]:
if key not in data:
data[key] = None

return Response(data["id"], data["result"], data["error"])
12 changes: 2 additions & 10 deletions roc/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from roc.packet import Packet
from roc.request import Request
from roc.request import Response
from roc.request import make_response


class SocketException(Exception):
Expand Down Expand Up @@ -89,16 +90,7 @@ async def request(self, request: Request) -> Response:

data = json.loads(res)

if "id" not in data:
data["id"] = None

if "result" not in data:
data["result"] = None

if "error" not in data:
data["error"] = None

return Response(data["id"], data["result"], data["error"])
return make_response(data)

async def start(self):
reader, writer = await asyncio.open_connection(self.host, self.port)
Expand Down

0 comments on commit 106e7ac

Please sign in to comment.