From ea5bf860a3540f8c0c16fe8b3041ac45ae431111 Mon Sep 17 00:00:00 2001 From: Vaibhav Chouhan <152781960+vaibhav-2703@users.noreply.github.com> Date: Fri, 26 Jul 2024 08:07:36 +0530 Subject: [PATCH] Update api.py 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. --- src/api.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/api.py b/src/api.py index 641ef06..daa3111 100644 --- a/src/api.py +++ b/src/api.py @@ -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) @@ -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)