Skip to content

Commit

Permalink
compress all responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Anne Maier committed Aug 27, 2024
1 parent 87b1062 commit 4b1e76e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
Binary file added .coverage.8674b3a12c0a.9.XzQXStzx
Binary file not shown.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DEV_CREATE_OEREBLEX_TABLES_SCRIPT = $(VENV_BIN)/create_oereblex_tables

MODEL_PK_TYPE_IS_STRING ?= true

PRINT_URL ?= http://oereb-print:8080/print/oereb
PRINT_URL ?= http://svamaa-print-1:8080/print/oereb

# ********************
# Variable definitions
Expand Down
4 changes: 2 additions & 2 deletions dev/config/create_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def _create_yaml_config_(
name='pyramid_oereb.yml',
database='postgresql://postgres:password@localhost/pyramid_oereb',
print_url='http://oereb-print:8080/print/oereb'):
print_url='http://svamaa-print-1:8080/print/oereb'):
"""
Creates the specified YAML file using a template. This YAML file contains the standard
configuration to run a oereb server out of the box.
Expand Down Expand Up @@ -61,7 +61,7 @@ def create_yaml():
dest='print_url',
metavar='PRINT_URL',
type='string',
default='http://oereb-print:8080/print/oereb',
default='http://svamaa-print-1:8080/print/oereb',
help='The URL of the print server'
)
options, args = parser.parse_args()
Expand Down
10 changes: 10 additions & 0 deletions development.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ pyramid.default_locale_name = en
pyramid_oereb.cfg.file = %(here)s/pyramid_oereb.yml
pyramid_oereb.cfg.section = pyramid_oereb

# Enable debug toolbar
pyramid.includes = pyramid_debugtoolbar
debugtoolbar.hosts = 0.0.0.0/0
debugtoolbar.enabled = true
debugtoolbar.intercept_exc = display
debugtoolbar.max_request_history = 100
debugtoolbar.panels = headers logging performance renderings
request_vars sqlalchemy traceback


sqlalchemy.url = sqlite:///%(here)s/pyramid_oereb.sqlite

###
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ recommend = [
"requests==2.32.3",
"geolink-formatter==2.0.5",
"pyconizer==0.1.4",
"c2cwsgiutils[standard]==6.0.8"]
"c2cwsgiutils[standard]==6.0.8",
"Brotli==1.1.0"]
no-version = [
"pypdf",
"filetype",
Expand Down
41 changes: 36 additions & 5 deletions pyramid_oereb/core/views/webservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# import yappi
import qrcode
import io
import brotli
import gzip
# import re

from pyramid.httpexceptions import HTTPBadRequest, HTTPSeeOther, HTTPInternalServerError, HTTPNoContent, \
Expand Down Expand Up @@ -74,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 response
return self.__response_compression__(self, response)

def get_capabilities(self):
"""
Expand Down Expand Up @@ -115,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 response
return self.__response_compression__(self, response)

def get_egrid(self):
"""
Expand Down Expand Up @@ -168,7 +170,7 @@ def get_egrid(self):
params=dict(self._params),
output_format=output_format
)
return response
return self.__response_compression__(self, response)

def _get_egrid_coord(self, params):
"""
Expand Down Expand Up @@ -342,7 +344,8 @@ def get_extract_by_id(self):
response.extras = OerebStats(service='GetExtractById', params={'error': response.message})
except AttributeError:
response.extras = OerebStats(service='GetExtractById')
return response

return self.__response_compression__(self, response)

def __validate_extract_params__(self):
"""
Expand Down Expand Up @@ -523,7 +526,8 @@ def __get_egrid_response__(self, records, params):
)

response.extras = OerebStats(service='GetEGRID', output_format=output_format)
return response

return self.__response_compression__(self, response)

@staticmethod
def __parse_en__(en, buffer_dist=None):
Expand Down Expand Up @@ -607,6 +611,33 @@ 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 '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 4b1e76e

Please sign in to comment.