Skip to content

Commit afd24a5

Browse files
committed
Allow failed request to make multiple attempts.
1 parent 58fe7b9 commit afd24a5

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/recnetpy/rest/request.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Request(ThreadTask[Response]):
2222
client: ClientSession
2323
url: str
2424
method: str
25+
attempts: int
2526
params: Optional[Dict]
2627
body: Optional[Dict]
2728
headers: Optional[Dict]
@@ -42,9 +43,26 @@ async def run(self) -> Response:
4243
4344
@return: A response object containing the fetched data.
4445
"""
45-
async with self.client.request(self.method, self.url, data = self.body, params = self.params, headers = self.headers) as response:
46-
data = await parse_response(response)
47-
return Response(response.status, response.ok, data)
46+
self.attempts = 0
47+
return await self.make_request()
48+
49+
async def make_request(self) -> Response:
50+
"""
51+
This functions attempts to make a request. If an error is
52+
encountered the request will be attempted again up to three
53+
times. Successful attempts will return the requested data as
54+
a response object.
55+
56+
@return: A response object containing the fetched data.
57+
"""
58+
try:
59+
async with self.client.request(self.method, self.url, data = self.body, params = self.params, headers = self.headers) as response:
60+
data = await parse_response(response)
61+
return Response(response.status, response.ok, data)
62+
except Exception as e:
63+
self.attempts += 1
64+
if self.attempts <= 3: return await self.make_request()
65+
raise e
4866

4967
@property
5068
def bucket(self) -> str:

0 commit comments

Comments
 (0)