Additional support needed when using orjson

This commit is contained in:
Miguel Grinberg
2025-04-12 23:58:48 +01:00
parent 1f64478957
commit cd0b3234dd
4 changed files with 7 additions and 11 deletions

View File

@@ -1,13 +1,9 @@
import binascii
import hashlib
import hmac
import json
from time import time
try:
import orjson as json
except ImportError:
import json
def _to_b64url(data):
return (
binascii.b2a_base64(data)

View File

@@ -578,9 +578,9 @@ class Response:
self.headers = NoCaseDict(headers or {})
self.reason = reason
if isinstance(body, (dict, list)):
self.body = json.dumps(body).encode()
body = json.dumps(body)
self.headers['Content-Type'] = 'application/json; charset=UTF-8'
elif isinstance(body, str):
if isinstance(body, str):
self.body = body.encode()
else:
# this applies to bytes, file-like objects or generators

View File

@@ -29,8 +29,8 @@ class SSE:
given, it must be a string.
"""
if isinstance(data, (dict, list)):
data = json.dumps(data).encode()
elif isinstance(data, str):
data = json.dumps(data)
if isinstance(data, str):
data = data.encode()
elif not isinstance(data, bytes):
data = str(data).encode()

View File

@@ -105,10 +105,10 @@ class TestClient:
if body is None:
body = b''
elif isinstance(body, (dict, list)):
body = json.dumps(body).encode()
body = json.dumps(body)
if 'Content-Type' not in headers: # pragma: no cover
headers['Content-Type'] = 'application/json'
elif isinstance(body, str):
if isinstance(body, str):
body = body.encode()
if body and 'Content-Length' not in headers:
headers['Content-Length'] = str(len(body))