Skip to content

Commit

Permalink
Update api.py
Browse files Browse the repository at this point in the history
Enhanced get_coordinates Function:

The function now checks if the provided location is valid. If the location is invalid, it prints a message and falls back to the default location.
This ensures that the application does not break when an invalid location is provided.
  • Loading branch information
vaibhav-2703 authored Jul 26, 2024
1 parent 1c13898 commit ea5bf86
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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
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 +27,15 @@ 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 ea5bf86

Please sign in to comment.