Updated Current and Extended to use precise location

This commit is contained in:
Andrew Scott 2022-07-26 20:28:36 -04:00
parent 87a9143248
commit af88be6ef1
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
3 changed files with 12 additions and 10 deletions

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {