From ab071e1cbed0b5c50f2e557fd64f8634d73a9fcd Mon Sep 17 00:00:00 2001 From: Vaibhav Chouhan <152781960+vaibhav-2703@users.noreply.github.com> Date: Sat, 27 Jul 2024 12:20:31 +0530 Subject: [PATCH] Update api.py 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. --- src/api.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/api.py b/src/api.py index 641ef06..12b730f 100644 --- a/src/api.py +++ b/src/api.py @@ -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) @@ -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)