Skip to content

Commit

Permalink
✨ Change from aioredis to hiredis, update to pydantic v2
Browse files Browse the repository at this point in the history
Change from aioredis to hiredis, update to pydantic v2
  • Loading branch information
roma-glushko committed Jun 9, 2024
2 parents 1240576 + 916ddfc commit f4aa606
Show file tree
Hide file tree
Showing 7 changed files with 613 additions and 411 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9, 3.10.4]
python-version: [3.8, 3.9, 3.10.4, 3.11]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install system dependencies
run: |
sudo apt-get install gcc gfortran libopenblas-dev liblapack-dev cython
sudo apt-get install gcc gfortran libopenblas-dev liblapack-dev cython3
python -m pip install --upgrade pip
pip install poetry
- name: Install dependencies
Expand All @@ -44,7 +44,7 @@ jobs:
run: poetry run coverage report --fail-under=80

- name: Upload HTML report if check failed.
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: html-report
path: htmlcov
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.venv
env
*.pyc

.idea
Expand Down
955 changes: 579 additions & 376 deletions poetry.lock

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ packages = [

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
aioredis = "2.0.1"
msgpack = "1.0.3"
pydantic = "1.9.0"
msgpack = "1.0.8"
pydantic = "==2.4.2"
redis = {version = "==5.0.2", extras = ["hiredis"]}

[tool.poetry.dev-dependencies]
pytest = "7.1.1"
isort = "5.10.1"
black = "22.1.0"
flake8 = "4.0.1"
mypy = "0.941"
pytest-asyncio = "0.18.2"
black = "22.3.0"
flake8 = "5.0.4"
mypy = "1.6.1"
pytest-asyncio = "0.21.1"
coverage = "6.3.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.isort]
profile = "black"
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ poetry add socket.io-redis-emitter

- High quality, typed and modern Python codebase
- Clean, concise and Pythonic API
- Uses [aioredis](https://aioredis.readthedocs.io/en/latest/) as a Redis client
- Uses [redis](https://redis.readthedocs.io/en/latest/) as a Redis client
- Supports namespaces, rooms and regular Socket.IO message emitting

```python
from aioredis import Redis
import redis
from socketio_emitter import Emitter

client = Redis(...)
client = redis.Redis(...)
emitter = Emitter(client=client)

async with emitter.namespace("/nsp") as nsp:
async with nsp.rooms("room1", "room2") as clients:
await clients.emit("machineStatus", {"status": "ok"})
```

- Remote requests to join, leave rooms or to disconnect
- Remote requests to join, leave rooms or to disconnect

```python
from aioredis import Redis
import redis
from socketio_emitter import Emitter

client = Redis(...)
client = redis.Redis(...)
emitter = Emitter(client=client)

async with emitter.namespace("/nsp") as nsp:
async with nsp.rooms("room1", "room2") as clients:
await clients.join("room3")
# await clients.leave("room3")
# await clients.disconnect()
```
```
6 changes: 3 additions & 3 deletions socketio_emitter/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator, Dict, List, Optional, Sequence, Union

from aioredis import Redis
import redis

from socketio_emitter.consts import (
CHANNEL_SEPARATOR,
Expand All @@ -29,7 +29,7 @@
class Emitter:
def __init__(
self,
client: "Redis",
client: redis.Redis,
*,
channel_prefix: str = DEFAULT_CHANNEL_PREFIX,
emitter_id: str = DEFAULT_EMITTER_ID,
Expand All @@ -55,7 +55,7 @@ async def namespace(
self, name: str = ROOT_NAMESPACE
) -> AsyncGenerator["Emitter", None]:
"""
Select a namespace to braodcast
Select a namespace to broadcast
Args:
name (str): Namespace Name
Returns:
Expand Down
21 changes: 8 additions & 13 deletions socketio_emitter/entities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, List, Optional, Sequence, Tuple

import msgpack
from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field

from socketio_emitter.consts import ROOT_NAMESPACE, PacketTypes, RequestTypes

Expand Down Expand Up @@ -29,17 +29,15 @@ class MessageOptions(BaseModel):
flags: Optional[MessageFlags] = None
except_rooms: Optional[List[str]] = Field(None, alias="except")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class Packet(BaseModel):
data: MessageData
type: PacketTypes = PacketTypes.REGULAR
namespace: str = Field(ROOT_NAMESPACE, alias="nsp")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class Message(BaseModel):
Expand Down Expand Up @@ -67,8 +65,8 @@ def raw(self) -> bytes:
msgpack.packb(
(
self.emitter_id,
self.packet.dict(by_alias=True),
self.options.dict(by_alias=True),
self.packet.model_dump(by_alias=True),
self.options.model_dump(by_alias=True),
)
)
)
Expand All @@ -78,23 +76,20 @@ class RequestOptions(BaseModel):
rooms: Optional[Sequence[str]] = None
except_rooms: Optional[Sequence[str]] = Field(None, alias="except")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class RoomRequest(BaseModel):
type: RequestTypes
rooms: Sequence[str]
options: RequestOptions = Field(..., alias="opts")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)


class DisconnectRequest(BaseModel):
type: RequestTypes = RequestTypes.DISCONNECT
close: bool
options: RequestOptions = Field(..., alias="opts")

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True)

0 comments on commit f4aa606

Please sign in to comment.