Make all models frozen, and sessions hashable
This commit is contained in:
@@ -27,12 +27,7 @@ class Client:
|
||||
"""
|
||||
|
||||
#: The session to use when making requests.
|
||||
_session = attr.ib(type=_session.Session)
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
"""The session that's used when making requests."""
|
||||
return self._session
|
||||
session = attr.ib(type=_session.Session)
|
||||
|
||||
def fetch_users(self) -> Sequence[_user.UserData]:
|
||||
"""Fetch users the client is currently chatting with.
|
||||
|
@@ -8,11 +8,10 @@ log = logging.getLogger("fbchat")
|
||||
kw_only = sys.version_info[:2] > (3, 5)
|
||||
|
||||
#: Default attrs settings for classes
|
||||
attrs_default = attr.s(slots=True, kw_only=kw_only)
|
||||
attrs_default = attr.s(frozen=True, slots=True, kw_only=kw_only)
|
||||
|
||||
|
||||
# Frozen, so that it can be used in sets
|
||||
@attr.s(frozen=True, slots=True, kw_only=kw_only)
|
||||
@attrs_default
|
||||
class Image:
|
||||
#: URL to the image
|
||||
url = attr.ib(type=str)
|
||||
|
@@ -4,10 +4,7 @@ import requests
|
||||
from typing import Any
|
||||
|
||||
# Not frozen, since that doesn't work in PyPy
|
||||
attrs_exception = attr.s(slots=True, auto_exc=True)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class FacebookError(Exception):
|
||||
"""Base class for all custom exceptions raised by ``fbchat``.
|
||||
|
||||
@@ -18,7 +15,7 @@ class FacebookError(Exception):
|
||||
message = attr.ib(type=str)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class HTTPError(FacebookError):
|
||||
"""Base class for errors with the HTTP(s) connection to Facebook."""
|
||||
|
||||
@@ -31,7 +28,7 @@ class HTTPError(FacebookError):
|
||||
return "Got {} response: {}".format(self.status_code, self.message)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class ParseError(FacebookError):
|
||||
"""Raised when we fail parsing a response from Facebook.
|
||||
|
||||
@@ -49,7 +46,7 @@ class ParseError(FacebookError):
|
||||
return msg.format(self.message, self.data)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class ExternalError(FacebookError):
|
||||
"""Base class for errors that Facebook return."""
|
||||
|
||||
@@ -64,7 +61,7 @@ class ExternalError(FacebookError):
|
||||
return "{}: {}".format(self.message, self.description)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class GraphQLError(ExternalError):
|
||||
"""Raised by Facebook if there was an error in the GraphQL query."""
|
||||
|
||||
@@ -79,7 +76,7 @@ class GraphQLError(ExternalError):
|
||||
return super().__str__()
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class InvalidParameters(ExternalError):
|
||||
"""Raised by Facebook if:
|
||||
|
||||
@@ -89,14 +86,14 @@ class InvalidParameters(ExternalError):
|
||||
"""
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class NotLoggedIn(ExternalError):
|
||||
"""Raised by Facebook if the client has been logged out."""
|
||||
|
||||
code = attr.ib(1357001)
|
||||
|
||||
|
||||
@attrs_exception
|
||||
@attr.s(slots=True, auto_exc=True)
|
||||
class PleaseRefresh(ExternalError):
|
||||
"""Raised by Facebook if the client has been inactive for too long.
|
||||
|
||||
|
@@ -115,7 +115,7 @@ def get_error_data(html: str, url: str) -> Tuple[Optional[int], Optional[str]]:
|
||||
return code, soup.get_text() or None
|
||||
|
||||
|
||||
@attr.s(slots=True, kw_only=kw_only, repr=False)
|
||||
@attr.s(slots=True, kw_only=kw_only, repr=False, eq=False)
|
||||
class Session:
|
||||
"""Stores and manages state required for most Facebook requests.
|
||||
|
||||
|
@@ -1,13 +1,9 @@
|
||||
import pytest
|
||||
import fbchat
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def session():
|
||||
class FakeSession:
|
||||
# TODO: Add a further mocked session
|
||||
user_id = "31415926536"
|
||||
|
||||
def __repr__(self):
|
||||
return "<FakeSession>"
|
||||
|
||||
return FakeSession()
|
||||
return fbchat.Session(
|
||||
user_id="31415926536", fb_dtsg=None, revision=None, session=None
|
||||
)
|
||||
|
Reference in New Issue
Block a user