Added a stream class for use with the logging module.

License assent:
    https://github.com/zulip/python-zulip/pull/3#issuecomment-18182458

(imported from commit 9faf9dd147032b1e56b113bc0f0d729a653e1e49)
This commit is contained in:
Rory Kirchner 2013-10-30 15:56:43 -04:00 committed by Luke Faraone
parent b0f73806de
commit fb5a3dc3d8
2 changed files with 39 additions and 0 deletions

View file

@ -31,6 +31,7 @@ import optparse
from distutils.version import LooseVersion
from ConfigParser import SafeConfigParser
import logging
__version__ = "0.2.1"
@ -293,6 +294,27 @@ def _mk_events(event_types=None):
def _kwargs_to_dict(**kwargs):
return kwargs
class ZulipStream(object):
"""
A Zulip stream-like object
"""
def __init__(self, type, to, subject, **kwargs):
self.client = Client(**kwargs)
self.type = type
self.to = to
self.subject = subject
def write(self, content):
message = {"type": self.type,
"to": self.to,
"subject": self.subject,
"content": content}
self.client.send_message(message)
def flush(self):
pass
Client._register('send_message', url='messages', make_request=(lambda request: request))
Client._register('update_message', method='PATCH', url='messages', make_request=(lambda request: request))
Client._register('get_messages', method='GET', url='messages/latest', longpolling=True)