Simplified code for Current and Extended

This commit is contained in:
Andrew Scott 2022-07-26 15:11:01 -04:00
parent 4296dbc43c
commit 971489f4ad
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
2 changed files with 8 additions and 16 deletions

View file

@ -10,10 +10,11 @@ import (
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
)
func getCurrent(in *pb.RequestCurrent) string {
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
log.Println("'Current' function called...")
city := in.City
url := "https://pro.openweathermap.org/data/2.5/weather?q="
city := in.City
token := "&appid=" + os.Getenv("API_KEY")
url = url + city + "&units=imperial" + token
@ -28,13 +29,8 @@ func getCurrent(in *pb.RequestCurrent) string {
if err != nil {
log.Printf("Error reading weather response: %v", err)
}
return string(body)
}
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
log.Println("'Current' function called...")
return &pb.SendCurrent{
Payload: getCurrent(in),
Payload: string(body),
}, nil
}

View file

@ -11,11 +11,12 @@ import (
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
)
func getExtended(in *pb.RequestExtended) string {
func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) {
log.Println("'Extended' function called...")
url := "https://api.openweathermap.org/data/2.5/forecast/daily?q="
city := in.City
days := "&cnt=" + fmt.Sprint(in.Days)
url := "https://api.openweathermap.org/data/2.5/forecast/daily?q="
token := "&appid=" + os.Getenv("API_KEY")
url = url + city + "&units=imperial" + days + token
@ -30,13 +31,8 @@ func getExtended(in *pb.RequestExtended) string {
if err != nil {
log.Printf("Error reading extending weather: %v\n", err)
}
return string(body)
}
func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) {
log.Println("'Extended' function called...")
return &pb.SendExtended{
Payload: getExtended(in),
Payload: string(body),
}, nil
}