Skip to content

Fix python-arango Client (de)serialization #264

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

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
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
28 changes: 26 additions & 2 deletions arango/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@
)


def default_serializer(x: Any) -> str:
"""
Default JSON serializer

:param x: A JSON data type object to serialize
:type x: Any
:return: The object serialized as a JSON string
:rtype: str
"""
return dumps(x)


def default_deserializer(x: str) -> Any:
"""
Default JSON de-serializer

:param x: A JSON string to deserialize
:type x: str
:return: The de-serialized JSON object
:rtype: Any
"""
return loads(x)


class ArangoClient:
"""ArangoDB client.

Expand Down Expand Up @@ -67,8 +91,8 @@ def __init__(
host_resolver: str = "roundrobin",
resolver_max_tries: Optional[int] = None,
http_client: Optional[HTTPClient] = None,
serializer: Callable[..., str] = lambda x: dumps(x),
deserializer: Callable[[str], Any] = lambda x: loads(x),
serializer: Callable[..., str] = default_serializer,
deserializer: Callable[[str], Any] = default_deserializer,
verify_override: Union[bool, str, None] = None,
request_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
) -> None:
Expand Down
10 changes: 10 additions & 0 deletions arango/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class DefaultHTTPAdapter(HTTPAdapter):
:type kwargs: Any
"""

__attrs__ = [
"max_retries",
"config",
"_connection_timeout",
"_pool_connections",
"_pool_maxsize",
"_pool_timeout",
"_pool_block",
]

def __init__(
self,
connection_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import pickle
from typing import Union

import importlib_metadata
Expand Down Expand Up @@ -180,3 +181,10 @@ def assert_verify(
assert_verify("test", False, False)
assert_verify("test", False, False)
assert_verify("test", "foo", "foo")


def test_can_serialize_deserialize_client() -> None:
client = ArangoClient(hosts="http://127.0.0.1:8529")
client_pstr = pickle.dumps(client)
client2 = pickle.loads(client_pstr)
assert len(client2._sessions) > 0