Skip to content

Commit

Permalink
python: add parser for welcome message
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahad-10 committed Jun 8, 2024
1 parent eab4239 commit e220c66
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions python/wampprotobuf/parsers/welcome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Any

from wampproto.messages import Message
from wampproto.messages.welcome import IWelcomeFields, Welcome

from wampprotobuf.gen import welcome_pb2


class WelcomeFields(IWelcomeFields):
def __init__(self, msg: welcome_pb2.Welcome):
super().__init__()
self._msg = msg

@property
def session_id(self) -> int:
return self._msg.session_id

@property
def roles(self) -> dict[str, Any]:
return {}

@property
def authid(self) -> str:
return self._msg.auth_id

@property
def authrole(self) -> str:
return self._msg.auth_role

@property
def authmethod(self) -> str:
return ""

@property
def authextra(self) -> dict[str, Any]:
return {}


def from_protobuf(payload: bytes) -> Message:
result = welcome_pb2.Welcome()
result.ParseFromString(payload)

return Welcome(WelcomeFields(result))


def to_protobuf(welcome: Welcome) -> bytes:
result = welcome_pb2.Welcome()
result.session_id = welcome.session_id
result.auth_id = welcome.authid
result.auth_role = welcome.authrole

return result.SerializeToString()

0 comments on commit e220c66

Please sign in to comment.