Skip to content

Commit 968257f

Browse files
committed
Adds dict access to all responses
1 parent 0c0447c commit 968257f

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

algoliasearch/responses.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22

3-
from typing import List
3+
from typing import List, Union
44

55
from algoliasearch.exceptions import RequestException
66
from algoliasearch.helpers import get_items
@@ -26,6 +26,12 @@ def wait(self):
2626

2727
pass # pragma: no cover
2828

29+
@abc.abstractmethod
30+
def __getitem__(self, key):
31+
# type:(Union[int, str]) -> Union[int, str, dict]
32+
33+
pass # pragma: no cover
34+
2935

3036
class IndexingResponse(Response):
3137

@@ -48,6 +54,11 @@ def wait(self):
4854

4955
return self
5056

57+
def __getitem__(self, key):
58+
# type:(Union[int, str]) -> Union[int, str, dict]
59+
60+
return self.raw_responses[key]
61+
5162

5263
class MultipleResponse(Response):
5364

@@ -57,6 +68,9 @@ def __init__(self, responses=None):
5768
self.responses = [] if responses is None else responses
5869
self._waitable = list(self.responses)
5970

71+
def __repr__(self):
72+
return self.responses
73+
6074
def push(self, response):
6175
# type: (Response) -> None
6276

@@ -75,6 +89,11 @@ def wait(self):
7589

7690
return self
7791

92+
def __getitem__(self, key):
93+
# type:(Union[int, str]) -> Union[int, str, dict]
94+
95+
return self.responses[key]
96+
7897

7998
class AddApiKeyResponse(Response):
8099

@@ -98,6 +117,11 @@ def wait(self):
98117

99118
return self
100119

120+
def __getitem__(self, key):
121+
# type:(Union[int, str]) -> Union[int, str, dict]
122+
123+
return self.raw_response[key]
124+
101125

102126
class UpdateApiKeyResponse(Response):
103127

@@ -114,7 +138,8 @@ def wait(self):
114138

115139
while not self._done:
116140
api_key = self._client._sync().get_api_key(
117-
self.raw_response['key']
141+
self.raw_response['key'],
142+
self._request_options
118143
)
119144

120145
if self._have_changed(api_key):
@@ -136,6 +161,11 @@ def _have_changed(self, api_key):
136161
return any([(valid_key in body and body[valid_key] == api_key.get(
137162
valid_key)) for valid_key in valid_keys])
138163

164+
def __getitem__(self, key):
165+
# type:(Union[int, str]) -> Union[int, str, dict]
166+
167+
return self.raw_response[key]
168+
139169

140170
class DeleteApiKeyResponse(Response):
141171

@@ -158,6 +188,11 @@ def wait(self):
158188

159189
return self
160190

191+
def __getitem__(self, key):
192+
# type:(Union[int, str]) -> Union[int, str, dict]
193+
194+
return self.raw_response[key]
195+
161196

162197
class RestoreApiKeyResponse(Response):
163198

@@ -182,6 +217,11 @@ def wait(self):
182217

183218
return self
184219

220+
def __getitem__(self, key):
221+
# type:(Union[int, str]) -> Union[int, str, dict]
222+
223+
return self.raw_response[key]
224+
185225

186226
class MultipleIndexBatchIndexingResponse(Response):
187227

@@ -201,3 +241,8 @@ def wait(self):
201241
self._done = True
202242

203243
return self
244+
245+
def __getitem__(self, key):
246+
# type:(Union[int, str]) -> Union[int, str, dict]
247+
248+
return self.raw_response[key]

tests/unit/test_responses.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import mock
44

55
from algoliasearch.exceptions import RequestException
6-
from algoliasearch.responses import IndexingResponse
6+
from algoliasearch.responses import (
7+
IndexingResponse, MultipleResponse,
8+
AddApiKeyResponse, UpdateApiKeyResponse,
9+
DeleteApiKeyResponse, MultipleIndexBatchIndexingResponse
10+
)
711
from algoliasearch.search_client import SearchClient
812

913

@@ -18,9 +22,26 @@ def test_dict_access(self):
1822
'foo': 'bar',
1923
}
2024

21-
index = self.client.init_index('foo')
22-
response = IndexingResponse(index, [response])
23-
self.assertEqual(response.raw_responses[0]['foo'], 'bar')
25+
response_object = IndexingResponse({}, [response])
26+
self.assertEqual(response_object[0]['foo'], 'bar')
27+
28+
response_object = MultipleResponse([response])
29+
self.assertEqual(response_object[0]['foo'], 'bar')
30+
31+
response_object = MultipleResponse([IndexingResponse({}, [response])])
32+
self.assertEqual(response_object[0][0]['foo'], 'bar')
33+
34+
response_object = AddApiKeyResponse({}, response)
35+
self.assertEqual(response_object['foo'], 'bar')
36+
37+
response_object = UpdateApiKeyResponse({}, response, {})
38+
self.assertEqual(response_object['foo'], 'bar')
39+
40+
response_object = DeleteApiKeyResponse({}, response, '')
41+
self.assertEqual(response_object['foo'], 'bar')
42+
43+
response_object = MultipleIndexBatchIndexingResponse({}, response)
44+
self.assertEqual(response_object['foo'], 'bar')
2445

2546
def test_request_exception(self):
2647
self.client._transporter.write.side_effect = RequestException('', 300)

0 commit comments

Comments
 (0)