Refactor Current forecast and Five day

This commit is contained in:
Andrew Scott 2022-09-02 22:50:27 -04:00
parent 45d4f3d80c
commit 715d5e3533
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
6 changed files with 191 additions and 131 deletions

View file

@ -1,37 +0,0 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
pb "codeberg.org/andcscott/OpenWeather-gRPC-API/proto"
)
// Receives a gRPC request for the current forecast
// Returns a SendCurrent message containing the forecast in JSON
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
log.Printf("'Current' called: %v\n", in)
url := "https://api.openweathermap.org/data/2.5/weather?"
lat, lon, err := getLocation(in.Location.String(), s.ApiKey)
units := "&units=imperial"
token := "&appid=" + s.ApiKey
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + token
res, err := http.Get(url)
if err != nil {
log.Printf("Error fetching weather: %v\n", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Error reading weather response: %v", err)
}
return &pb.SendCurrent{Payload: string(body)}, nil
}

View file

@ -1,83 +0,0 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
pb "codeberg.org/andcscott/OpenWeather-gRPC-API/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// 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
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?"
units := "&units="
switch in.Units {
case pb.Units_UNITS_IMPERIAL:
units += "imperial"
case pb.Units_UNITS_METRIC:
units += "metric"
default:
units += "standard"
}
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:
lat = in.Location.GetCoords().Latitude
lon = in.Location.GetCoords().Longitude
default:
fmt.Println(in.Location.String())
lat, lon, err = getLocation(in.Location.String(), s.ApiKey)
}
if err != nil {
return "", err
}
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units
return url, nil
}

57
server/five_day.go Normal file
View file

@ -0,0 +1,57 @@
package main
import (
"context"
"fmt"
"log"
pb "codeberg.org/andcscott/OpenWeather-gRPC-API/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// 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)
token := "&appid=" + s.ApiKey
url, err := s.createFiveDayUrl(in)
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
fmt.Sprintf(
"Invalid location or location type: %v, %v\n",
in.Location,
in.LocationType,
))
}
fcst, err := fetchForecast(url + token)
if err != nil {
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf(
"A server error occurred while fetching the forecast: %v\n",
err,
))
}
return &pb.SendFiveDay{
Payload: fcst,
}, nil
}
// Assembles the URL for five day requests to Open Weather
func (s *Server) createFiveDayUrl(in *pb.RequestFiveDay) (string, error) {
url := "https://api.openweathermap.org/data/2.5/forecast?"
units := updateUnits(in.Units)
lat, lon, err := s.fetchLocation(in.LocationType, in.Location)
if err != nil {
return "", err
}
url += fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units
return url, nil
}

120
server/forecast.go Normal file
View file

@ -0,0 +1,120 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
pb "codeberg.org/andcscott/OpenWeather-gRPC-API/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Receives a gRPC request for the current forecast
// Returns a SendCurrent message containing the forecast in JSON
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
log.Printf("'Current' called: %v\n", in)
token := "&appid=" + s.ApiKey
url, err := s.createCurrentUrl(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()),
)
}
fcst, err := fetchForecast(url + token)
if err != nil {
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf(
"A server error occurred while fetching the forecast: %v\n",
err,
))
}
return &pb.SendCurrent{
Payload: fcst,
}, nil
}
// Assembles the URL for current weather requests to OpenWeather
func (s *Server) createCurrentUrl(in *pb.RequestCurrent) (string, error) {
url := "https://api.openweathermap.org/data/2.5/weather?"
units := updateUnits(in.Units)
lat, lon, err := s.fetchLocation(in.LocationType, in.Location)
if err != nil {
return "", err
}
url += fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units
return url, nil
}
// Creates the units portion of the request to OpenWeater
func updateUnits(units pb.Units) string {
unitStr := "&units="
switch units {
case pb.Units_UNITS_IMPERIAL:
unitStr += "imperial"
case pb.Units_UNITS_METRIC:
unitStr += "metric"
default:
unitStr += "standard"
}
return unitStr
}
// Obtains a locations exact coordinates for the request to OpenWeather
func (s *Server) fetchLocation(locType pb.LocationType, loc *pb.OneOfLocation) (float32, float32, error) {
var lat, lon float32
var err error
switch locType {
case pb.LocationType_LOCATION_TYPE_CITY:
lat, lon, err = fetchCityCoords(loc.GetCity(), s.ApiKey)
case pb.LocationType_LOCATION_TYPE_ZIP_CODE:
lat, lon, err = fetchZipCoords(loc.GetZipCode(), s.ApiKey)
case pb.LocationType_LOCATION_TYPE_COORDS:
lat = loc.GetCoords().Latitude
lon = loc.GetCoords().Longitude
default:
_, err = strconv.Atoi(loc.GetZipCode())
if err != nil {
lat, lon, err = fetchCityCoords(loc.GetCity(), s.ApiKey)
} else {
lat, lon, err = fetchZipCoords(loc.GetZipCode(), s.ApiKey)
}
}
if err != nil {
return 0, 0, err
}
return lat, lon, nil
}
// Obtains forecasts from OpenWeather
// Receives the URL as a string
// Returns the JSON response from OpenWeather
func fetchForecast(url string) (string, error) {
res, err := http.Get(url)
if err != nil {
log.Printf("Error fetching extended weather: %v\n", err)
return "", err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Error reading extending weather: %v\n", err)
return "", err
}
return string(body), nil
}

View file

@ -10,11 +10,14 @@ import (
func doFiveDay(c pb.WeatherServiceClient) {
res, err := c.FiveDay(context.Background(), &pb.RequestFiveDay{
LocationType: pb.LocationType_LOCATION_TYPE_UNSPECIFIED,
Units: pb.Units_UNITS_METRIC,
LocationType: pb.LocationType_LOCATION_TYPE_COORDS,
Units: pb.Units_UNITS_IMPERIAL,
Location: &pb.OneOfLocation{
LocationId: &pb.OneOfLocation_ZipCode{
ZipCode: "97330",
LocationId: &pb.OneOfLocation_Coords{
Coords: &pb.Coordinates{
Latitude: 41,
Longitude: -123,
},
},
},
})

View file

@ -10,14 +10,14 @@ import (
func doCurrent(c pb.WeatherServiceClient) {
res, err := c.Current(context.Background(), &pb.RequestCurrent{
LocationType: pb.LocationType_LOCATION_TYPE_CITY,
Units: pb.Units_UNITS_METRIC,
LocationType: pb.LocationType_LOCATION_TYPE_COORDS,
Units: pb.Units_UNITS_UNSPECIFIED,
Location: &pb.OneOfLocation{
LocationId: &pb.OneOfLocation_City{
City: "Corvallis",
},
},
})
LocationId: &pb.OneOfLocation_Coords{
Coords: &pb.Coordinates{
Latitude: 41,
Longitude: -123,
}}}})
if err != nil {
log.Fatalln(err)
}