From 971489f4ad5662e1ae2406b1e4a25bf8696c6122 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 26 Jul 2022 15:11:01 -0400 Subject: [PATCH] Simplified code for Current and Extended --- server/current.go | 12 ++++-------- server/extended.go | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/server/current.go b/server/current.go index 5220c2a..3bd8e0a 100644 --- a/server/current.go +++ b/server/current.go @@ -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 } diff --git a/server/extended.go b/server/extended.go index d5d08e5..5d81017 100644 --- a/server/extended.go +++ b/server/extended.go @@ -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 }