Skip to content

Commit

Permalink
response compression as instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
Anne Maier committed Aug 28, 2024
1 parent bab9189 commit 67d5020
Showing 1 changed file with 36 additions and 35 deletions.
71 changes: 36 additions & 35 deletions pyramid_oereb/core/views/webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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))

Check warning on line 376 in pyramid_oereb/core/views/webservice.py

View check run for this annotation

Codecov / codecov/patch

pyramid_oereb/core/views/webservice.py#L365-L376

Added lines #L365 - L376 were not covered by tests

return response


def __validate_extract_params__(self):
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 67d5020

Please sign in to comment.