import logging
import requests
import json


def geocode_address(address):
    """Use World Geocoder to get XY for one address at a time."""
    
    logging.info("Geocoding " + address)
    
    querystring = {
        "f": "json",
        "singleLine": address}
    url = "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates"
    response = requests.request("GET", url, params=querystring)
    p = response.text
    j = json.loads(p)
    
    try:
        location = j['candidates'][0]['location']  # returns first location as X, Y
        logging.info("Geocoded results: {}".format(str(location)))
            
        return location
    except IndexError:
        logging.error("Index Error on {}".format(address))
        return "IndexError"