Skip to content

Commit f2802c3

Browse files
authored
Support pickling ApiResponse (#100)
1 parent 6d6e64a commit f2802c3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

elastic_transport/_response.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Iterator,
2424
List,
2525
NoReturn,
26+
Tuple,
2627
TypeVar,
2728
Union,
2829
overload,
@@ -97,6 +98,12 @@ def __getitem__(self, item: Any) -> Any:
9798
def __getattr__(self, attr: str) -> Any:
9899
return getattr(self._body, attr)
99100

101+
def __getstate__(self) -> Tuple[_BodyType, ApiResponseMeta]:
102+
return self._body, self._meta
103+
104+
def __setstate__(self, state: Tuple[_BodyType, ApiResponseMeta]) -> None:
105+
self._body, self._meta = state
106+
100107
def __len__(self) -> int:
101108
return len(self._body)
102109

tests/test_response.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import pickle
19+
1820
import pytest
1921

2022
from elastic_transport import (
@@ -139,3 +141,19 @@ def test_constructor_allowed():
139141

140142
resp = ObjectApiResponse(meta=meta, body={}, body_cls=int)
141143
assert resp == {}
144+
145+
146+
@pytest.mark.parametrize(
147+
"response_cls, body",
148+
[
149+
(TextApiResponse, "Hello World"),
150+
(BinaryApiResponse, b"Hello World"),
151+
(ObjectApiResponse, {"Hello": "World"}),
152+
(ListApiResponse, ["Hello", "World"]),
153+
],
154+
)
155+
def test_pickle(response_cls, body):
156+
resp = response_cls(meta=meta, body=body)
157+
pickled_resp = pickle.loads(pickle.dumps(resp))
158+
assert pickled_resp == resp
159+
assert pickled_resp.meta == resp.meta

0 commit comments

Comments
 (0)