2022-07-26 03:42:51 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
|
|
|
|
)
|
|
|
|
|
2022-07-26 19:42:25 -04:00
|
|
|
// Receives a gRPC request for an extended forecast
|
|
|
|
// Returns a SendExtended message with the forecast in JSON
|
2022-07-26 15:11:01 -04:00
|
|
|
func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) {
|
|
|
|
log.Println("'Extended' function called...")
|
2022-07-26 03:42:51 -04:00
|
|
|
|
2022-07-26 20:28:36 -04:00
|
|
|
url := "https://api.openweathermap.org/data/2.5/forecast/daily?"
|
|
|
|
lat, lon := getLocation(in, in.City)
|
2022-07-26 03:42:51 -04:00
|
|
|
days := "&cnt=" + fmt.Sprint(in.Days)
|
2022-07-26 20:28:36 -04:00
|
|
|
units := "&units=imperial"
|
2022-07-26 03:42:51 -04:00
|
|
|
token := "&appid=" + os.Getenv("API_KEY")
|
|
|
|
|
2022-07-26 20:28:36 -04:00
|
|
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + days + token
|
2022-07-26 03:42:51 -04:00
|
|
|
|
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error fetching extended weather: %v\n", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error reading extending weather: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.SendExtended{
|
2022-07-26 15:11:01 -04:00
|
|
|
Payload: string(body),
|
2022-07-26 03:42:51 -04:00
|
|
|
}, nil
|
|
|
|
}
|