Skip to content

Commit

Permalink
fix: support requests that use url params or form data independent of…
Browse files Browse the repository at this point in the history
… the method
  • Loading branch information
stdavis committed Dec 12, 2023
1 parent cd105d1 commit 1900f23
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
17 changes: 7 additions & 10 deletions src/masquerade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from requests.models import HTTPError

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

load_dotenv()

Expand Down Expand Up @@ -173,9 +173,8 @@ def geocode_map_server(path):
def suggest():
"""provide single-line address suggestions"""

request_params = get_request_params(request)
search_text = request_params.get("text")
max_results = request_params.get("maxSuggestions") or DEFAULT_MAX_SUGGESTIONS
search_text = get_request_param(request, "text")
max_results = get_request_param(request, "maxSuggestions") or DEFAULT_MAX_SUGGESTIONS
if isinstance(max_results, str):
max_results = DEFAULT_MAX_SUGGESTIONS

Expand All @@ -189,8 +188,7 @@ def find_candidates():
ugrc geocoding service
"""

request_params = get_request_params(request)
magic_key = request_params.get("magicKey")
magic_key = get_request_param(request, "magicKey")

request_wkid, out_spatial_reference = get_out_spatial_reference(request)

Expand All @@ -201,8 +199,8 @@ def find_candidates():
candidate = open_sgid.get_candidate_from_magic_key(magic_key, out_spatial_reference)
candidates = [candidate]
else:
single_line_address = cleanse_text(request_params.get("Single Line Input"))
max_locations = request_params.get("maxLocations")
single_line_address = cleanse_text(get_request_param(request, "Single Line Input"))
max_locations = get_request_param(request, "maxLocations")
try:
candidates = web_api.get_candidates_from_single_line(
single_line_address, out_spatial_reference, max_locations
Expand Down Expand Up @@ -232,8 +230,7 @@ def geocode_addresses():

request_wkid, out_spatial_reference = get_out_spatial_reference(request)

request_params = get_request_params(request)
addresses = json.loads(request_params["addresses"])
addresses = json.loads(get_request_param(request, "addresses"))

locations = []

Expand Down
21 changes: 9 additions & 12 deletions src/masquerade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,25 @@ def cleanse_text(text):
return text.strip().replace('"', "").replace("'", "")


def get_request_params(incoming_request):
"""get the request parameters from the request"""
if incoming_request.method == "GET":
request_params = incoming_request.args
else:
request_params = incoming_request.form
def get_request_param(request, param_name):
"""get the parameter from the request checking both url params and form data"""
if param_name in request.args:
return request.args.get(param_name)

return request_params
return request.form.get(param_name)


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

request_params = get_request_params(incoming_request)
param_value = get_request_param(incoming_request, out_sr_param_name)

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

Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_get_out_spatial_reference_default():
request = MagicMock()
request.method = "GET"
request.args = {}
request.form = {}

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

Expand Down

0 comments on commit 1900f23

Please sign in to comment.