-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include to use Server-Sent Events (SSE) instead of WebSocket
- Loading branch information
1 parent
93283b9
commit 7cba22c
Showing
4 changed files
with
43 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,44 +1,42 @@ | ||
from typing import Union | ||
import requests | ||
from typing import Union, Literal | ||
from urllib.parse import urlencode, urljoin | ||
from sseclient import SSEClient | ||
from tagoio_sdk.common.tagoio_module import GenericModuleParams | ||
from tagoio_sdk.regions import getConnectionURI | ||
|
||
channelsWithID = ["device_inspector", "analysis_console", "ui_dashboard"] | ||
channelsWithoutID = ["notification", "analysis_trigger", "ui"] | ||
channels = channelsWithID + channelsWithoutID | ||
|
||
class OpenSSEWithID(GenericModuleParams): | ||
def __init__(self, channel: str, resource_id: str): | ||
super().__init__() | ||
self.channel = channel | ||
self.resource_id = resource_id | ||
|
||
channel: Literal["device_inspector", "analysis_console", "ui_dashboard"] | ||
resources_id: str | ||
|
||
class OpenSSEWithoutID(GenericModuleParams): | ||
def __init__(self, channel: str): | ||
super().__init__() | ||
self.channel = channel | ||
channel: Literal["notification", "analysis_trigger", "ui"] | ||
|
||
|
||
OpenSSEConfig = Union[OpenSSEWithID, OpenSSEWithoutID] | ||
|
||
channelsWithID = ["device_inspector", "analysis_console", "ui_dashboard"] | ||
channelsWithoutID = ["notification", "analysis_trigger", "ui"] | ||
channels = channelsWithID + channelsWithoutID | ||
|
||
|
||
def isChannelWithID(params: OpenSSEConfig) -> bool: | ||
return params.channel in channelsWithID | ||
return params.get("channel") in channelsWithID | ||
|
||
|
||
def openSSEListening(params: OpenSSEConfig) -> SSEClient: | ||
base_url = getConnectionURI(params.region)["sse"] | ||
base_url = getConnectionURI(params.get("region"))["sse"] | ||
url = urljoin(base_url, "/events") | ||
|
||
query_params = {"token": params.token} | ||
query_params = {} | ||
if isChannelWithID(params): | ||
query_params["channel"] = f"{params.channel}.{params.resource_id}" | ||
query_params["channel"] = f"{params.get('channel')}.{params.get('resources_id')}" | ||
else: | ||
query_params["channel"] = params.channel | ||
query_params["channel"] = params.get("channel") | ||
|
||
query_params["token"] = params.get("token") | ||
|
||
url += "?" + urlencode(query_params) | ||
|
||
return SSEClient(url) | ||
response = requests.get(url, stream=True, headers={"Accept": "text/event-stream"}) | ||
|
||
return SSEClient(response) |
This file contains 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 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