Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gateway client #3

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
44c0633
Add basic gateway client
Not-Nik Jul 21, 2019
a6c6692
Code style
Not-Nik Jul 21, 2019
6468318
Satisfy travis
Not-Nik Jul 21, 2019
aed79e5
Add close function
Not-Nik Jul 21, 2019
a30dde8
Make it better
Not-Nik Jul 21, 2019
50d8735
Add resuming
Not-Nik Jul 23, 2019
e1423b8
Add very partial sharding
Not-Nik Jul 23, 2019
28a75c6
Added compression, better emitter /w tests
Zwork101 Jul 23, 2019
195e38f
Python 3.5 compatability fix
Zwork101 Jul 23, 2019
a8dcf48
Fixed ws connection and connection tests
Zwork101 Jul 23, 2019
1528129
Add BucketStore class and add basic crud methods
Stupremee Jul 31, 2019
33d6246
Create InMemoryBucketStore class and change method signature of store…
Stupremee Jul 31, 2019
abbb02e
Implement all methods for InMemoryBucketStore
Stupremee Jul 31, 2019
1e3399d
Add constructor parameter for bucket store in RateLimiter class
Stupremee Jul 31, 2019
102be40
Moved emitter to utils package
Zwork101 Aug 2, 2019
ea3c32d
Don't run tests on pull requests
vbe0201 Jul 28, 2019
abbca76
Fix spelling and some unit tests, refactor gateway package
vbe0201 Aug 3, 2019
2b76645
Added model base, along with snowflake and user
Zwork101 Aug 4, 2019
35bdeed
Remove Constructor parameter in Ratelimiter
Stupremee Aug 4, 2019
63f603b
Add constructor parameter bucket_store to RateLimiter
Stupremee Aug 4, 2019
d821fb7
Add documentation
Stupremee Aug 4, 2019
4f6b1c9
Begin to implement RedisBucketStore
Stupremee Aug 4, 2019
4d43e17
Started moving endpoint code into one file
Zwork101 Aug 6, 2019
5f09d58
Tranfered guild endpoint
Zwork101 Aug 6, 2019
c686d92
Transfered invite and oauth files
Zwork101 Aug 6, 2019
4b9b070
Transfered user and voice endpoints
Zwork101 Aug 6, 2019
f60f91d
Finished by transfering webhook endpoint
Zwork101 Aug 6, 2019
f0468f6
Finished User and Webhook, created cache and files
Zwork101 Aug 8, 2019
56bb845
Merge branches 'better-ratelimit-backend', 'gateway' and 'models' of …
vbe0201 Aug 8, 2019
83fe01e
Add support for the guild_subscriptions identify field
vbe0201 Aug 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ script:
- pylama .

# Unit tests
- python setup.py test
- if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then python setup.py test; fi

# Build docs
- make -C docs html
Expand Down
2 changes: 2 additions & 0 deletions clamor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
:license: MIT, see LICENSE for more details.
"""

from .gateway import *
from .meta import *
from .rest import *
from .utils import *

import logging

Expand Down
90 changes: 86 additions & 4 deletions clamor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
from asks.response_objects import Response

__all__ = (
'JSONErrorCode',
'ClamorError',
'RequestFailed',
'Unauthorized',
'EncodingFailed',
'Forbidden',
'NotFound',
'GatewayCloseCode',
'GatewayError',
'Hierarchied',
'InvalidListener',
'JSONErrorCode',
'NotFound',
'RequestFailed',
'Unauthorized',
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -147,6 +151,48 @@ def name(self) -> str:
return ' '.join(part.capitalize() for part in self._name_.split('_'))


class GatewayCloseCode(IntEnum):
"""Enum that holds the possible WebSocket close codes for gateway."""

#: Connection was closed gracefully (or heartbeats timed out).
OK = 1000
#: Connection was closed due to CloudFlare load balancing.
LOAD_BALANCING_CLOSURE = 1001
#: Random server error.
RANDOM_SERVER_ERROR = 1006

#: Unknown error.
UNKNOWN_ERROR = 4000
#: An invalid opcode or an invalid payload for an opcode was sent.
UNKNOWN_OPCODE = 4001
#: Server failed to decode a payload.
DECODE_ERROR = 4002
#: A payload was sent prior to identifying.
NOT_AUTHENTICATED = 4003
#: The token in the identify payload was incorrect.
AUTHENTICATION_FAILED = 4004
#: More than one identify payload was sent.
ALREADY_AUTHENTICATED = 4005
#: Attempted to resume an invalid session. Now unused, Op 9 is sent instead.
INVALID_SESSION_RESUMED = 4006
#: An invalid sequence was used for resuming.
INVALID_SEQUENCE = 4007
#: We are being rate limited.
RATE_LIMITED = 4008
#: The session timed out.
SESSION_TIMEOUT = 4009
#: Invalid shard was sent in the identify payload.
INVALID_SHARD = 4010
#: Too many guilds were to be handled by a single connection.
SHARDING_REQUIRED = 4011

@property
def name(self) -> str:
"""Returns a human-readable version of the enum member's name."""

return ' '.join(part.capitalize() for part in self._name_.split('_'))


class ClamorError(Exception):
"""Base exception class for any exceptions raised by this library.

Expand Down Expand Up @@ -291,3 +337,39 @@ class Hierarchied(ClamorError):
*Even occurs if the bot has ``Kick/Ban Members`` permissions.*
"""
pass


class GatewayError(ClamorError):
"""Base class for every error raised by gateway components.

Catching this error is not recommended as it mostly
indicates a client or server panic.
"""
pass


class EncodingFailed(GatewayError):
"""Raised when the encoding or decoding of a message fails.

Parameters
----------
err : Optional[str]
Error message returned by the encoder
data : Optional[Union[dict, str]]
Raw data of the message
"""

def __init__(self, err: Optional[str], data: Optional[Union[dict, str]] = None):
if data:
error = "Encoding of message {} failed".format(str(data))
else:
error = "Decoding of gateway message failed"
if error:
error += " with exception {}".format(err)

super().__init__(error)


class InvalidListener(GatewayError):
"""Raised by the emitter when a listener is not a coroutine."""
pass
4 changes: 4 additions & 0 deletions clamor/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .connector import *
from .encoding import *
from .opcodes import *
from .serialization import *
Loading