From 67d5020d068f920cf4180b0b3ddc8b114fae606c Mon Sep 17 00:00:00 2001 From: Anne Maier Date: Wed, 28 Aug 2024 14:27:20 +0200 Subject: [PATCH] response compression as instance method --- pyramid_oereb/core/views/webservice.py | 71 +++++++++++++------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/pyramid_oereb/core/views/webservice.py b/pyramid_oereb/core/views/webservice.py index 2b8ecfbc05..6352ffb6e6 100644 --- a/pyramid_oereb/core/views/webservice.py +++ b/pyramid_oereb/core/views/webservice.py @@ -76,7 +76,7 @@ def get_versions(self): if output_format == 'json': response.content_type = 'application/json; charset=UTF-8' response.extras = OerebStats(service='GetVersions', output_format=output_format) - return self.__response_compression__(self, response) + return self.response_compression(response) def get_capabilities(self): """ @@ -117,7 +117,7 @@ def get_capabilities(self): if output_format == 'json': response.content_type = 'application/json; charset=UTF-8' response.extras = OerebStats(service='GetCapabilities', output_format=output_format) - return self.__response_compression__(self, response) + return self.response_compression(response) def get_egrid(self): """ @@ -170,7 +170,7 @@ def get_egrid(self): params=dict(self._params), output_format=output_format ) - return self.__response_compression__(self, response) + return self.response_compression(response) def _get_egrid_coord(self, params): """ @@ -345,7 +345,38 @@ def get_extract_by_id(self): except AttributeError: response.extras = OerebStats(service='GetExtractById') - return self.__response_compression__(self, response) + return self.response_compression(response) + + + def response_compression(self, response): + """ + Returns a response object where the body is compressed using brotli or gzip, + depending on the accepted encoding. + + Args: + response (pyramid.response.Response): + the generated response. + + Returns: + pyramid.response.Response: The response. + """ + + if 'Accept-Encoding' in self._request.headers: + if 'br' in self._request.headers['Accept-Encoding']: + response.content_encoding = 'br' + compressed_response = brotli.compress(response.body) + response.content_length = str(len(compressed_response)) + response.body = compressed_response + elif 'gzip' in self._request.headers['Accept-Encoding']: + response.content_encoding = 'gzip' + gzip_buffer = io.BytesIO() + with gzip.GzipFile(mode='wb', fileobj=gzip_buffer) as gzip_file: + gzip_file.write(response.body) + response.body = gzip_buffer.getvalue() + response.content_length = str(len(response.body)) + + return response + def __validate_extract_params__(self): """ @@ -527,7 +558,7 @@ def __get_egrid_response__(self, records, params): response.extras = OerebStats(service='GetEGRID', output_format=output_format) - return self.__response_compression__(self, response) + return self.response_compression(response) @staticmethod def __parse_en__(en, buffer_dist=None): @@ -611,36 +642,6 @@ def __redirect_to_dynamic_client__(real_estate): raise HTTPInternalServerError('Missing configuration for redirect to dynamic client.') return HTTPSeeOther(url.format(**vars(real_estate))) - @staticmethod - def __response_compression__(self, response): - """ - Returns a response object where the body is compressed using brotli or gzip, - depending on the accepted encoding. - - Args: - response (pyramid.response.Response): - the generated response. - - Returns: - pyramid.response.Response: The response. - """ - - if 'Accept-Encoding' in self._request.headers: - if 'br' in self._request.headers['Accept-Encoding']: - response.content_encoding = 'br' - compressed_response = brotli.compress(response.body) - response.content_length = str(len(compressed_response)) - response.body = compressed_response - elif 'gzip' in self._request.headers['Accept-Encoding']: - response.content_encoding = 'gzip' - gzip_buffer = io.BytesIO() - with gzip.GzipFile(mode='wb', fileobj=gzip_buffer) as gzip_file: - gzip_file.write(response.body) - response.body = gzip_buffer.getvalue() - response.content_length = str(len(response.body)) - - return response - class Parameter(object): def __init__(self, response_format, with_geometry=False, images=False, signed=False, identdn=None,