pincer.core.dispatch   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 28
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GatewayDispatch.__init__() 0 11 1
A GatewayDispatch.__str__() 0 3 1
A GatewayDispatch.from_string() 0 21 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
5
from __future__ import annotations
6
7
from json import dumps, loads
8
from typing import TYPE_CHECKING
9
10
if TYPE_CHECKING:
11
    from typing import Any, Dict, Optional, Union
12
13
14
class GatewayDispatch:
15
    """Represents a websocket message.
16
17
    Attributes
18
    ----------
19
    op: :class:`int`
20
        The discord opcode which represents what the message means.
21
    data: Optional[Union[:class:`int`, Dict[:class:`str`, Any]]]
22
        The event data that has been sent/received.
23
    seq: Optional[:class:`int`]
24
        The sequence number of a message, which can be used
25
        for resuming sessions and heartbeats.
26
    event_name: Optional[:class:`str`]
27
        The event name for the payload.
28
    """
29
30
    def __init__(
31
        self,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
32
        op: int,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
33
        data: Optional[Union[int, bool, Dict[str, Any]]] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
34
        seq: Optional[int] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
35
        name: Optional[str] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
36
    ):
37
        self.op: int = op
38
        self.data: Optional[Union[int, bool, Dict[str, Any]]] = data
39
        self.seq: Optional[int] = seq
40
        self.event_name: Optional[str] = name
41
42
    def __str__(self) -> str:
43
        return dumps(
44
            dict(op=self.op, d=self.data, s=self.seq, t=self.event_name)
45
        )
46
47
    @classmethod
48
    def from_string(cls, payload: str) -> GatewayDispatch:
49
        """Parses a given payload from a string format
50
        and returns a GatewayDispatch.
51
52
        Parameters
53
        ----------
54
        payload : :class:`str`
55
            The payload to parse.
56
57
        Returns
58
        -------
59
        :class:`~pincer.core.dispatch.GatewayDispatch`
60
            The new class.
61
        """
62
        payload: Dict[str, Union[int, str, Dict[str, Any]]] = loads(payload)
63
        return cls(
64
            payload.get("op"),
65
            payload.get("d"),
66
            payload.get("s"),
67
            payload.get("t"),
68
        )
69