Skip to content

Commit

Permalink
Implement full compatibility for Python 3.7 (#9)
Browse files Browse the repository at this point in the history
* fix: replace TypedDict with dataclass for py3.7 compatibility

* fix: response.get("error") -> response.error

* fix: TypedDict notation -> dataclass

* fix: error not being optional

Co-authored-by: tsunyoku <[email protected]>
  • Loading branch information
tsunyoku and tsunyoku authored Sep 18, 2022
1 parent a15662c commit c2e4ada
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
31 changes: 16 additions & 15 deletions surrealdb/clients/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import annotations

import asyncio
import dataclasses
from types import TracebackType
from typing import Any
from typing import Dict
Expand Down Expand Up @@ -83,7 +84,7 @@ async def connect(self) -> None:
await self.authenticate(self._token)

if self._username is not None and self._password is not None:
await self.sign_in(username=self._username, password=self._password)
await self.signin(username=self._username, password=self._password)

if self._namespace is not None and self._database is not None:
await self.use(self._namespace, self._database)
Expand All @@ -107,11 +108,12 @@ async def _receive_task(self) -> None:
if msg.type == WSMsgType.ERROR:
raise SurrealWebsocketException(msg.data)

response: RPCResponse = jsonlib.loads(msg.data)
if response.get("error") is not None:
raise SurrealWebsocketException(response["error"]["message"])
json_response = jsonlib.loads(msg.data)
response = RPCResponse(**json_response)
if response.error is not None:
raise SurrealWebsocketException(response.error.message)

self._responses[response["id"]] = response
self._responses[response.id] = response

async def _wait_response(self, id: str) -> RPCResponse:
while id not in self._responses:
Expand All @@ -125,16 +127,15 @@ async def _send(
method: str,
*params: Any,
) -> Any:
request: RPCRequest = {
"id": generate_id(length=16),
"method": method,
"params": params,
}

await self._ws.send_json(request)

response = await self._wait_response(request["id"])
return response["result"]
request = RPCRequest(
id=generate_id(length=16),
method=method,
params=params,
)
await self._ws.send_json(dataclasses.asdict(request))

response = await self._wait_response(request.id)
return response.result

async def ping(self) -> bool:
response = await self._send("ping")
Expand Down
5 changes: 3 additions & 2 deletions surrealdb/models/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from dataclasses import dataclass
from typing import Optional
from typing import TypedDict

__all__ = ("RPCError",)


class RPCError(TypedDict):
@dataclass(frozen=True)
class RPCError:
code: int
message: Optional[str]
5 changes: 3 additions & 2 deletions surrealdb/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
from dataclasses import dataclass
from typing import Any
from typing import List
from typing import TypedDict

__all__ = ("RPCRequest",)


class RPCRequest(TypedDict):
@dataclass(frozen=True)
class RPCRequest:
id: str
method: str
params: List[Any]
6 changes: 3 additions & 3 deletions surrealdb/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from typing import Dict
from typing import List
from typing import Optional
from typing import TypedDict

from .error import RPCError

Expand All @@ -35,7 +34,8 @@ class SurrealResponse:
result: List[Dict[str, Any]]


class RPCResponse(TypedDict):
@dataclass(frozen=True)
class RPCResponse:
id: str
error: Optional[RPCError]
result: Any
error: Optional[RPCError] = None

0 comments on commit c2e4ada

Please sign in to comment.