1+ import copy
2+ import json
13import requests
24from tenacity import retry , wait_exponential , stop_after_attempt
35
79RETRY_MAX = 10
810RETRY_STOP = 5
911
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+
1021@retry (wait = wait_exponential (multiplier = RETRY_MULTIPLIER , min = RETRY_MIN , max = RETRY_MAX ),
1122 stop = stop_after_attempt (RETRY_STOP ))
1223def post (url , headers , data , is_json = True ):
1324 """
1425 HTTP POST with retries
1526 """
1627 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 )
2031
2132 return response
2233
@@ -27,8 +38,8 @@ def put(url, headers, data, is_json=True, timeout=DEFAULT_API_TIMEOUT):
2738 HTTP PUT with retries
2839 """
2940 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 )
3344
3445 return response
0 commit comments