From 0a2efb58cd8593ab0f4aadbe697c92a175088ca2 Mon Sep 17 00:00:00 2001 From: RGBCube <78925721+RGBCube@users.noreply.github.com> Date: Tue, 28 Jun 2022 15:41:07 +0300 Subject: [PATCH] Make HTTPClient.request use orjson if it exists --- github/internals/http.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/github/internals/http.py b/github/internals/http.py index 9f7526b..cd69bb5 100644 --- a/github/internals/http.py +++ b/github/internals/http.py @@ -14,6 +14,14 @@ from aiohttp import ClientSession, __version__ as aiohttp_version from ..errors import error_from_request from ..utils import human_readable_time_until +try: + import orjson +except ImportError: + import json + json_loads = json.loads +else: + json_loads = orjson.loads + if TYPE_CHECKING: from aiohttp import BasicAuth from typing_extensions import Self @@ -194,10 +202,12 @@ class HTTPClient: ) if 200 <= response.status <= 299: - if response.headers["Content-Type"] == "application/json": - return await response.json() + data = await response.text(encoding='utf-8') - return await response.text(encoding="utf-8") + if response.headers["Content-Type"] == "application/json": + return json_loads(data) + + return data raise error_from_request(response)