From 2ea7920ca2c554bfc040f59a04e70224c999b44e Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 1 Sep 2022 20:16:07 -0400 Subject: [PATCH] Units, City, and Zip lookups ok, need to handle unspecified location --- server/extended.go | 37 +++++++++++++++++++++++++++---------- test-client/extended.go | 4 ++-- test-client/main.go | 4 ++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/server/extended.go b/server/extended.go index 668bec5..5c758f3 100644 --- a/server/extended.go +++ b/server/extended.go @@ -8,48 +8,65 @@ import ( "net/http" pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) 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?" + units := "&units=" switch in.Units { case pb.Units_UNITS_IMPERIAL: - units = "imperial" + units += "imperial" case pb.Units_UNITS_METRIC: - units = "metric" + units += "metric" default: - units = "standard" + units += "standard" } - if in.LocationType == pb.LocationType_LOCATION_TYPE_COORDS { + switch in.LocationType { + case pb.LocationType_LOCATION_TYPE_CITY: + lat, lon, err = getLocation(in.Location.GetCity(), s.ApiKey) + if err != nil { + return "", err + } + case pb.LocationType_LOCATION_TYPE_ZIP_CODE: + lat, lon, err = getZipLocation(in.Location.GetZipCode(), s.ApiKey) + if err != nil { + return "", err + } + case pb.LocationType_LOCATION_TYPE_COORDS: lat = in.Location.GetCoords().Latitude lon = in.Location.GetCoords().Longitude - } else { + default: lat, lon, err = getLocation(in.Location.String(), s.ApiKey) if err != nil { - return "", fmt.Errorf("Error: %v\n", err) + return "", err } } url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units - return url, err + return url, nil } // Receives a gRPC request for an extended forecast // Returns a SendExtended message with the forecast in JSON func (s *Server) FiveDay(ctx context.Context, in *pb.RequestFiveDay) (*pb.SendFiveDay, error) { - log.Println("'Extended' function called...") + log.Println("'FiveDay' function called...") url, err := s.createFiveDayUrl(in) if err != nil { - return nil, err + return nil, status.Errorf( + codes.InvalidArgument, + fmt.Sprintf("Invalid location: %s", in.Location.String()), + ) } + token := "&appid=" + s.ApiKey res, err := http.Get(url + token) diff --git a/test-client/extended.go b/test-client/extended.go index df39b8b..d00a808 100644 --- a/test-client/extended.go +++ b/test-client/extended.go @@ -11,10 +11,10 @@ func doFiveDay(c pb.WeatherServiceClient) { res, err := c.FiveDay(context.Background(), &pb.RequestFiveDay{ LocationType: pb.LocationType_LOCATION_TYPE_CITY, - Units: pb.Units_UNITS_IMPERIAL, + Units: pb.Units_UNITS_METRIC, Location: &pb.OneOfLocation{ LocationId: &pb.OneOfLocation_City{ - City: "Corvalls", + City: "", }, }, }) diff --git a/test-client/main.go b/test-client/main.go index 3e67615..2cb430f 100644 --- a/test-client/main.go +++ b/test-client/main.go @@ -19,6 +19,6 @@ func main() { c := pb.NewWeatherServiceClient(conn) //doCurrent(c) - //doExtended(c) - doLocation(c) + doFiveDay(c) + //doLocation(c) }