Skip to content

Commit

Permalink
Support preflight requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnisbet committed Feb 19, 2024
1 parent cb7a69d commit b903632
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
5 changes: 4 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
This is a list of changes to Open Topo Data between each release.


## Version 1.8.4 (18 Aug 2023)
## Version 1.8.4 (19 Feb 2024)
* Dependency upgrades
* Fix handling of preflight requests ([#93](https://github.com/ajnisbet/opentopodata/issues/93))


## Version 1.8.3 (7 Feb 2023)

Expand Down
34 changes: 23 additions & 11 deletions opentopodata/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import os

from flask import Flask, jsonify, request
from flask import Flask, jsonify, request, Response
from flask_caching import Cache
import polyline

Expand Down Expand Up @@ -64,6 +64,18 @@ def _load_config_memcache():
return config.load_config()


@app.before_request
def handle_preflight():
# If before_request returns a non-none value, the regular view isn't run.
# after_request() does still run though, so the CORS header and OTD version
# will be set correctly there.
if request.method == "OPTIONS":
response = Response(status=204)
response.headers["access-control-allow-methods"] = "GET,POST,OPTIONS,HEAD"
response.headers["access-control-allow-headers"] = "content-type,x-api-key"
return response


@app.after_request
def apply_cors(response):
"""Set CORs header.
Expand All @@ -84,6 +96,16 @@ def apply_cors(response):
return response


@app.after_request
def add_version(response):
if "version" not in _SIMPLE_CACHE:
with open(VERSION_PATH) as f:
version = f.read().strip()
_SIMPLE_CACHE["version"] = version
response.headers["x-opentopodata-version"] = _SIMPLE_CACHE["version"]
return response


class ClientError(ValueError):
"""Invalid input data.
Expand Down Expand Up @@ -543,13 +565,3 @@ def get_elevation(dataset_name):
app.logger.error(e)
msg = "Unhandled server error, see server logs for details."
return jsonify({"status": "SERVER_ERROR", "error": msg}), 500


@app.after_request
def add_version(response):
if "version" not in _SIMPLE_CACHE:
with open(VERSION_PATH) as f:
version = f.read().strip()
_SIMPLE_CACHE["version"] = version
response.headers["x-opentopodata-version"] = _SIMPLE_CACHE["version"]
return response
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def test_no_cors(self, patch_config):
response = test_api.get(url)
assert response.headers.get("access-control-allow-origin") is None

def test_options(self):
test_api = api.app.test_client()
url = "/"
response = test_api.options(url)
assert response.status_code == 204
assert "x-opentopodata-version" in response.headers
assert "access-control-allow-methods" in response.headers
assert response.headers.get("access-control-allow-origin") == "*"


class TestFindRequestAgument:
def test_no_argument(self, patch_config):
Expand Down

0 comments on commit b903632

Please sign in to comment.