smoothapi-py 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/.gitignore +1 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/PKG-INFO +36 -9
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/README.md +35 -8
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/pyproject.toml +1 -1
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/smooth_api/__init__.py +9 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/LICENSE +0 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/smooth_api/config.py +0 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/smooth_api/state.py +0 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/smooth_api/utils.py +0 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/tests/conftest.py +0 -0
- {smoothapi_py-0.1.0 → smoothapi_py-0.2.0}/tests/test_resilience.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: smoothapi-py
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: API resilience library — exponential backoff and circuit breaker
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -34,7 +34,41 @@ pip install smoothapi-py
|
|
|
34
34
|
|
|
35
35
|
## Usage
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
### Basic Usage (Defaults)
|
|
38
|
+
|
|
39
|
+
If you don't need custom configurations, you can instantiate the decorator with its defaults by passing an empty config object.
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import requests
|
|
43
|
+
from smooth_api import resilient_api, ResilientConfig
|
|
44
|
+
|
|
45
|
+
# Create it with default settings
|
|
46
|
+
config = ResilientConfig()
|
|
47
|
+
|
|
48
|
+
@resilient_api(config)
|
|
49
|
+
def get_user_data(user_id: str):
|
|
50
|
+
res = requests.get(f"https://api.example.com/users/{user_id}")
|
|
51
|
+
res.raise_for_status() # Always raise so the decorator knows it failed!
|
|
52
|
+
return res.json()
|
|
53
|
+
|
|
54
|
+
# Standard usage
|
|
55
|
+
try:
|
|
56
|
+
data = get_user_data("123")
|
|
57
|
+
print(data)
|
|
58
|
+
except Exception as e:
|
|
59
|
+
print("Request failed completely:", e)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**Default Settings provided automatically:**
|
|
63
|
+
- **Retries**: 3 attempts
|
|
64
|
+
- **Backoff Base Delay**: 0.1 seconds (100 milliseconds)
|
|
65
|
+
- **Circuit Failure Threshold**: Trips after 3 consecutive failures
|
|
66
|
+
- **Circuit Cooldown**: Stays open for 10 seconds before probing
|
|
67
|
+
- **Status Codes to Retry**: `429`, `500`, `502`, `503`, and `504`
|
|
68
|
+
|
|
69
|
+
### Advanced Usage (Custom Settings)
|
|
70
|
+
|
|
71
|
+
You can override any of the defaults to suit your application's needs, such as adding a fallback object.
|
|
38
72
|
|
|
39
73
|
```python
|
|
40
74
|
from smooth_api import resilient_api, ResilientConfig
|
|
@@ -63,13 +97,6 @@ def get_user_data(user_id: str):
|
|
|
63
97
|
res.raise_for_status()
|
|
64
98
|
return res.json()
|
|
65
99
|
|
|
66
|
-
# Standard usage
|
|
67
|
-
try:
|
|
68
|
-
data = get_user_data("123")
|
|
69
|
-
print(data)
|
|
70
|
-
except Exception as e:
|
|
71
|
-
print("Request failed completely:", e)
|
|
72
|
-
|
|
73
100
|
# You can also override the fallback at runtime per-call:
|
|
74
101
|
data = get_user_data("456", fallback={"status": "override"})
|
|
75
102
|
```
|
|
@@ -19,7 +19,41 @@ pip install smoothapi-py
|
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
### Basic Usage (Defaults)
|
|
23
|
+
|
|
24
|
+
If you don't need custom configurations, you can instantiate the decorator with its defaults by passing an empty config object.
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
import requests
|
|
28
|
+
from smooth_api import resilient_api, ResilientConfig
|
|
29
|
+
|
|
30
|
+
# Create it with default settings
|
|
31
|
+
config = ResilientConfig()
|
|
32
|
+
|
|
33
|
+
@resilient_api(config)
|
|
34
|
+
def get_user_data(user_id: str):
|
|
35
|
+
res = requests.get(f"https://api.example.com/users/{user_id}")
|
|
36
|
+
res.raise_for_status() # Always raise so the decorator knows it failed!
|
|
37
|
+
return res.json()
|
|
38
|
+
|
|
39
|
+
# Standard usage
|
|
40
|
+
try:
|
|
41
|
+
data = get_user_data("123")
|
|
42
|
+
print(data)
|
|
43
|
+
except Exception as e:
|
|
44
|
+
print("Request failed completely:", e)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Default Settings provided automatically:**
|
|
48
|
+
- **Retries**: 3 attempts
|
|
49
|
+
- **Backoff Base Delay**: 0.1 seconds (100 milliseconds)
|
|
50
|
+
- **Circuit Failure Threshold**: Trips after 3 consecutive failures
|
|
51
|
+
- **Circuit Cooldown**: Stays open for 10 seconds before probing
|
|
52
|
+
- **Status Codes to Retry**: `429`, `500`, `502`, `503`, and `504`
|
|
53
|
+
|
|
54
|
+
### Advanced Usage (Custom Settings)
|
|
55
|
+
|
|
56
|
+
You can override any of the defaults to suit your application's needs, such as adding a fallback object.
|
|
23
57
|
|
|
24
58
|
```python
|
|
25
59
|
from smooth_api import resilient_api, ResilientConfig
|
|
@@ -48,13 +82,6 @@ def get_user_data(user_id: str):
|
|
|
48
82
|
res.raise_for_status()
|
|
49
83
|
return res.json()
|
|
50
84
|
|
|
51
|
-
# Standard usage
|
|
52
|
-
try:
|
|
53
|
-
data = get_user_data("123")
|
|
54
|
-
print(data)
|
|
55
|
-
except Exception as e:
|
|
56
|
-
print("Request failed completely:", e)
|
|
57
|
-
|
|
58
85
|
# You can also override the fallback at runtime per-call:
|
|
59
86
|
data = get_user_data("456", fallback={"status": "override"})
|
|
60
87
|
```
|
|
@@ -63,6 +63,11 @@ def resilient_api(config: ResilientConfig):
|
|
|
63
63
|
last_err = err
|
|
64
64
|
if attempt < config.backoff.max_retries:
|
|
65
65
|
await asyncio.sleep(calculate_backoff(attempt, config.backoff))
|
|
66
|
+
continue
|
|
67
|
+
|
|
68
|
+
# If retries are exhausted and it's an HTTP error, return the response instead of raising
|
|
69
|
+
if status is not None and hasattr(err, 'response'):
|
|
70
|
+
return err.response
|
|
66
71
|
|
|
67
72
|
raise last_err # type: ignore[misc]
|
|
68
73
|
|
|
@@ -93,6 +98,10 @@ def resilient_api(config: ResilientConfig):
|
|
|
93
98
|
last_err = err
|
|
94
99
|
if attempt < config.backoff.max_retries:
|
|
95
100
|
sleep_backoff(calculate_backoff(attempt, config.backoff))
|
|
101
|
+
continue
|
|
102
|
+
|
|
103
|
+
if status is not None and hasattr(err, 'response'):
|
|
104
|
+
return err.response
|
|
96
105
|
|
|
97
106
|
raise last_err # type: ignore[misc]
|
|
98
107
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|