34
34
APPLICATION_JSON = 'application/json' # default
35
35
36
36
37
- def wait_for_async_api (response , api_timeout , headers = None , timeout = None ):
37
+ def call_finished (location_uri , headers , timeout ):
38
+ """
39
+ :param location_uri: URI to check the status of API call
40
+ :param headers: HTTP headers
41
+ :param timeout: connect timeout
42
+ """
43
+ logger = logging .getLogger (__name__ )
44
+
45
+ logger .debug (f"GET API call: { location_uri } , timeout { timeout } seconds and headers: { headers } " )
46
+ response = requests .get (location_uri , headers = headers , proxies = get_proxies (location_uri ), timeout = timeout )
47
+ if response is None :
48
+ raise Exception ("API call failed" )
49
+
50
+ response .raise_for_status ()
51
+ if response .status_code == 202 :
52
+ return False
53
+ else :
54
+ return True
55
+
56
+
57
+ def wait_for_async_api (response , api_timeout = None , headers = None , timeout = None ):
38
58
"""
39
59
:param response: request
40
- :param api_timeout: asynchronous API timeout
60
+ :param api_timeout: asynchronous API timeout (will wait forever or until error if None)
41
61
:param headers: request headers
42
62
:param timeout: connect timeout
43
63
:return: request
@@ -49,16 +69,16 @@ def wait_for_async_api(response, api_timeout, headers=None, timeout=None):
49
69
raise Exception (f"no Location header in { response } " )
50
70
51
71
start_time = time .time ()
52
- for _ in range (api_timeout ):
53
- logger .debug (f"GET API call: { location_uri } , timeout { timeout } seconds and headers: { headers } " )
54
- response = requests .get (location_uri , headers = headers , proxies = get_proxies (location_uri ), timeout = timeout )
55
- if response is None :
56
- raise Exception ("API call failed" )
57
-
58
- if response .status_code == 202 :
72
+ if api_timeout is None :
73
+ while True :
74
+ if call_finished (location_uri , headers , timeout ):
75
+ break
76
+ time .sleep (1 )
77
+ else :
78
+ for _ in range (api_timeout ):
79
+ if call_finished (location_uri , headers , timeout ):
80
+ break
59
81
time .sleep (1 )
60
- else :
61
- break
62
82
63
83
if response .status_code == 202 :
64
84
wait_time = time .time () - start_time
@@ -81,9 +101,7 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
81
101
:param data: data or None
82
102
:param timeout: optional connect timeout in seconds.
83
103
Applies also to asynchronous API status calls.
84
- If None, default (60 seconds) will be used.
85
104
:param api_timeout: optional timeout for asynchronous API requests in seconds.
86
- If None, default (300 seconds) will be used.
87
105
:return: the result of the handler call, can be None
88
106
"""
89
107
logger = logging .getLogger (__name__ )
@@ -92,12 +110,6 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
92
110
if handler is None or not callable (handler ):
93
111
raise Exception ('Unknown HTTP verb: {}' .format (verb ))
94
112
95
- if timeout is None :
96
- timeout = 60
97
-
98
- if api_timeout is None :
99
- api_timeout = 300
100
-
101
113
logger .debug ("{} API call: {} with data '{}', connect timeout {} seconds, API timeout {} seconds and headers: {}" .
102
114
format (verb , uri , data , timeout , api_timeout , headers ))
103
115
r = handler (
@@ -113,7 +125,7 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
113
125
raise Exception ("API call failed" )
114
126
115
127
if r .status_code == 202 :
116
- r = wait_for_async_api (r , api_timeout , headers = headers , timeout = timeout )
128
+ r = wait_for_async_api (r , api_timeout = api_timeout , headers = headers , timeout = timeout )
117
129
118
130
r .raise_for_status ()
119
131
@@ -142,7 +154,7 @@ def call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
142
154
data substitution
143
155
:param http_headers: optional dictionary of HTTP headers to be appended
144
156
:param timeout: optional timeout in seconds for API call response
145
- :return return value from given requests method
157
+ :return value from given requests method
146
158
"""
147
159
148
160
logger = logging .getLogger (__name__ )
0 commit comments