Skip to content

Commit dd13998

Browse files
committed
Explicitly create json body for the requests module to support NaN, -inf, inf
1 parent 4120d22 commit dd13998

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

simvue/api.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import copy
2+
import json
13
import requests
24
from tenacity import retry, wait_exponential, stop_after_attempt
35

@@ -7,16 +9,25 @@
79
RETRY_MAX = 10
810
RETRY_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))
1223
def 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

Comments
 (0)