Skip to content

Commit

Permalink
feat: add post to allow methods for geocodeAddresses and suggest
Browse files Browse the repository at this point in the history
This was requested by CityWorks developers.
  • Loading branch information
stdavis committed Dec 12, 2023
1 parent f133e72 commit 5ebd6f6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
24 changes: 12 additions & 12 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
from .utils import WGS84, cleanse_text, get_out_spatial_reference, get_request_params

load_dotenv()

Expand Down Expand Up @@ -168,27 +168,29 @@ def geocode_map_server(path):
return f"no map server available for this service: {path}", 403


@app.route(f"{GEOCODE_SERVER_ROUTE}/suggest")
@app.route(f"{GEOCODE_SERVER_ROUTE}/suggest", methods=["GET", "POST"])
@as_json_p
def suggest():
"""provide single-line address suggestions"""

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

return {"suggestions": open_sgid.get_suggestions(cleanse_text(search_text), max_results)}


@app.route(f"{GEOCODE_SERVER_ROUTE}/findAddressCandidates")
@app.route(f"{GEOCODE_SERVER_ROUTE}/findAddressCandidates", methods=["GET", "POST"])
@as_json_p
def find_candidates():
"""get address candidates from address points (if there is a magic key) or
ugrc geocoding service
"""

magic_key = request.args.get("magicKey")
request_params = get_request_params(request)
magic_key = request_params.get("magicKey")

request_wkid, out_spatial_reference = get_out_spatial_reference(request)

Expand All @@ -199,8 +201,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.args.get("Single Line Input"))
max_locations = request.args.get("maxLocations")
single_line_address = cleanse_text(request_params.get("Single Line Input"))
max_locations = request_params.get("maxLocations")
try:
candidates = web_api.get_candidates_from_single_line(
single_line_address, out_spatial_reference, max_locations
Expand Down Expand Up @@ -230,10 +232,8 @@ def geocode_addresses():

request_wkid, out_spatial_reference = get_out_spatial_reference(request)

if request.method == "POST":
addresses = json.loads(request.form["addresses"])
else:
addresses = json.loads(request.args["addresses"])
request_params = get_request_params(request)
addresses = json.loads(request_params["addresses"])

locations = []

Expand Down
15 changes: 11 additions & 4 deletions src/masquerade/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ def cleanse_text(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"

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

return request_params


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)

if out_sr_param_name in request_params:
out_sr_param = request_params.get(out_sr_param_name)
try:
Expand Down
10 changes: 9 additions & 1 deletion tests/test_masquerade.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from unittest import mock

from flask import json

from masquerade.main import BASE_ROUTE, GEOCODE_SERVER_ROUTE


Expand Down Expand Up @@ -47,6 +46,15 @@ def test_find_candidates(get_candidate_mock, test_client):
assert response_json["candidates"][0] == "blah"
assert response_json["spatialReference"]["latestWkid"] == 3857

response = test_client.post(
f"{GEOCODE_SERVER_ROUTE}/findAddressCandidates",
data={"magicKey": "1-table", "outSR": '{"wkid": 102100}'},
)
response_json = json.loads(response.data)

assert response_json["candidates"][0] == "blah"
assert response_json["spatialReference"]["latestWkid"] == 3857


def test_find_candidates_invalid_magic_key(test_client):
response = test_client.get(f"{GEOCODE_SERVER_ROUTE}/findAddressCandidates?magicKey=invalid")
Expand Down

0 comments on commit 5ebd6f6

Please sign in to comment.