2022-07-26 03:42:51 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
|
|
|
|
)
|
|
|
|
|
2022-09-01 02:09:45 -04:00
|
|
|
func (s *Server) createFiveDayUrl(in *pb.RequestFiveDay) (string, error) {
|
|
|
|
|
|
|
|
var lat, lon float32
|
|
|
|
var units string
|
|
|
|
var err error
|
|
|
|
url := "https://api.openweathermap.org/data/2.5/forecast?"
|
|
|
|
|
|
|
|
switch in.Units {
|
|
|
|
case pb.Units_UNITS_IMPERIAL:
|
|
|
|
units = "imperial"
|
|
|
|
case pb.Units_UNITS_METRIC:
|
|
|
|
units = "metric"
|
|
|
|
default:
|
|
|
|
units = "standard"
|
|
|
|
}
|
|
|
|
|
|
|
|
if in.LocationType == pb.LocationType_LOCATION_TYPE_COORDS {
|
|
|
|
lat = in.Location.GetCoords().Latitude
|
|
|
|
lon = in.Location.GetCoords().Longitude
|
|
|
|
} else {
|
|
|
|
lat, lon, err = getLocation(in.Location.String(), s.ApiKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units
|
|
|
|
return url, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-09-01 02:09:45 -04:00
|
|
|
func (s *Server) FiveDay(ctx context.Context, in *pb.RequestFiveDay) (*pb.SendFiveDay, error) {
|
2022-07-26 15:11:01 -04:00
|
|
|
log.Println("'Extended' function called...")
|
2022-07-26 03:42:51 -04:00
|
|
|
|
2022-09-01 02:09:45 -04:00
|
|
|
url, err := s.createFiveDayUrl(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-27 02:42:40 -04:00
|
|
|
token := "&appid=" + s.ApiKey
|
2022-07-26 03:42:51 -04:00
|
|
|
|
2022-09-01 02:09:45 -04:00
|
|
|
res, err := http.Get(url + token)
|
2022-07-26 03:42:51 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:09:45 -04:00
|
|
|
return &pb.SendFiveDay{
|
2022-07-26 15:11:01 -04:00
|
|
|
Payload: string(body),
|
2022-07-26 03:42:51 -04:00
|
|
|
}, nil
|
|
|
|
}
|