Skip to content

Commit 5c4fad7

Browse files
committed
Adds sleep method to ApiKeys responses
1 parent 6687038 commit 5c4fad7

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

algoliasearch/helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
2+
import math
23
import sys
4+
import time
35

46
from typing import Optional, Iterable, List, Union, Iterator, Dict, Any
57

@@ -68,3 +70,10 @@ def is_async_available():
6870
pass
6971

7072
return False
73+
74+
75+
def sleep_for(retries_count, before_retry):
76+
# type: (int, int) -> None
77+
78+
factor = math.ceil(retries_count / 10)
79+
time.sleep(factor * before_retry / 1000000.0)

algoliasearch/responses.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import abc
2+
import math
3+
import time
24

35
from typing import List, Union
46

57
from algoliasearch.exceptions import RequestException
6-
from algoliasearch.helpers import get_items
8+
from algoliasearch.helpers import get_items, sleep_for
79
from algoliasearch.http.request_options import RequestOptions
810

911
try:
@@ -98,13 +100,20 @@ def __init__(self, client, raw_response):
98100
def wait(self):
99101
# type: () -> AddApiKeyResponse
100102

103+
retries_count = 1
104+
101105
while not self._done:
102106
try:
103107
self._client._sync().get_api_key(self.raw_response['key'])
104108
self._done = True
105109
except RequestException as e:
106110
if e.status_code != 404:
107111
raise e
112+
retries_count += 1
113+
sleep_for(
114+
retries_count,
115+
self._client._config.wait_task_time_before_retry
116+
)
108117

109118
return self
110119

@@ -127,14 +136,22 @@ def __init__(self, client, raw_response, request_options):
127136
def wait(self):
128137
# type: () -> UpdateApiKeyResponse
129138

139+
retries_count = 1
140+
130141
while not self._done:
131142
api_key = self._client._sync().get_api_key(
132143
self.raw_response['key'],
133144
self._request_options
134145
)
135146

136-
if self._have_changed(api_key):
137-
break
147+
self._done = self._have_changed(api_key)
148+
149+
if not self._done:
150+
retries_count += 1
151+
sleep_for(
152+
retries_count,
153+
self._client._config.wait_task_time_before_retry
154+
)
138155

139156
return self
140157

@@ -171,12 +188,21 @@ def __init__(self, client, raw_response, key):
171188
def wait(self):
172189
# type: () -> DeleteApiKeyResponse
173190

191+
retries_count = 1
192+
174193
while not self._done:
175194
try:
176195
self._client._sync().get_api_key(self._key)
177196
except RequestException as e:
178197
self._done = e.status_code == 404
179198

199+
if not self._done:
200+
retries_count += 1
201+
sleep_for(
202+
retries_count,
203+
self._client._config.wait_task_time_before_retry
204+
)
205+
180206
return self
181207

182208
def __getitem__(self, key):
@@ -198,13 +224,20 @@ def __init__(self, client, raw_response, key):
198224
def wait(self):
199225
# type: () -> RestoreApiKeyResponse
200226

227+
retries_count = 1
228+
201229
while not self._done:
202230
try:
203231
self._client._sync().get_api_key(self._key)
204232
self._done = True
205233
except RequestException as e:
206234
if e.status_code != 404:
207235
raise e
236+
retries_count += 1
237+
sleep_for(
238+
retries_count,
239+
self._client._config.wait_task_time_before_retry
240+
)
208241

209242
return self
210243

0 commit comments

Comments
 (0)