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 attempt pour issue 457 #216

Merged
merged 8 commits into from
Oct 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ repos:
- types-aiofiles==23.2.0

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
rev: v5.0.0
hooks:
- id: check-json
- id: no-commit-to-branch
Expand Down
4 changes: 4 additions & 0 deletions pyhilo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ async def post_devicehub_negociate(self) -> tuple[str, str]:
resp = await self.async_request("post", url)
ws_url = resp.get("url")
ws_token = resp.get("accessToken")
LOG.debug("Calling set_state")
await set_state(
self._state_yaml,
"websocket",
Expand Down Expand Up @@ -398,6 +399,7 @@ async def get_websocket_params(self) -> None:
"available_transports": transport_dict,
"full_ws_url": self.full_ws_url,
}
LOG.debug("Calling set_state")
await set_state(self._state_yaml, "websocket", websocket_dict)

async def fb_install(self, fb_id: str) -> None:
Expand All @@ -423,6 +425,7 @@ async def fb_install(self, fb_id: str) -> None:
raise RequestError(err) from err
LOG.debug(f"FB Install data: {resp}")
auth_token = resp.get("authToken", {})
LOG.debug("Calling set_state")
await set_state(
self._state_yaml,
"firebase",
Expand Down Expand Up @@ -464,6 +467,7 @@ async def android_register(self) -> None:
LOG.error(f"Android registration error: {msg}")
raise RequestError
token = msg.split("=")[-1]
LOG.debug("Calling set_state")
await set_state(
self._state_yaml,
"android",
Expand Down
2 changes: 1 addition & 1 deletion pyhilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
LOG: Final = logging.getLogger(__package__)
DEFAULT_STATE_FILE: Final = "hilo_state.yaml"
REQUEST_RETRY: Final = 9
PYHILO_VERSION: Final = "2024.06.01"
PYHILO_VERSION: Final = "2024.10.02"
# TODO: Find a way to keep previous line in sync with pyproject.toml automatically

CONTENT_TYPE_FORM: Final = "application/x-www-form-urlencoded"
Expand Down
18 changes: 11 additions & 7 deletions pyhilo/util/state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from datetime import datetime
from os.path import isfile
from typing import Any, Optional, Type, TypedDict, TypeVar, Union
Expand All @@ -7,6 +8,8 @@

from pyhilo.const import LOG

lock = asyncio.Lock()


class TokenDict(TypedDict):
access: Optional[str]
Expand Down Expand Up @@ -103,10 +106,11 @@ async def set_state(
:type state: ``StateDict``
:rtype: ``StateDict``
"""
current_state = await get_state(state_yaml) or {}
merged_state: dict[str, Any] = {key: {**current_state.get(key, {}), **state}} # type: ignore
new_state: dict[str, Any] = {**current_state, **merged_state}
async with aiofiles.open(state_yaml, mode="w") as yaml_file:
LOG.debug("Saving state to yaml file")
content = yaml.dump(new_state)
await yaml_file.write(content)
async with lock: # note ic-dev21: on lock le fichier pour être sûr de finir la job
current_state = await get_state(state_yaml) or {}
merged_state: dict[str, Any] = {key: {**current_state.get(key, {}), **state}} # type: ignore
new_state: dict[str, Any] = {**current_state, **merged_state}
async with aiofiles.open(state_yaml, mode="w") as yaml_file:
LOG.debug("Saving state to yaml file")
content = yaml.dump(new_state)
await yaml_file.write(content)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exclude = ".venv/.*"

[tool.poetry]
name = "python-hilo"
version = "2024.10.1"
version = "2024.10.2"
description = "A Python3, async interface to the Hilo API"
readme = "README.md"
authors = ["David Vallee Delisle <[email protected]>"]
Expand Down