Passing values from multiple lists through an API

39 Views Asked by At

I am trying to calculate the driving distance between a location and a train station. I have four lists of data containing the location latitude, the location longitude, the station latitude and station longitude.

I'd like to take a value from each of these and pass it through an API that calculates driving distance.

I have made the following function, which gives the distance in miles:

def get_driving_distance(lat1,lon1,lat2,lon2):
    r = requests.get(f"http://router.project-osrm.org/route/v1/car/{lon1},{lat1};{lon2},{lat2}?overview=false""")
    routes = json.loads(r.content)
    route_1 = routes.get("routes")[0]
    driving_distance = route_1['distance']
    return driving_distance/1609.34

Ultimately I'd like a dataframe which shows these four lists (one per column), with a fifth column showing the driving distance.

How do I do this? Everything I've tried keeps failing.

It would be useful to put a timer delay in the request to the API, so it only makes 500 calls before pausing for 5 seconds

(Sorry appreciate this is probably super basic, but I'm new to Python and self-taught)

1

There are 1 best solutions below

0
On

You can use a simple contextmanager to check a counter and sleep if the counter is too high.

from contextlib import contextmanager
from time import sleep

api_calls = 0

@contextmanager
def rate_limited(limit=500, delay=5):
    global api_calls
    if api_calls + 1 >= limit:
        sleep(delay)
        api_calls -= 500
    api_calls += 1
    yield

def get_driving_distance(lat1,lon1,lat2,lon2):
    with rate_limited():
        r = requests.get(f"http://router.project-osrm.org/route/v1/car/{lon1},{lat1};{lon2},{lat2}?overview=false""")
    routes = json.loads(r.content)
    route_1 = routes.get("routes")[0]
    driving_distance = route_1['distance']
    return driving_distance/1609.34

If you make a lot of requests. It is generally a good idea to check the http status code before parsing the response. One easy way provided by the requests library is Response.raise_for_status. This will raise an exception if an error occurred. Another way is checking the Response.status_code directly, in that case you probably want to use the http.HTTPStatus provided in the standard library. to help your code solve the network issue correctly.