Added extended.go for server and client

This commit is contained in:
Andrew Scott 2022-07-26 03:42:51 -04:00
parent 27b125f6a0
commit 4ddc054c64
Signed by: a
GPG key ID: 3EB62D0BBB8DB381
2 changed files with 62 additions and 0 deletions

42
server/extended.go Normal file
View file

@ -0,0 +1,42 @@
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
)
func getExtended(in *pb.RequestExtended) string {
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
res, err := http.Get(url)
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 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),
}, nil
}

20
test-client/extended.go Normal file
View file

@ -0,0 +1,20 @@
package main
import (
"context"
"log"
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
)
func doExtended(c pb.WeatherServiceClient) {
res, err := c.Extended(context.Background(), &pb.RequestExtended{
City: "Corvallis",
Days: 7,
})
if err != nil {
log.Fatalln(err)
}
log.Println(res.Payload)
}