From f36a005ad103287fc607a5a98cd82c2c90dde884 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 27 Jul 2022 02:58:00 -0400 Subject: [PATCH] Cleaned up redundant code --- server/location.go | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/server/location.go b/server/location.go index 47985fa..ee7efb7 100644 --- a/server/location.go +++ b/server/location.go @@ -20,36 +20,15 @@ type Coordinates struct { func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.SendLocation, error) { log.Println("'Location' function called...") - url := "http://api.openweathermap.org/geo/1.0/direct?q=" - city := in.City - token := "&appid=" + s.ApiKey - - url = url + city + token - - res, err := http.Get(url) - if err != nil { - log.Printf("Error fetching location: %v\n", err) - } - defer res.Body.Close() - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - log.Printf("Error reading location: %v\n", err) - } - - coords := []Coordinates{} - err = json.Unmarshal(body, &coords) - if err != nil { - log.Printf("Error decoding JSON: %v\n", err) - } + lat, lon := getLocation(in.City, s.ApiKey) return &pb.SendLocation{ - Latitude: coords[0].Latitude, - Longitude: coords[0].Longitude, + Latitude: lat, + Longitude: lon, }, nil } -// Used internally to fetch precise locations for Current and Extended +// Used internally to fetch precise locations // Receives gRPC requests (interface) and the location (string) // Returns the latitude (float32) and longitude (float32) for a given location func getLocation(city string, key string) (float32, float32) {