2022-07-26 01:07:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
|
|
|
|
)
|
|
|
|
|
2022-07-26 15:11:01 -04:00
|
|
|
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
|
|
|
|
log.Println("'Current' function called...")
|
2022-07-26 01:07:04 -04:00
|
|
|
|
|
|
|
url := "https://pro.openweathermap.org/data/2.5/weather?q="
|
2022-07-26 15:11:01 -04:00
|
|
|
city := in.City
|
2022-07-26 01:07:04 -04:00
|
|
|
token := "&appid=" + os.Getenv("API_KEY")
|
|
|
|
|
2022-07-26 03:52:33 -04:00
|
|
|
url = url + city + "&units=imperial" + token
|
2022-07-26 01:07:04 -04:00
|
|
|
|
|
|
|
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{
|
2022-07-26 15:11:01 -04:00
|
|
|
Payload: string(body),
|
2022-07-26 01:07:04 -04:00
|
|
|
}, nil
|
|
|
|
}
|