Skip to content

Commit 09e9966

Browse files
Merge pull request #60 from IvanildoBarauna/feature/MaxOffQuantityOfRetries
feature: added max of retries for protect APIServer
2 parents a789e46 + 2c9f68b commit 09e9966

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

README.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,15 @@ client = ClientBuilder(endpoint="https://api.example.com"
5858
,connection_timeout=2
5959
,headers=headers)
6060

61-
"""
62-
NOTE: by default the quantity of retries is 3 and the time between retries is 1 second, but you can define manually.
63-
"""
64-
61+
# if you can define timeout with EXPONENTIAL_RETRY_STRATEGY and set headers:
6562
client = ClientBuilder(endpoint="https://api.example.com"
66-
,retry_strategy=RetryStrategies.LINEAR_RETRY_STRATEGY
63+
,retry_strategy=RetryStrategies.EXPONENTIAL_RETRY_STRATEGY
6764
,connection_timeout=10
6865
,headers=headers
6966
,retries=5
7067
,initial_delay=10)
7168

7269

73-
### timeout, retry_strategy and headers are opcionals parameters
74-
7570
# Get data from the API
7671
data = client.get_api_data()
7772

@@ -80,4 +75,28 @@ df = client.api_to_dataframe(data)
8075

8176
# Display the DataFrame
8277
print(df)
83-
```
78+
```
79+
80+
## Important notes:
81+
* **Opcionals Parameters:** The params timeout, retry_strategy and headers are opcionals.
82+
* **Default Params Value:** By default the quantity of retries is 3 and the time between retries is 1 second, but you can define manually.
83+
* **Max Of Retries:** For security of API Server there is a limit for quantity of retries, actually this value is 5, this value is defined in lib constant. You can inform any value in RETRIES param, but the lib only will try 5x.
84+
* **Exponential Retry Strategy:** The increment of time between retries is time passed in **initial_delay** param * 2 * the retry_number, e.g with initial_delay=2
85+
86+
RetryNumber | WaitingTime
87+
------------ | -----------
88+
2 | 2s
89+
2 | 4s
90+
3 | 6s
91+
4 | 8s
92+
5 | 10s
93+
* **Linear Retry Strategy:** The increment of time between retries is time passed in **initial_delay**
94+
e.g with initial_delay=2
95+
96+
RetryNumber | WaitingTime
97+
------------ | -----------
98+
1 | 2s
99+
2 | 2s
100+
3 | 2s
101+
4 | 2s
102+
5 | 2s

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "api-to-dataframe"
3-
version = "1.3.7"
3+
version = "1.3.10"
44
description = "A package to convert API responses to pandas dataframe"
55
authors = ["IvanildoBarauna <ivanildo.jnr@outlook.com>"]
66
readme = "README.md"

src/api_to_dataframe/models/retainer.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import time
22
from enum import Enum
3+
34
from requests.exceptions import RequestException
45
from api_to_dataframe.utils.logger import log, LogLevel
6+
from api_to_dataframe.utils import Constants
57

68

79
class Strategies(Enum):
@@ -16,7 +18,7 @@ def wrapper(*args, **kwargs): # pylint: disable=inconsistent-return-statements
1618
while retry_number < args[0].retries:
1719
try:
1820
log(
19-
f"Trying for the {retry_number + 1} of {args[0].retries} retries. "
21+
f"Trying for the {retry_number} of {Constants.MAX_OF_RETRIES} retries. "
2022
f"Using {args[0].retry_strategy}",
2123
LogLevel.INFO,
2224
)
@@ -29,9 +31,9 @@ def wrapper(*args, **kwargs): # pylint: disable=inconsistent-return-statements
2931
if args[0].retry_strategy == Strategies.LINEAR_RETRY_STRATEGY:
3032
time.sleep(args[0].delay)
3133
elif args[0].retry_strategy == Strategies.EXPONENTIAL_RETRY_STRATEGY:
32-
time.sleep(args[0].delay * 2**retry_number)
34+
time.sleep(args[0].delay * retry_number)
3335

34-
if retry_number == args[0].retries:
36+
if retry_number in (args[0].retries, Constants.MAX_OF_RETRIES):
3537
log(
3638
f"Failed after {retry_number} retries using {args[0].retry_strategy}",
3739
LogLevel.ERROR,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Constants:
2+
MAX_OF_RETRIES = 5

tests/test_controller_client_builder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22
import pandas as pd
3-
import requests
43

54
from api_to_dataframe import ClientBuilder
65

tests/test_models_retainer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from api_to_dataframe import ClientBuilder, RetryStrategies
66

7+
# from api_to_dataframe.utils import Constants
8+
79

810
def test_linear_strategy():
911
endpoint = "https://api-to-dataframe/"
@@ -60,7 +62,7 @@ def test_exponential_strategy():
6062
client.get_api_data()
6163
except requests.exceptions.RequestException:
6264
end = time.time()
63-
assert end - start >= client.delay * 2**retry_number
65+
assert end - start >= client.delay * retry_number
6466
retry_number += 1
6567

6668
assert retry_number == max_retries

0 commit comments

Comments
 (0)