Skip to content

Commit

Permalink
fix: correctly get output spatial reference for POST requests
Browse files Browse the repository at this point in the history
Includes a refactor to move the helper function out into the utils module.
  • Loading branch information
stdavis committed Dec 12, 2023
1 parent c6e785b commit 5bf0b56
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
24 changes: 1 addition & 23 deletions src/masquerade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
from requests.models import HTTPError

from .providers import open_sgid, web_api
from .utils import cleanse_text
from .utils import WGS84, cleanse_text, get_out_spatial_reference

load_dotenv()

BASE_ROUTE = "/arcgis/rest"
GEOCODE_SERVER_ROUTE = f"{BASE_ROUTE}/services/UtahLocator/GeocodeServer"
ADMIN_BASE_ROUTE = "/arcgis/admin"
WGS84 = 4326
WEB_MERCATOR = 3857
OLD_WEB_MERCATOR = 102100
SERVER_VERSION_MAJOR = 10
SERVER_VERSION_MINOR = 8
SERVER_VERSION_PATCH = 1
Expand Down Expand Up @@ -226,25 +223,6 @@ def find_candidates():
}


def get_out_spatial_reference(incoming_request):
"""get the desired output spatial reference from the request"""
out_sr_param_name = "outSR"
if out_sr_param_name in incoming_request.args:
out_sr_param = incoming_request.args.get(out_sr_param_name)
try:
request_wkid = int(out_sr_param)
except ValueError:
request_wkid = json.loads(out_sr_param)["wkid"]
else:
request_wkid = WGS84

#: switch out old mercator for new one otherwise, pass it through
return (
request_wkid,
WEB_MERCATOR if request_wkid == OLD_WEB_MERCATOR else request_wkid,
)


@app.route(f"{GEOCODE_SERVER_ROUTE}/geocodeAddresses", methods=["GET", "POST"])
@as_json_p
def geocode_addresses():
Expand Down
30 changes: 30 additions & 0 deletions src/masquerade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"""
Utility functions
"""
import json

WGS84 = 4326
WEB_MERCATOR = 3857
OLD_WEB_MERCATOR = 102100


def cleanse_text(text):
Expand All @@ -14,3 +19,28 @@ def cleanse_text(text):
return text

return text.strip().replace('"', "").replace("'", "")


def get_out_spatial_reference(incoming_request):
"""get the desired output spatial reference from the request"""
out_sr_param_name = "outSR"

if incoming_request.method == "GET":
request_params = incoming_request.args
else:
request_params = incoming_request.form

if out_sr_param_name in request_params:
out_sr_param = request_params.get(out_sr_param_name)
try:
request_wkid = int(out_sr_param)
except ValueError:
request_wkid = json.loads(out_sr_param)["wkid"]
else:
request_wkid = WGS84

#: switch out old mercator for new one otherwise, pass it through
return (
request_wkid,
WEB_MERCATOR if request_wkid == OLD_WEB_MERCATOR else request_wkid,
)
36 changes: 35 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from masquerade.utils import cleanse_text
from unittest.mock import MagicMock

from masquerade.utils import cleanse_text, get_out_spatial_reference


def test_removes_spaces():
Expand All @@ -14,3 +16,35 @@ def test_cleanse_text_without_strings():
assert cleanse_text(None) is None
assert cleanse_text(1) == 1
assert cleanse_text({}) == {}


def test_get_out_spatial_reference_get_request():
request = MagicMock()
request.method = "GET"
request.args = {"outSR": 3857}

assert get_out_spatial_reference(request) == (3857, 3857)


def test_get_out_spatial_reference_get_request_json():
request = MagicMock()
request.method = "GET"
request.args = {"outSR": '{"wkid": 3857}'}

assert get_out_spatial_reference(request) == (3857, 3857)


def test_get_out_spatial_reference_default():
request = MagicMock()
request.method = "GET"
request.args = {}

assert get_out_spatial_reference(request) == (4326, 4326)


def test_get_out_spatial_reference_post_request():
request = MagicMock()
request.method = "POST"
request.form = {"outSR": 3857}

assert get_out_spatial_reference(request) == (3857, 3857)

0 comments on commit 5bf0b56

Please sign in to comment.