2022-07-26 01:07:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-26 20:28:36 -04:00
|
|
|
"fmt"
|
2022-07-26 01:07:04 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
pb "codeberg.org/andcscott/OpenWeatherMap-gRPC-API/proto"
|
|
|
|
)
|
|
|
|
|
2022-07-26 19:42:25 -04:00
|
|
|
// Receives a gRPC request for the current forecast
|
|
|
|
// Returns a SendCurrent message containing the forecast in JSON
|
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
|
|
|
|
2022-08-06 23:28:30 -04:00
|
|
|
url := "https://api.openweathermap.org/data/2.5/weather?"
|
2022-07-27 02:42:40 -04:00
|
|
|
lat, lon := getLocation(in.City, s.ApiKey)
|
2022-07-26 20:28:36 -04:00
|
|
|
units := "&units=imperial"
|
2022-07-27 02:42:40 -04:00
|
|
|
token := "&appid=" + s.ApiKey
|
2022-07-26 01:07:04 -04:00
|
|
|
|
2022-07-26 20:28:36 -04:00
|
|
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + 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)
|
|
|
|
}
|
|
|
|
|
2022-07-27 02:42:40 -04:00
|
|
|
return &pb.SendCurrent{Payload: string(body)}, nil
|
2022-07-26 01:07:04 -04:00
|
|
|
}
|