Skip to content

Commit

Permalink
update README, freeze client
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed May 29, 2024
1 parent 37a2b7a commit 8a5d555
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ assert c.get("name") == "Fabian"
```

> [!NOTE]
> ZnSocket does not decode strings automatically. Using it is equivalent to using `Redis.from_url(storage, decode_responses=True)` in the Redis client.
> ZnSocket does not encode/decode strings. Using it is equivalent to using `Redis.from_url(storage, decode_responses=True)` in the Redis client.

## Lists
Expand Down
15 changes: 10 additions & 5 deletions znsocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
from znsocket import exceptions


@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class Client:
address: str
sio: socketio.SimpleClient = dataclasses.field(default=None, repr=False, init=False)
decode_responses: bool = True
sio: socketio.SimpleClient = dataclasses.field(
default_factory=socketio.SimpleClient, repr=False, init=False
)

@classmethod
def from_url(cls, url):
def from_url(cls, url, **kwargs) -> "Client":
"""Connect to a znsocket server using a URL.
Parameters
Expand All @@ -20,15 +23,17 @@ def from_url(cls, url):
The URL of the znsocket server. Should be in the format
"znsocket://127.0.0.1:5000".
"""
return cls(address=url.replace("znsocket://", "http://"))
return cls(address=url.replace("znsocket://", "http://"), **kwargs)

def __post_init__(self):
self.sio = socketio.SimpleClient()
try:
self.sio.connect(self.address)
except socketio.exceptions.ConnectionError as err:
raise exceptions.ConnectionError(self.address) from err

if not self.decode_responses:
raise NotImplementedError("decode_responses=False is not supported yet")

def delete(self, name):
return self.sio.call("delete", {"name": name})

Expand Down

0 comments on commit 8a5d555

Please sign in to comment.