Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: Add JSONEncoder parameter #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/ucall/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ class Client:
"""JSON-RPC Client that uses classic sync Python `requests` to pass JSON calls over HTTP"""

def __init__(
self, uri: str = "127.0.0.1", port: int = 8545, use_http: bool = True
self, uri: str = "127.0.0.1", port: int = 8545, use_http: bool = True, jsonencoder: type = json.JSONEncoder
) -> None:
self.uri = uri
self.port = port
self.use_http = use_http
self.jsonencoder = jsonencoder
self.sock = None
self.http_template = f"POST / HTTP/1.1\r\nHost: {uri}:{port}\r\nUser-Agent: py-ucall\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: %i\r\nContent-Type: application/json\r\n\r\n"

Expand Down Expand Up @@ -169,7 +170,7 @@ def _socket_is_closed(self) -> bool:
def _send(self, json_data: dict):
json_data["id"] = random.randint(1, 2**16)
req_obj = Request(json_data)
request = json.dumps(req_obj.packed)
request = json.dumps(req_obj.packed, cls=self.jsonencoder)
if self.use_http:
request = self.http_template % (len(request)) + request

Expand All @@ -194,9 +195,10 @@ def __init__(
ssl_context: ssl.SSLContext = None,
allow_self_signed: bool = False,
enable_session_resumption: bool = True,
jsonencoder: type = json.JSONEncoder,
) -> None:

super().__init__(uri, port, use_http=True)
super().__init__(uri, port, use_http=True, jsonencoder=jsonencoder)

if ssl_context is None:
ssl_context = ssl.create_default_context()
Expand Down