import six
import gzip


def compress_response(response, compression_level):
	"""Compress pycsw's response with gzip

	Parameters
	----------
	response: str
		The already processed CSW request
	compression_level: int
		Level of compression to use in gzip algorithm

	Returns
	-------
	bytes
		The full binary contents of the compressed response
	dict
		Extra HTTP headers that are useful for the response

	"""

	buf = six.BytesIO()
	gzipfile = gzip.GzipFile(mode='wb', fileobj=buf,
							 compresslevel=compression_level)
	gzipfile.write(response)
	gzipfile.close()
	compressed_response = buf.getvalue()
	compression_headers = {'Content-Encoding': 'gzip'}
	return compressed_response, compression_headers