Skip to content

Commit

Permalink
making sure cors makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcruse3 committed Oct 26, 2023
1 parent cc601e1 commit 4bf009e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion acai_aws/apigateway/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def requirements(**kwargs):
def decorator_func(func):

def raise_timeout(*_):
raise ApiTimeOutException
raise ApiTimeOutException()

def start_timeout(timeout=None):
if kwargs.get('timeout') is not None or timeout is not None:
Expand Down
22 changes: 15 additions & 7 deletions acai_aws/apigateway/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

class Response:

def __init__(self):
def __init__(self, **kwargs):
self.__code = 200
self.__is_json = True
self.__open_cors = True
self.__cors = kwargs.get('cors', True)
self.__base64_encoded = False
self.__compress = False
self.__content_type = ''
Expand All @@ -19,8 +19,8 @@ def __init__(self):

@property
def headers(self):
if self.open_cors:
self.__set_open_cors()
if self.cors:
self.__set_cors()
return self.__headers

@headers.setter
Expand All @@ -38,13 +38,21 @@ def content_type(self):
def content_type(self, content_type ):
self.__content_type = content_type

@property
def cors(self):
return self.__cors

@cors.setter
def cors(self, access):
self.__cors = access

@property
def open_cors(self):
return self.__open_cors
return self.__cors

@open_cors.setter
def open_cors(self, access):
self.__open_cors = access
self.__cors = access

@property
def base64_encoded(self):
Expand Down Expand Up @@ -123,7 +131,7 @@ def __compress_body(self, body):
file.write(body.encode('utf-8'))
return base64.b64encode(bytes_io.getvalue()).decode('ascii')

def __set_open_cors(self):
def __set_cors(self):
self.__headers['Access-Control-Allow-Origin'] = '*'
self.__headers['Access-Control-Allow-Headers'] = '*'

Expand Down
3 changes: 2 additions & 1 deletion acai_aws/apigateway/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, **kwargs):
self.__with_auth = kwargs.get('with_auth')
self.__on_error = kwargs.get('on_error')
self.__on_timeout = kwargs.get('on_timeout')
self.__cors = kwargs.get('cors', True)
self.__timeout = kwargs.get('timeout', None)
self.__output_error = kwargs.get('output_error', False)
self.__verbose = kwargs.get('verbose_logging', False)
Expand All @@ -32,7 +33,7 @@ def auto_load(self):

def route(self, event, context):
request = Request(event, context, self.__timeout)
response = Response()
response = Response(cors=self.__cors)
try:
self.__log_verbose(title='request-received', log={'request': request})
self.__run_route_procedure(request, response)
Expand Down
4 changes: 4 additions & 0 deletions tests/acai_aws/apigateway/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def test_default_headers(self):
)

def test_closed_cors_headers(self):
self.response.cors = False
self.assertDictEqual(self.response.headers, {})

def test_closed_open_cors_headers(self):
self.response.open_cors = False
self.assertDictEqual(self.response.headers, {})

Expand Down

0 comments on commit 4bf009e

Please sign in to comment.