# Path: django_eventstream/utils.py
# def sse_encode_event(event_type, data, event_id=None, escape=False, json_encode=True):
# 	if json_encode:
# 		data_str = json.dumps(data, cls=DjangoJSONEncoder)
# 	else:
# 		data_str = data
# 	if escape:
# 		event_type = build_id_escape(event_type)
# 		data_str = build_id_escape(data_str)
# 	out = 'event: %s\n' % event_type
# 	if event_id:
# 		out += 'id: %s\n' % event_id
# 	out += 'data: %s\n\n' % data_str
# 	return out
# 
# def make_id(ids):
# 	id_parts = []
# 	for channel, id in six.iteritems(ids):
# 		enc_channel = quote(channel)
# 		id_parts.append('%s:%s' % (enc_channel, id))
# 	return ','.join(id_parts)
# 
# def build_id_escape(s):
# 	out = ''
# 	for c in s:
# 		if c == '%':
# 			out += '%%'
# 		else:
# 			out += c
# 	return out


import copy
import time
import jwt
import six
from gripcontrol import Channel
from django.conf import settings
from django.http import HttpResponse
from .utils import sse_encode_event, make_id, build_id_escape
	from urllib import quote
	from urllib.parse import quote

class EventResponse(object):
	def __init__(self):
		self.channel_items = {}
		self.channel_last_ids = {}
		self.channel_reset = set()
		self.channel_more = set()
		self.is_next = False
		self.is_recover = False
		self.user = None

	def to_http_response(self, http_request):
		last_ids = copy.deepcopy(self.channel_last_ids)
		event_id = make_id(last_ids)

		body = ''

		if not self.is_next:
			body += ':' + (' ' * 2048) + '\n\n'
			body += 'event: stream-open\ndata:\n\n'

		if len(self.channel_reset) > 0:
			body += sse_encode_event(
				'stream-reset',
				{'channels': list(self.channel_reset)},
				event_id=event_id)

		for channel, items in six.iteritems(self.channel_items):
			for item in items:
				last_ids[channel] = item.id

# Path: django_eventstream/utils.py
# def sse_encode_event(event_type, data, event_id=None, escape=False, json_encode=True):
# 	if json_encode:
# 		data_str = json.dumps(data, cls=DjangoJSONEncoder)
# 	else:
# 		data_str = data
# 	if escape:
# 		event_type = build_id_escape(event_type)
# 		data_str = build_id_escape(data_str)
# 	out = 'event: %s\n' % event_type
# 	if event_id:
# 		out += 'id: %s\n' % event_id
# 	out += 'data: %s\n\n' % data_str
# 	return out
# 
# def make_id(ids):
# 	id_parts = []
# 	for channel, id in six.iteritems(ids):
# 		enc_channel = quote(channel)
# 		id_parts.append('%s:%s' % (enc_channel, id))
# 	return ','.join(id_parts)
# 
# def build_id_escape(s):
# 	out = ''
# 	for c in s:
# 		if c == '%':
# 			out += '%%'
# 		else:
# 			out += c
# 	return out


# Path: django_eventstream/eventresponse.py
import copy
import time
import jwt
import six
from gripcontrol import Channel
from django.conf import settings
from django.http import HttpResponse
from .utils import sse_encode_event, make_id, build_id_escape
	from urllib import quote
	from urllib.parse import quote


class EventResponse(object):
	def __init__(self):
		self.channel_items = {}
		self.channel_last_ids = {}
		self.channel_reset = set()
		self.channel_more = set()
		self.is_next = False
		self.is_recover = False
		self.user = None

	def to_http_response(self, http_request):
		last_ids = copy.deepcopy(self.channel_last_ids)
		event_id = make_id(last_ids)

		body = ''

		if not self.is_next:
			body += ':' + (' ' * 2048) + '\n\n'
			body += 'event: stream-open\ndata:\n\n'

		if len(self.channel_reset) > 0:
			body += sse_encode_event(
				'stream-reset',
				{'channels': list(self.channel_reset)},
				event_id=event_id)

		for channel, items in six.iteritems(self.channel_items):
			for item in items:
				last_ids[channel] = item.id

				event_id = make_id(last_ids)