diff --git a/server/current.go b/server/current.go index 432896c..2cdd855 100644 --- a/server/current.go +++ b/server/current.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "io/ioutil" "log" "net/http" @@ -15,11 +16,12 @@ import ( func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) { log.Println("'Current' function called...") - url := "https://pro.openweathermap.org/data/2.5/weather?q=" - city := in.City + url := "https://pro.openweathermap.org/data/2.5/weather?" + lat, lon := getLocation(in, in.City) + units := "&units=imperial" token := "&appid=" + os.Getenv("API_KEY") - url = url + city + "&units=imperial" + token + url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + token res, err := http.Get(url) if err != nil { diff --git a/server/extended.go b/server/extended.go index 2c82710..d0f886b 100644 --- a/server/extended.go +++ b/server/extended.go @@ -16,12 +16,13 @@ import ( func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) { log.Println("'Extended' function called...") - url := "https://api.openweathermap.org/data/2.5/forecast/daily?q=" - city := in.City + url := "https://api.openweathermap.org/data/2.5/forecast/daily?" + lat, lon := getLocation(in, in.City) days := "&cnt=" + fmt.Sprint(in.Days) + units := "&units=imperial" token := "&appid=" + os.Getenv("API_KEY") - url = url + city + "&units=imperial" + days + token + url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + days + token res, err := http.Get(url) if err != nil { diff --git a/server/location.go b/server/location.go index 6186782..7f7e5c3 100644 --- a/server/location.go +++ b/server/location.go @@ -51,15 +51,14 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send } // Used internally to fetch precise locations for Current and Extended -// Receives gRPC requests as an interface +// Receives gRPC requests (interface) and the location (string) // Returns the latitude (float32) and longitude (float32) for a given location -func getLocation(msg interface{}) (float32, float32) { +func getLocation(msg interface{}, loc string) (float32, float32) { url := "http://api.openweathermap.org/geo/1.0/direct?q=" - city := in.City token := "&appid=" + os.Getenv("API_KEY") - url = url + city + token + url = url + loc + token res, err := http.Get(url) if err != nil {