Skip to content

Commit

Permalink
Use orjson (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
bieniu committed Aug 24, 2022
1 parent b5f6594 commit 31b2753
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ repos:
rev: "v0.971"
hooks:
- id: mypy
additional_dependencies:
- types-orjson
files: ^(accuweather)/.+\.py$
7 changes: 4 additions & 3 deletions accuweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from __future__ import annotations

import json
import logging
from http import HTTPStatus
from typing import Any

import orjson
from aiohttp import ClientSession

from .const import (
Expand Down Expand Up @@ -146,14 +146,15 @@ async def _async_get_data(self, url: str) -> Any:
if resp.status == HTTPStatus.UNAUTHORIZED.value:
raise InvalidApiKeyError("Invalid API key")
if resp.status != HTTPStatus.OK.value:
error_text = json.loads(await resp.text())
# pylint: disable=no-member
error_text = orjson.loads(await resp.text())
if error_text["Message"] == REQUESTS_EXCEEDED:
raise RequestsExceededError(
"The allowed number of requests has been exceeded"
)
raise ApiError(f"Invalid response from AccuWeather API: {resp.status}")
_LOGGER.debug("Data retrieved from %s, status: %s", url, resp.status)
data = await resp.json()
data = await resp.json(loads=orjson.loads) # pylint: disable=no-member
if resp.headers["RateLimit-Remaining"].isdigit():
self._requests_remaining = int(resp.headers["RateLimit-Remaining"])

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
aiohttp>3
aiohttp>3
orjson

0 comments on commit 31b2753

Please sign in to comment.