diff --git a/Dockerfile b/Dockerfile index 4c464fdf..d250560a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - # # Read more on Dockerfile best practices at the source: # https://docs.docker.com/develop/develop-images/dockerfile_best-practices -RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs +RUN apt-get update && apt-get install -y --no-install-recommends postgresql-client nodejs npm # Inside the container, create an app directory and switch into it RUN mkdir /app diff --git a/parserator_web/static/js/index.js b/parserator_web/static/js/index.js index 492674cc..00eea677 100644 --- a/parserator_web/static/js/index.js +++ b/parserator_web/static/js/index.js @@ -1,2 +1,28 @@ /* TODO: Flesh this out to connect the form to the API and render results in the #address-results div. */ + document.addEventListener('DOMContentLoaded', () => { + document.getElementById('submit').addEventListener('click', async (e) => { + const inputString = document.getElementById('address').value; + const url = `api/parse/?input_string=${encodeURIComponent(inputString)}`; + + try { + const response = await fetch(url, { + method: "GET", + headers: { + 'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value, + } + }); + + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + + const data = await response.json(); + console.log(data); + } catch (error) { + console.error('Error:', error); + } + }, false); + }); + + \ No newline at end of file diff --git a/parserator_web/views.py b/parserator_web/views.py index 0be3f4a9..cebb9c4a 100644 --- a/parserator_web/views.py +++ b/parserator_web/views.py @@ -16,9 +16,13 @@ class AddressParse(APIView): def get(self, request): # TODO: Flesh out this method to parse an address string using the # parse() method and return the parsed components to the frontend. - return Response({}) + address_components, address_type = AddressParse.parse(request.input_string) + input_string = request.input_string + return Response({input_string, address_components, address_type}) def parse(self, address): # TODO: Implement this method to return the parsed components of a # given address using usaddress: https://github.com/datamade/usaddress + address_components, address_type = zip(usaddress.parse(address)) return address_components, address_type + diff --git a/tests/test_views.py b/tests/test_views.py index bfd5d0b7..ac8c6b08 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,15 +1,19 @@ import pytest +import urllib.parse def test_api_parse_succeeds(client): # TODO: Finish this test. Send a request to the API and confirm that the # data comes back in the appropriate format. address_string = '123 main st chicago il' - pytest.fail() + client.get('api/parse/', {'input_string': address_string}) + # assert client.get(url) + # pytest.fail() def test_api_parse_raises_error(client): # TODO: Finish this test. The address_string below will raise a # RepeatedLabelError, so ParseAddress.parse() will not be able to parse it. address_string = '123 main st chicago il 123 main st' - pytest.fail() + client.get('api/parse/', {'input_string': address_string}) + # pytest.fail()