From 7e51085b1a08f5e97493b2d56694e5cb98edafcc Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 1 Sep 2022 22:48:38 -0400 Subject: [PATCH] Updated to allow query by zip code & improved error handling --- server/location.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/server/location.go b/server/location.go index 3989cfe..6835832 100644 --- a/server/location.go +++ b/server/location.go @@ -21,11 +21,26 @@ type Coordinates struct { // Receives a gRPC request for Location // Returns a SendLocation message with the Latitude and Longitude func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.SendLocation, error) { - log.Println("'Location' function called...") + log.Printf("'Location' called, location: %v\n", in.Location) - lat, lon, err := getLocation(in.Location.String(), s.ApiKey) + var err error + var lat, lon float32 + + switch in.LocationType { + case pb.LocationType_LOCATION_TYPE_CITY: + lat, lon, err = getLocation(in.Location.GetCity(), s.ApiKey) + case pb.LocationType_LOCATION_TYPE_ZIP_CODE: + lat, lon, err = getZipLocation(in.Location.GetZipCode(), s.ApiKey) + default: + lat, lon, err = getLocation(in.Location.String(), s.ApiKey) + } if err != nil { - return nil, fmt.Errorf("Error: %v\n", err) + return nil, status.Errorf( + codes.InvalidArgument, + fmt.Sprintf("Invalid location or location type: %s, %s\n", + in.Location.String(), + in.LocationType.String()), + ) } return &pb.SendLocation{ @@ -38,7 +53,7 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send // Receives the city name and the server's API key // Returns the latitude and longitude for the given location func getLocation(location string, key string) (float32, float32, error) { - log.Println("'getLocation' function called...") + log.Printf("'getLocation' called, location: %v\n", location) url := "http://api.openweathermap.org/geo/1.0/direct?q=" token := "&appid=" + key @@ -70,7 +85,7 @@ func getLocation(location string, key string) (float32, float32, error) { } func getZipLocation(zip string, key string) (float32, float32, error) { - log.Println("'getZipLocation' function called...") + log.Printf("'getZipLocation' called, zip code: %v\n", zip) url := "https://api.openweathermap.org/geo/1.0/zip?zip=" token := "&appid=" + key