Skip to content

Commit

Permalink
Merge pull request #2 from staciax/speed
Browse files Browse the repository at this point in the history
Add msgspec for speedup json decoding
  • Loading branch information
staciax authored Jan 4, 2025
2 parents 14147ac + bd152c8 commit 945f850
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ dev = [
"anyio>=4.7.0,<5.0",
]

[project.optional-dependencies]
speed = ["msgspec>=0.19.0,<1.0"]

[tool.setuptools.dynamic]
version = { attr = "valorant.__version__" }

Expand Down Expand Up @@ -132,7 +135,6 @@ ignore = [
"D107", # undocumented-public-init
]


[tool.ruff.lint.isort]
combine-as-imports = true

Expand Down
42 changes: 42 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion valorant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__copyright__ = 'Copyright 2023-present STACiA'
__version__ = '2.0.1a'

from . import models
from . import models, utils
from .client import Client
from .enums import (
AbilitySlot,
Expand Down Expand Up @@ -51,4 +51,5 @@
'ValorantError',
'WeaponCategory',
'models',
'utils',
)
9 changes: 7 additions & 2 deletions valorant/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import aiohttp

from . import __version__
from . import __version__, utils
from .errors import BadRequest, HTTPException, InternalServerError, NotFound, RateLimited

if TYPE_CHECKING:
Expand All @@ -47,6 +47,11 @@
# http-client inspired by https://github.com/Rapptz/discord.py/blob/master/discord/http.py


async def to_json(response: aiohttp.ClientResponse) -> dict[str, Any] | str:
text = await response.text(encoding='utf-8')
return utils._from_json(text) # type: ignore[no-any-return]


class Route:
BASE: ClassVar[str] = 'https://valorant-api.com/v1'

Expand Down Expand Up @@ -91,7 +96,7 @@ async def request(self, route: Route, **kwargs: Any) -> Any:
async with self.__session.request(method, url, **kwargs) as response: # noqa: F811
_log.debug('%s %s with returned %s', method, url, response.status)

data = await response.json()
data = await to_json(response)

if 300 > response.status >= 200:
_log.debug('%s %s has received %s', method, url, data)
Expand Down
14 changes: 14 additions & 0 deletions valorant/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# mypy: disable-error-code="assignment, import-not-found"

import json

try:
import msgspec
except ImportError: # pragma: no cover
msgspec = None


if msgspec is None: # pragma: no cover # noqa: SIM108
_from_json = json.loads
else:
_from_json = msgspec.json.decode

0 comments on commit 945f850

Please sign in to comment.