1
+ import copy
2
+ import json
1
3
import requests
2
4
from tenacity import retry , wait_exponential , stop_after_attempt
3
5
7
9
RETRY_MAX = 10
8
10
RETRY_STOP = 5
9
11
12
+ def set_json_header (headers ):
13
+ """
14
+ Return a copy of the headers with Content-Type set to
15
+ application/json
16
+ """
17
+ headers = copy .deepcopy (headers )
18
+ headers ['Content-Type' ] = 'application/json'
19
+ return headers
20
+
10
21
@retry (wait = wait_exponential (multiplier = RETRY_MULTIPLIER , min = RETRY_MIN , max = RETRY_MAX ),
11
22
stop = stop_after_attempt (RETRY_STOP ))
12
23
def post (url , headers , data , is_json = True ):
13
24
"""
14
25
HTTP POST with retries
15
26
"""
16
27
if is_json :
17
- response = requests . post ( url , headers = headers , json = data , timeout = DEFAULT_API_TIMEOUT )
18
- else :
19
- response = requests .post (url , headers = headers , data = data , timeout = DEFAULT_API_TIMEOUT )
28
+ data = json . dumps ( data )
29
+ headers = set_json_header ( headers )
30
+ response = requests .post (url , headers = headers , data = data , timeout = DEFAULT_API_TIMEOUT )
20
31
21
32
return response
22
33
@@ -27,8 +38,8 @@ def put(url, headers, data, is_json=True, timeout=DEFAULT_API_TIMEOUT):
27
38
HTTP PUT with retries
28
39
"""
29
40
if is_json :
30
- response = requests . put ( url , headers = headers , json = data , timeout = timeout )
31
- else :
32
- response = requests .put (url , headers = headers , data = data , timeout = timeout )
41
+ data = json . dumps ( data )
42
+ headers = set_json_header ( headers )
43
+ response = requests .put (url , headers = headers , data = data , timeout = timeout )
33
44
34
45
return response
0 commit comments