Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HTTPClient): use orjson if it's installed #262

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions nextcore/http/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from aiohttp import ClientSession

from ... import __version__ as nextcore_version
from ...common import UNDEFINED, Dispatcher, UndefinedType
from ...common import UNDEFINED, Dispatcher, UndefinedType, json_dumps
from ..bucket import Bucket
from ..bucket_metadata import BucketMetadata
from ..errors import (
Expand All @@ -45,10 +45,12 @@
from ..rate_limit_storage import RateLimitStorage
from ..route import Route
from .base_client import BaseHTTPClient
from .client_response import ClientResponse

if TYPE_CHECKING:
from typing import Any, Final, Literal

# ClientResponse is still imported here because ours is "incompatible"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incredibly cursed. Wouldnt it make more sense to make ours compatible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pyright isn't being specific on how it's "incompatible", so I'm not sure how to make it compatible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nextcore/nextcore/http/client/client.py:316:28 - error: Expression of type "ClientResponse" cannot be assigned to return type "ClientResponse"
"aiohttp.client_reqrep.ClientResponse" is incompatible with "nextcore.http.client.client_response.ClientResponse" (reportGeneralTypeIssues)

Here's what I'm getting, for reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird. Will look into it eventually tm

from aiohttp import ClientResponse, ClientWebSocketResponse

logger = getLogger(__name__)
Expand Down Expand Up @@ -189,7 +191,7 @@ async def setup(self) -> None:
"""
if self._session is not None:
raise RuntimeError("This method can only be called once!")
self._session = ClientSession()
self._session = ClientSession(json_serialize=json_dumps, response_class=ClientResponse)

async def close(self) -> None:
"""Clean up internal state"""
Expand Down
40 changes: 40 additions & 0 deletions nextcore/http/client/client_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# The MIT License (MIT)
# Copyright (c) 2021-present tag-epic
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from typing import Any, Optional

from aiohttp import ClientResponse as _ClientResponse
from aiohttp.typedefs import JSONDecoder

from ...common import json_loads


class ClientResponse(_ClientResponse):
"""A patched version of :class:`aiohttp.ClientResponse` that decodes JSON using orjson, if installed."""

async def json(
self,
*,
encoding: Optional[str] = None,
loads: JSONDecoder = json_loads,
content_type: Optional[str] = "application/json"
) -> Any:
return await super().json(encoding=encoding, loads=loads, content_type=content_type)