Skip to content

Commit

Permalink
Add http_client_options and ws_client_options. Remove extra_headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-mosleh committed Feb 22, 2022
1 parent f7dcbe0 commit 6e42ac6
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 18 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pip install signalr-async

### Create it

* Create a file `main.py` with:
- Create a file `main.py` with:

```Python
import asyncio
Expand Down Expand Up @@ -78,11 +78,13 @@ for i in range(10):

async def main():
token = "mytoken"
headers = {"Authorization": f"Bearer {token}"}
async with Client(
"https://localhost:9000",
hub,
connection_options={
"extra_headers": {"Authorization": f"Bearer {token}"},
"http_client_options": {"headers": headers},
"ws_client_options": {"headers": headers, "timeout": 1.0},
"protocol": MessagePackProtocol(),
},
) as client:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "signalr-async"
version = "2.3.1"
version = "3.0.0"
description = "Python SignalR async client"
authors = ["Sam Mosleh <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion signalr_async/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.3.1"
__version__ = "3.0.0"
2 changes: 1 addition & 1 deletion signalr_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def _producer(self) -> None:
await self._connection.send(message)
except ConnectionClosed:
self.logger.error(
f"Message has not been sent because connection is closed"
f"Message {message.invocation_id} has not been sent because connection is closed"
)
self._invoke_manager.set_invocation_exception(
message.invocation_id, "Connection is closed"
Expand Down
13 changes: 9 additions & 4 deletions signalr_async/connection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import time
from abc import abstractmethod
from typing import Dict, Generic, List, Optional, Tuple, TypeVar, Union
from typing import Any, Dict, Generic, List, Optional, Tuple, TypeVar, Union

import aiohttp
import yarl
Expand All @@ -17,12 +17,14 @@ def __init__(
self,
base_url: str,
extra_params: Optional[Dict[str, str]] = None,
extra_headers: Optional[Dict[str, str]] = None,
http_client_options: Optional[Dict[str, Any]] = None,
ws_client_options: Optional[Dict[str, Any]] = None,
logger: Optional[logging.Logger] = None,
):
self._base_url = yarl.URL(base_url)
self._extra_params = extra_params or {}
self._extra_headers = extra_headers or {}
self._http_client_options = http_client_options or {}
self._ws_client_options = ws_client_options or {}
self.logger = logger or logging.getLogger(__name__)
self.last_message_received_time: Optional[float] = None
self.last_message_sent_time: Optional[float] = None
Expand All @@ -43,7 +45,8 @@ async def start(self) -> bool:
connect_path = self._generate_connect_path()
self.logger.debug(f"Connecting to {connect_path}")
self._websocket = await self._session.ws_connect(
connect_path, headers=self._extra_headers
connect_path,
**self._ws_client_options,
)
self.logger.debug("Initializing")
await self._initialize_connection()
Expand Down Expand Up @@ -98,6 +101,8 @@ async def _receive_raw(self, timeout: Optional[float] = None) -> Union[str, byte
raise ConnectionClosed() from None
raw_ws_message = await self._websocket.receive(timeout=timeout)
self.logger.debug(f"Raw message received: {raw_ws_message}")
if raw_ws_message.type == aiohttp.WSMsgType.ERROR:
raise ConnectionError()
if raw_ws_message.type in (
aiohttp.WSMsgType.CLOSE,
aiohttp.WSMsgType.CLOSING,
Expand Down
3 changes: 2 additions & 1 deletion signalr_async/net/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def build_connection(
base_url=base_url,
hub_names=[hub.name for hub in self._hubs],
extra_params=connection_options.get("extra_params"),
extra_headers=connection_options.get("extra_headers"),
http_client_options=connection_options.get("http_client_options"),
ws_client_options=connection_options.get("ws_client_options"),
logger=self.logger,
)

Expand Down
9 changes: 6 additions & 3 deletions signalr_async/net/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ def __init__(
base_url: str,
hub_names: Optional[List[str]] = None,
extra_params: Optional[Dict[str, str]] = None,
extra_headers: Optional[Dict[str, str]] = None,
http_client_options: Optional[Dict[str, Any]] = None,
ws_client_options: Optional[Dict[str, Any]] = None,
logger: Optional[logging.Logger] = None,
):
super().__init__(base_url, extra_params, extra_headers, logger)
super().__init__(
base_url, extra_params, http_client_options, ws_client_options, logger
)
self._hub_names = hub_names or []
self.transport: Optional[str] = None
self.message_id: Optional[str] = None
Expand Down Expand Up @@ -85,7 +88,7 @@ async def _send_command(self, command: CommandEnum) -> Any:
async with self._session.get(
self._base_url / command.value,
params=self._common_params(),
headers=self._extra_headers,
**self._http_client_options,
) as resp:
resp.raise_for_status()
return await resp.json()
Expand Down
3 changes: 2 additions & 1 deletion signalr_async/netcore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def build_connection(
hub_name=self._hub.name,
protocol=connection_options.get("protocol"),
extra_params=connection_options.get("extra_params"),
extra_headers=connection_options.get("extra_headers"),
http_client_options=connection_options.get("http_client_options"),
ws_client_options=connection_options.get("ws_client_options"),
logger=self.logger,
)

Expand Down
9 changes: 6 additions & 3 deletions signalr_async/netcore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def __init__(
hub_name: str,
protocol: Optional[Union[JsonProtocol, MessagePackProtocol]] = None,
extra_params: Optional[Dict[str, str]] = None,
extra_headers: Optional[Dict[str, str]] = None,
http_client_options: Optional[Dict[str, Any]] = None,
ws_client_options: Optional[Dict[str, Any]] = None,
logger: Optional[logging.Logger] = None,
):
super().__init__(base_url, extra_params, extra_headers, logger)
super().__init__(
base_url, extra_params, http_client_options, ws_client_options, logger
)
self._url = self._base_url / hub_name
self.protocol = protocol or JsonProtocol()
self.handshake_protocol = JsonProtocol()
Expand All @@ -42,7 +45,7 @@ async def _send_command(
async with self._session.post(
self._url / command,
params=params or self._common_params(),
headers=self._extra_headers,
**self._http_client_options,
) as resp:
resp.raise_for_status()
return await resp.json()
Expand Down
Empty file added signalr_async/py.typed
Empty file.
2 changes: 1 addition & 1 deletion tests/test_signalr_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "2.3.1"
assert __version__ == "3.0.0"

0 comments on commit 6e42ac6

Please sign in to comment.