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 20:16:07 -04:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-07-26 03:42:51 -04:00
|
|
|
)
|
|
|
|
|
2022-09-01 22:47:37 -04:00
|
|
|
// Receives a gRPC request for an extended forecast
|
|
|
|
// Returns a SendFiveDay message with the forecast as JSON
|
|
|
|
func (s *Server) FiveDay(ctx context.Context, in *pb.RequestFiveDay) (*pb.SendFiveDay, error) {
|
|
|
|
log.Printf("'FiveDay' called: %v\n", in)
|
|
|
|
|
|
|
|
url, err := s.createFiveDayUrl(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
fmt.Sprintf("Invalid location or location type: %s, %s\n",
|
|
|
|
in.Location.String(),
|
|
|
|
in.LocationType.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
token := "&appid=" + s.ApiKey
|
|
|
|
res, err := http.Get(url + token)
|
|
|
|
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.SendFiveDay{
|
|
|
|
Payload: string(body),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assembles the OpenWeather API URL for FiveDay
|
2022-09-01 02:09:45 -04:00
|
|
|
func (s *Server) createFiveDayUrl(in *pb.RequestFiveDay) (string, error) {
|
|
|
|
|
|
|
|
var lat, lon float32
|
|
|
|
var err error
|
|
|
|
url := "https://api.openweathermap.org/data/2.5/forecast?"
|
2022-09-01 20:16:07 -04:00
|
|
|
units := "&units="
|
2022-09-01 02:09:45 -04:00
|
|
|
|
|
|
|
switch in.Units {
|
|
|
|
case pb.Units_UNITS_IMPERIAL:
|
2022-09-01 20:16:07 -04:00
|
|
|
units += "imperial"
|
2022-09-01 02:09:45 -04:00
|
|
|
case pb.Units_UNITS_METRIC:
|
2022-09-01 20:16:07 -04:00
|
|
|
units += "metric"
|
2022-09-01 02:09:45 -04:00
|
|
|
default:
|
2022-09-01 20:16:07 -04:00
|
|
|
units += "standard"
|
2022-09-01 02:09:45 -04:00
|
|
|
}
|
|
|
|
|
2022-09-01 20:16:07 -04:00
|
|
|
switch in.LocationType {
|
|
|
|
case pb.LocationType_LOCATION_TYPE_CITY:
|
|
|
|
lat, lon, err = getLocation(in.Location.GetCity(), s.ApiKey)
|
|
|
|
case pb.LocationType_LOCATION_TYPE_ZIP_CODE:
|
|
|
|
lat, lon, err = getZipLocation(in.Location.GetZipCode(), s.ApiKey)
|
|
|
|
case pb.LocationType_LOCATION_TYPE_COORDS:
|
2022-09-01 02:09:45 -04:00
|
|
|
lat = in.Location.GetCoords().Latitude
|
|
|
|
lon = in.Location.GetCoords().Longitude
|
2022-09-01 20:16:07 -04:00
|
|
|
default:
|
2022-09-01 22:47:37 -04:00
|
|
|
fmt.Println(in.Location.String())
|
2022-09-01 02:09:45 -04:00
|
|
|
lat, lon, err = getLocation(in.Location.String(), s.ApiKey)
|
|
|
|
}
|
2022-07-26 03:42:51 -04:00
|
|
|
if err != nil {
|
2022-09-01 22:47:37 -04:00
|
|
|
return "", err
|
2022-07-26 03:42:51 -04:00
|
|
|
}
|
|
|
|
|
2022-09-01 22:47:37 -04:00
|
|
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units
|
|
|
|
return url, nil
|
2022-07-26 03:42:51 -04:00
|
|
|
|
|
|
|
}
|