Skip to content

Commit b903632

Browse files
committed
Support preflight requests
1 parent cb7a69d commit b903632

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

docs/changelog.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
This is a list of changes to Open Topo Data between each release.
44

55

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

811
## Version 1.8.3 (7 Feb 2023)
912

opentopodata/api.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import os
33

4-
from flask import Flask, jsonify, request
4+
from flask import Flask, jsonify, request, Response
55
from flask_caching import Cache
66
import polyline
77

@@ -64,6 +64,18 @@ def _load_config_memcache():
6464
return config.load_config()
6565

6666

67+
@app.before_request
68+
def handle_preflight():
69+
# If before_request returns a non-none value, the regular view isn't run.
70+
# after_request() does still run though, so the CORS header and OTD version
71+
# will be set correctly there.
72+
if request.method == "OPTIONS":
73+
response = Response(status=204)
74+
response.headers["access-control-allow-methods"] = "GET,POST,OPTIONS,HEAD"
75+
response.headers["access-control-allow-headers"] = "content-type,x-api-key"
76+
return response
77+
78+
6779
@app.after_request
6880
def apply_cors(response):
6981
"""Set CORs header.
@@ -84,6 +96,16 @@ def apply_cors(response):
8496
return response
8597

8698

99+
@app.after_request
100+
def add_version(response):
101+
if "version" not in _SIMPLE_CACHE:
102+
with open(VERSION_PATH) as f:
103+
version = f.read().strip()
104+
_SIMPLE_CACHE["version"] = version
105+
response.headers["x-opentopodata-version"] = _SIMPLE_CACHE["version"]
106+
return response
107+
108+
87109
class ClientError(ValueError):
88110
"""Invalid input data.
89111
@@ -543,13 +565,3 @@ def get_elevation(dataset_name):
543565
app.logger.error(e)
544566
msg = "Unhandled server error, see server logs for details."
545567
return jsonify({"status": "SERVER_ERROR", "error": msg}), 500
546-
547-
548-
@app.after_request
549-
def add_version(response):
550-
if "version" not in _SIMPLE_CACHE:
551-
with open(VERSION_PATH) as f:
552-
version = f.read().strip()
553-
_SIMPLE_CACHE["version"] = version
554-
response.headers["x-opentopodata-version"] = _SIMPLE_CACHE["version"]
555-
return response

tests/test_api.py

+9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def test_no_cors(self, patch_config):
4141
response = test_api.get(url)
4242
assert response.headers.get("access-control-allow-origin") is None
4343

44+
def test_options(self):
45+
test_api = api.app.test_client()
46+
url = "/"
47+
response = test_api.options(url)
48+
assert response.status_code == 204
49+
assert "x-opentopodata-version" in response.headers
50+
assert "access-control-allow-methods" in response.headers
51+
assert response.headers.get("access-control-allow-origin") == "*"
52+
4453

4554
class TestFindRequestAgument:
4655
def test_no_argument(self, patch_config):

0 commit comments

Comments
 (0)