mirror of
https://codeberg.org/andyscott/OpenWeather-gRPC-API.git
synced 2024-12-21 12:13:09 -05:00
Refactor Current forecast and Five day
This commit is contained in:
parent
45d4f3d80c
commit
715d5e3533
6 changed files with 191 additions and 131 deletions
|
@ -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
|
|
||||||
}
|
|
|
@ -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
57
server/five_day.go
Normal 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
120
server/forecast.go
Normal 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
|
||||||
|
}
|
|
@ -10,11 +10,14 @@ import (
|
||||||
func doFiveDay(c pb.WeatherServiceClient) {
|
func doFiveDay(c pb.WeatherServiceClient) {
|
||||||
|
|
||||||
res, err := c.FiveDay(context.Background(), &pb.RequestFiveDay{
|
res, err := c.FiveDay(context.Background(), &pb.RequestFiveDay{
|
||||||
LocationType: pb.LocationType_LOCATION_TYPE_UNSPECIFIED,
|
LocationType: pb.LocationType_LOCATION_TYPE_COORDS,
|
||||||
Units: pb.Units_UNITS_METRIC,
|
Units: pb.Units_UNITS_IMPERIAL,
|
||||||
Location: &pb.OneOfLocation{
|
Location: &pb.OneOfLocation{
|
||||||
LocationId: &pb.OneOfLocation_ZipCode{
|
LocationId: &pb.OneOfLocation_Coords{
|
||||||
ZipCode: "97330",
|
Coords: &pb.Coordinates{
|
||||||
|
Latitude: 41,
|
||||||
|
Longitude: -123,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
|
@ -10,14 +10,14 @@ import (
|
||||||
func doCurrent(c pb.WeatherServiceClient) {
|
func doCurrent(c pb.WeatherServiceClient) {
|
||||||
|
|
||||||
res, err := c.Current(context.Background(), &pb.RequestCurrent{
|
res, err := c.Current(context.Background(), &pb.RequestCurrent{
|
||||||
LocationType: pb.LocationType_LOCATION_TYPE_CITY,
|
LocationType: pb.LocationType_LOCATION_TYPE_COORDS,
|
||||||
Units: pb.Units_UNITS_METRIC,
|
Units: pb.Units_UNITS_UNSPECIFIED,
|
||||||
Location: &pb.OneOfLocation{
|
Location: &pb.OneOfLocation{
|
||||||
LocationId: &pb.OneOfLocation_City{
|
LocationId: &pb.OneOfLocation_Coords{
|
||||||
City: "Corvallis",
|
Coords: &pb.Coordinates{
|
||||||
},
|
Latitude: 41,
|
||||||
},
|
Longitude: -123,
|
||||||
})
|
}}}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
Loading…
Reference in a new issue