1
+ import logging
2
+
1
3
import backoff
2
4
import requests
3
5
from requests .exceptions import HTTPError
4
- import logging
5
6
6
7
7
8
# Function to log retry attempts
@@ -15,19 +16,14 @@ def __init__(self, backoff_max=30):
15
16
self .backoff_max = backoff_max
16
17
17
18
# GET request method with exponential backoff
18
- @backoff .on_exception (
19
- backoff .expo ,
20
- HTTPError ,
21
- max_time = 30 ,
22
- on_backoff = log_retry
23
- )
19
+ @backoff .on_exception (backoff .expo , HTTPError , max_time = 30 , on_backoff = log_retry )
24
20
def get (self , url , * args , ** kwargs ):
25
21
"""
26
22
Sends a GET request to the specified URL with optional extra arguments.
27
23
28
- This method is a thin wrapper around `requests.get()`. Any additional arguments
29
- are passed directly to `requests.get()`. For more information on the available
30
- arguments, refer to the `requests.get()` documentation:
24
+ This method is a thin wrapper around `requests.get()`. Any additional arguments
25
+ are passed directly to `requests.get()`. For more information on the available
26
+ arguments, refer to the `requests.get()` documentation:
31
27
https://docs.python-requests.org/en/latest/api/#requests.get
32
28
33
29
Args:
@@ -40,22 +36,22 @@ def get(self, url, *args, **kwargs):
40
36
Raises:
41
37
HTTPError: If the request fails for a network-related reason.
42
38
"""
43
- return self ._handle_request (' GET' , url , * args , ** kwargs )
39
+ return self ._handle_request (" GET" , url , * args , ** kwargs )
44
40
45
41
# POST request method with exponential backoff
46
42
@backoff .on_exception (
47
- backoff .expo ,
43
+ backoff .expo ,
48
44
HTTPError ,
49
- max_time = 30 ,
45
+ max_time = 30 ,
50
46
on_backoff = log_retry ,
51
47
)
52
48
def post (self , url , * args , ** kwargs ):
53
49
"""
54
50
Sends a POST request to the specified URL with optional extra arguments.
55
51
56
- This method is a thin wrapper around `requests.post()`. Any additional arguments
57
- are passed directly to `requests.post()`. For more information on the available
58
- arguments, refer to the `requests.post()` documentation:
52
+ This method is a thin wrapper around `requests.post()`. Any additional arguments
53
+ are passed directly to `requests.post()`. For more information on the available
54
+ arguments, refer to the `requests.post()` documentation:
59
55
https://docs.python-requests.org/en/latest/api/#requests.post
60
56
61
57
Args:
@@ -69,8 +65,8 @@ def post(self, url, *args, **kwargs):
69
65
Raises:
70
66
HTTPError: If the request fails for a network-related reason.
71
67
"""
72
- return self ._handle_request (' POST' , url , * args , ** kwargs )
73
-
68
+ return self ._handle_request (" POST" , url , * args , ** kwargs )
69
+
74
70
# Method to handle requests for GET and POST
75
71
def _handle_request (self , method , url , * args , ** kwargs ):
76
72
logging .info (f"Sending { method } request to { url } " )
@@ -80,8 +76,12 @@ def _handle_request(self, method, url, *args, **kwargs):
80
76
return response
81
77
82
78
except HTTPError as http_err :
83
- logging .error (f"HTTP error occurred: { http_err } when sending a { method } to { url } with headers { kwargs .get ('headers' )} " )
79
+ logging .error (
80
+ f"HTTP error occurred: { http_err } when sending a { method } to { url } with headers { kwargs .get ('headers' )} "
81
+ )
84
82
raise http_err
85
83
except Exception as err :
86
- logging .error (f"Other error occurred: { err } when sending a { method } to { url } with headers { kwargs .get ('headers' )} " )
84
+ logging .error (
85
+ f"Other error occurred: { err } when sending a { method } to { url } with headers { kwargs .get ('headers' )} "
86
+ )
87
87
raise err
0 commit comments