-
Notifications
You must be signed in to change notification settings - Fork 23
Premier essai (NE PAS MERGER!!!) #224
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
Closed
Closed
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
40b828f
Premier essai
ic-dev21 c526073
Linting
ic-dev21 ca26fd9
quelques progrès
ic-dev21 81e2e72
Linting, imports oubliés
ic-dev21 4e9b0ab
Keeping the right tokens for every websocket connection
Leicas 866ea6a
Too many arguments
ic-dev21 38f05ad
ça fonctionne comme ça
Leicas ffef113
Some Linting
ic-dev21 1cc1237
Some more linting
ic-dev21 5c4cd2c
better logs to see which ws is doing what.
Leicas a4837a3
Revert "Some more linting"
ic-dev21 d033e6e
More linting
ic-dev21 2d27556
Fix hilo_state.yaml bug
ic-dev21 509f40b
Remove uneeded function
ic-dev21 b025cd8
Remove unused import
ic-dev21 6db426e
Linting
ic-dev21 166e52e
Linting
ic-dev21 30d157b
Some more linting
ic-dev21 6f5486d
Update api.py
ic-dev21 469de83
Update api.py
ic-dev21 8bf5305
Update api.py
ic-dev21 59fce91
Meilleure identification
ic-dev21 cac3edf
Logging + websocket en parallèle
ic-dev21 7223d8b
Removing logging
ic-dev21 c25d223
Cleanup dead code
ic-dev21 62d3536
Cleanup dead code
ic-dev21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,7 +27,7 @@ | |||||||||||||||||||||||||||||
| API_NOTIFICATIONS_ENDPOINT, | ||||||||||||||||||||||||||||||
| API_REGISTRATION_ENDPOINT, | ||||||||||||||||||||||||||||||
| API_REGISTRATION_HEADERS, | ||||||||||||||||||||||||||||||
| AUTOMATION_DEVICEHUB_ENDPOINT, | ||||||||||||||||||||||||||||||
| AUTOMATION_CHALLENGE_ENDPOINT, | ||||||||||||||||||||||||||||||
| DEFAULT_STATE_FILE, | ||||||||||||||||||||||||||||||
| DEFAULT_USER_AGENT, | ||||||||||||||||||||||||||||||
| FB_APP_ID, | ||||||||||||||||||||||||||||||
|
|
@@ -51,7 +51,7 @@ | |||||||||||||||||||||||||||||
| get_state, | ||||||||||||||||||||||||||||||
| set_state, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| from pyhilo.websocket import WebsocketClient | ||||||||||||||||||||||||||||||
| from pyhilo.websocket import WebsocketClient, WebsocketManager | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class API: | ||||||||||||||||||||||||||||||
|
|
@@ -81,9 +81,13 @@ def __init__( | |||||||||||||||||||||||||||||
| self.device_attributes = get_device_attributes() | ||||||||||||||||||||||||||||||
| self.session: ClientSession = session | ||||||||||||||||||||||||||||||
| self._oauth_session = oauth_session | ||||||||||||||||||||||||||||||
| self.websocket: WebsocketClient | ||||||||||||||||||||||||||||||
| self.websocket_devices: WebsocketClient | ||||||||||||||||||||||||||||||
| self.websocket_challenges: WebsocketClient | ||||||||||||||||||||||||||||||
| self.log_traces = log_traces | ||||||||||||||||||||||||||||||
| self._get_device_callbacks: list[Callable[..., Any]] = [] | ||||||||||||||||||||||||||||||
| self.ws_url: str = "" | ||||||||||||||||||||||||||||||
| self.ws_token: str = "" | ||||||||||||||||||||||||||||||
| self.endpoint: str = "" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||
| async def async_create( | ||||||||||||||||||||||||||||||
|
|
@@ -132,6 +136,9 @@ async def async_get_access_token(self) -> str: | |||||||||||||||||||||||||||||
| if not self._oauth_session.valid_token: | ||||||||||||||||||||||||||||||
| await self._oauth_session.async_ensure_token_valid() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| access_token = str(self._oauth_session.token["access_token"]) | ||||||||||||||||||||||||||||||
| LOG.debug(f"ic-dev21 access token is {access_token}") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return str(self._oauth_session.token["access_token"]) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| def dev_atts( | ||||||||||||||||||||||||||||||
|
|
@@ -216,17 +223,24 @@ async def _async_request( | |||||||||||||||||||||||||||||
| :rtype: dict[str, Any] | ||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||
| kwargs.setdefault("headers", self.headers) | ||||||||||||||||||||||||||||||
| access_token = await self.async_get_access_token() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if endpoint.startswith(API_REGISTRATION_ENDPOINT): | ||||||||||||||||||||||||||||||
| kwargs["headers"] = {**kwargs["headers"], **API_REGISTRATION_HEADERS} | ||||||||||||||||||||||||||||||
| if endpoint.startswith(FB_INSTALL_ENDPOINT): | ||||||||||||||||||||||||||||||
| kwargs["headers"] = {**kwargs["headers"], **FB_INSTALL_HEADERS} | ||||||||||||||||||||||||||||||
| if endpoint.startswith(ANDROID_CLIENT_ENDPOINT): | ||||||||||||||||||||||||||||||
| kwargs["headers"] = {**kwargs["headers"], **ANDROID_CLIENT_HEADERS} | ||||||||||||||||||||||||||||||
| if host == API_HOSTNAME: | ||||||||||||||||||||||||||||||
| access_token = await self.async_get_access_token() | ||||||||||||||||||||||||||||||
| kwargs["headers"]["authorization"] = f"Bearer {access_token}" | ||||||||||||||||||||||||||||||
| kwargs["headers"]["Host"] = host | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # ic-dev21 trying Leicas suggestion | ||||||||||||||||||||||||||||||
| if endpoint.startswith(AUTOMATION_CHALLENGE_ENDPOINT): | ||||||||||||||||||||||||||||||
| # remove Ocp-Apim-Subscription-Key header to avoid 401 error | ||||||||||||||||||||||||||||||
| kwargs["headers"].pop("Ocp-Apim-Subscription-Key", None) | ||||||||||||||||||||||||||||||
| kwargs["headers"]["authorization"] = f"Bearer {access_token}" | ||||||||||||||||||||||||||||||
|
Comment on lines
+239
to
+242
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handling different authorization methods for various endpoints in the same function increases complexity and potential for errors. Consider extracting this logic into a separate function to improve readability and maintainability.
Suggested change
ic-dev21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| data: dict[str, Any] = {} | ||||||||||||||||||||||||||||||
| url = parse.urljoin(f"https://{host}", endpoint) | ||||||||||||||||||||||||||||||
| if self.log_traces: | ||||||||||||||||||||||||||||||
|
|
@@ -303,8 +317,9 @@ async def _async_handle_on_backoff(self, _: dict[str, Any]) -> None: | |||||||||||||||||||||||||||||
| LOG.info( | ||||||||||||||||||||||||||||||
| "401 detected on websocket, refreshing websocket token. Old url: {self.ws_url} Old Token: {self.ws_token}" | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| LOG.info(f"401 detected on {err.request_info.url}") | ||||||||||||||||||||||||||||||
| async with self._backoff_refresh_lock_ws: | ||||||||||||||||||||||||||||||
| (self.ws_url, self.ws_token) = await self.post_devicehub_negociate() | ||||||||||||||||||||||||||||||
| await self.refresh_ws_token() | ||||||||||||||||||||||||||||||
| await self.get_websocket_params() | ||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -354,30 +369,23 @@ async def _async_post_init(self) -> None: | |||||||||||||||||||||||||||||
| LOG.debug("Websocket postinit") | ||||||||||||||||||||||||||||||
| await self._get_fid() | ||||||||||||||||||||||||||||||
| await self._get_device_token() | ||||||||||||||||||||||||||||||
| await self.refresh_ws_token() | ||||||||||||||||||||||||||||||
| self.websocket = WebsocketClient(self) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def refresh_ws_token(self) -> None: | ||||||||||||||||||||||||||||||
| (self.ws_url, self.ws_token) = await self.post_devicehub_negociate() | ||||||||||||||||||||||||||||||
| await self.get_websocket_params() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def post_devicehub_negociate(self) -> tuple[str, str]: | ||||||||||||||||||||||||||||||
| LOG.debug("Getting websocket url") | ||||||||||||||||||||||||||||||
| url = f"{AUTOMATION_DEVICEHUB_ENDPOINT}/negotiate" | ||||||||||||||||||||||||||||||
| LOG.debug(f"devicehub URL is {url}") | ||||||||||||||||||||||||||||||
| resp = await self.async_request("post", url) | ||||||||||||||||||||||||||||||
| ws_url = resp.get("url") | ||||||||||||||||||||||||||||||
| ws_token = resp.get("accessToken") | ||||||||||||||||||||||||||||||
| LOG.debug("Calling set_state devicehub_negotiate") | ||||||||||||||||||||||||||||||
| await set_state( | ||||||||||||||||||||||||||||||
| self._state_yaml, | ||||||||||||||||||||||||||||||
| "websocket", | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| "url": ws_url, | ||||||||||||||||||||||||||||||
| "token": ws_token, | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| # Initialize WebsocketManager ic-dev21 | ||||||||||||||||||||||||||||||
| self.websocket_manager = WebsocketManager( | ||||||||||||||||||||||||||||||
| self.session, self.async_request, self._state_yaml, set_state | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| return (ws_url, ws_token) | ||||||||||||||||||||||||||||||
| await self.websocket_manager.initialize_websockets() | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Create both websocket clients | ||||||||||||||||||||||||||||||
| # ic-dev21 need to work on this as it can't lint as is, may need to | ||||||||||||||||||||||||||||||
| # instantiate differently | ||||||||||||||||||||||||||||||
| self.websocket_devices = WebsocketClient(self.websocket_manager.devicehub) | ||||||||||||||||||||||||||||||
| self.websocket_challenges = WebsocketClient(self.websocket_manager.challengehub) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def refresh_ws_token(self) -> None: | ||||||||||||||||||||||||||||||
| """Refresh the websocket token.""" | ||||||||||||||||||||||||||||||
| await self.websocket_manager.refresh_token(self.websocket_manager.devicehub) | ||||||||||||||||||||||||||||||
| await self.websocket_manager.refresh_token(self.websocket_manager.challengehub) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def get_websocket_params(self) -> None: | ||||||||||||||||||||||||||||||
| uri = parse.urlparse(self.ws_url) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the 'Ocp-Apim-Subscription-Key' header when the endpoint is 'AUTOMATION_CHALLENGE_ENDPOINT' might introduce issues if this header is required elsewhere in the system. It's crucial to document this change within the function's docstring or as comments explaining the broader context and its necessity.