mirror of
https://codeberg.org/andyscott/OpenWeather-gRPC-API.git
synced 2024-12-21 12:13:09 -05:00
Internal getLocation function added
This commit is contained in:
parent
99116a2693
commit
ba7e27dfea
1 changed files with 32 additions and 0 deletions
|
@ -16,6 +16,8 @@ type Coordinates struct {
|
||||||
Longitude float32 `json:"lon"`
|
Longitude float32 `json:"lon"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Receives a gRPC request for Location
|
||||||
|
// Returns a SendLocation message with the Latitude and Longitude
|
||||||
func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.SendLocation, error) {
|
func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.SendLocation, error) {
|
||||||
log.Println("'Location' function called...")
|
log.Println("'Location' function called...")
|
||||||
|
|
||||||
|
@ -47,3 +49,33 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send
|
||||||
Longitude: coords[0].Longitude,
|
Longitude: coords[0].Longitude,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Used internally to fetch precise locations for Current and Extended
|
||||||
|
// Receives gRPC requests as an interface
|
||||||
|
// Returns the latitude (float32) and longitude (float32) for a given location
|
||||||
|
func getLocation(msg interface{}) (float32, float32) {
|
||||||
|
|
||||||
|
url := "http://api.openweathermap.org/geo/1.0/direct?q="
|
||||||
|
city := in.City
|
||||||
|
token := "&appid=" + os.Getenv("API_KEY")
|
||||||
|
|
||||||
|
url = url + city + token
|
||||||
|
|
||||||
|
res, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error fetching location: %v\n", err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error reading location: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
coords := []Coordinates{}
|
||||||
|
err = json.Unmarshal(body, &coords)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error decoding JSON: %v\n", err)
|
||||||
|
}
|
||||||
|
return coords[0].Latitude, coords[0].Longitude
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue