Encode a Server-Sent Event (SSE).

    At least one field must be present. All fields are strings, except retry,
    which must be an integer. The event and id fields can not contain newlines.

def sse_event(event=None, data=None, id=None, retry=None, comment=None,
        encoding='utf-8'):
    """Encode a Server-Sent Event (SSE).

    At least one field must be present. All fields are strings, except retry,
    which must be an integer. The event and id fields can not contain newlines.
    """
    if all(x is None for x in [event, data, id, retry, comment]):
        raise TypeError("Event must have at least one field")
    if event and any(c in event for c in '\r\n'):
        raise ValueError("'event' can not contain newlines: '%s'" % event)
    if id and any(c in id for c in '\r\n'):
        raise ValueError("'id' can not contain newlines: '%s'" % id)
    return ''.join([
        _sse_encode('', comment) if comment is not None else '',
        _sse_encode('id', id) if id is not None else '',
        _sse_encode('event', event) if event is not None else '',
        _sse_encode('retry', str(int(retry))) if retry is not None else '',
        _sse_encode('data', data) if data is not None else '',
        '\n',
    ]).encode(encoding)