Skip to content

Commit 744a6e9

Browse files
authored
Merge pull request #264 from ArangoDB-Community/fix-serialization
Fix python-arango Client (de)serialization
2 parents 74680ae + bb2ccce commit 744a6e9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

arango/client.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@
2222
)
2323

2424

25+
def default_serializer(x: Any) -> str:
26+
"""
27+
Default JSON serializer
28+
29+
:param x: A JSON data type object to serialize
30+
:type x: Any
31+
:return: The object serialized as a JSON string
32+
:rtype: str
33+
"""
34+
return dumps(x)
35+
36+
37+
def default_deserializer(x: str) -> Any:
38+
"""
39+
Default JSON de-serializer
40+
41+
:param x: A JSON string to deserialize
42+
:type x: str
43+
:return: The de-serialized JSON object
44+
:rtype: Any
45+
"""
46+
return loads(x)
47+
48+
2549
class ArangoClient:
2650
"""ArangoDB client.
2751
@@ -67,8 +91,8 @@ def __init__(
6791
host_resolver: str = "roundrobin",
6892
resolver_max_tries: Optional[int] = None,
6993
http_client: Optional[HTTPClient] = None,
70-
serializer: Callable[..., str] = lambda x: dumps(x),
71-
deserializer: Callable[[str], Any] = lambda x: loads(x),
94+
serializer: Callable[..., str] = default_serializer,
95+
deserializer: Callable[[str], Any] = default_deserializer,
7296
verify_override: Union[bool, str, None] = None,
7397
request_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,
7498
) -> None:

arango/http.py

+10
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ class DefaultHTTPAdapter(HTTPAdapter):
8484
:type kwargs: Any
8585
"""
8686

87+
__attrs__ = [
88+
"max_retries",
89+
"config",
90+
"_connection_timeout",
91+
"_pool_connections",
92+
"_pool_maxsize",
93+
"_pool_timeout",
94+
"_pool_block",
95+
]
96+
8797
def __init__(
8898
self,
8999
connection_timeout: Union[int, float] = DEFAULT_REQUEST_TIMEOUT,

tests/test_client.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import pickle
23
from typing import Union
34

45
import importlib_metadata
@@ -180,3 +181,10 @@ def assert_verify(
180181
assert_verify("test", False, False)
181182
assert_verify("test", False, False)
182183
assert_verify("test", "foo", "foo")
184+
185+
186+
def test_can_serialize_deserialize_client() -> None:
187+
client = ArangoClient(hosts="http://127.0.0.1:8529")
188+
client_pstr = pickle.dumps(client)
189+
client2 = pickle.loads(client_pstr)
190+
assert len(client2._sessions) > 0

0 commit comments

Comments
 (0)