Skip to content

Instantly share code, notes, and snippets.

@reallistic
Last active September 2, 2017 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reallistic/c42c1235aacdf657922e3a4bf9f1d9af to your computer and use it in GitHub Desktop.
Save reallistic/c42c1235aacdf657922e3a4bf9f1d9af to your computer and use it in GitHub Desktop.
Push notifications model in Python
from .payload import PayloadFactory, Payload
class Comment:
text: str
def __init__(self, text: str) -> None:
self.text = text
@staticmethod
def get_payload_type() -> str:
return 'comment'
def create(text: str) -> Comment:
comment = Comment(text)
return comment
def send(comment: Comment, user_agent: str) -> bool:
payload: Payload = PayloadFactory.make_payload(comment)
return payload.send(user_agent)
class Payload(object):
def __init__(self, *args, **kwargs):
pass
@staticmethod
def parse_emojis(text):
pass
def to_apns(self):
pass
def supports_platform(self, user_agent: str):
pass
def send(self, user_agent: str):
if self.supports_platform(user_agent):
# TransportMethod.send(self.to_apns())
return True
return False
class CommentPayload(Payload):
comment = None
def __init__(self, comment, *args, **kwargs):
self.comment = comment
super().__init__(*args, **kwargs)
def supports_platform(self, user_agent: str):
return 'IOS ' in user_agent
class MessagePayload(Payload):
message = None
def __init__(self, message, *args, **kwargs):
self.message = message
super().__init__(*args, **kwargs)
def supports_platform(self, user_agent: str):
return True
class PayloadFactory(object):
types: Dict[str, Any] = {
'comment': CommentPayload,
'message': MessagePayload
}
@classmethod
def make_payload(cls, publishable, *args, **kwargs) -> Payload:
try:
payload_cls = cls.get_types().get(publishable.get_payload_type())
except AttributeError:
payload_cls = None
if payload_cls:
return payload_cls(publishable, *args, **kwargs)
return Payload(*args, **kwargs)
@classmethod
def get_types(cls) -> Dict[str, Any]:
return cls.types
@classmethod
def get_class(cls, type_str: str) -> Any:
type_map = cls.get_types()
return type_map.get(type_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment