Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
Handling invalid location - The get_coordinates function now prints a user-friendly message and defaults to the user's location if an invalid location is provided.
  • Loading branch information
vaibhav-2703 authored Jul 27, 2024
1 parent 36c91ce commit ab071e1
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

def get_coordinates(args):
"""
Takes a location(city or address) and returns the coordinates: [lat, long]
If no location is specified, default_location() finds the users coordinates
Takes a location (city or address) and returns the coordinates: [lat, long]
If no location is specified or the location is invalid, default_location()
finds the user's coordinates.
"""
for arg in args:
arg_str = str(arg)
Expand All @@ -27,13 +28,18 @@ def get_coordinates(args):
location = geolocator.geocode(address)
if location is not None:
return [location.latitude, location.longitude, location]
return "No data"
else:
print(
f"Invalid location '{address}' provided. "
"Using default location."
)
return default_location()
return default_location()


def default_location():
"""
If no location specified in cli, find users location
If no location specified in cli, find user's location
Make a GET request to the API endpoint
"""
response = requests.get("https://ipinfo.io/json", timeout=10)
Expand Down

0 comments on commit ab071e1

Please sign in to comment.