mirror of
https://codeberg.org/andyscott/OpenWeather-gRPC-API.git
synced 2024-12-21 12:13:09 -05:00
Cleaned up redundant code
This commit is contained in:
parent
6a92b805e0
commit
f36a005ad1
1 changed files with 4 additions and 25 deletions
|
@ -20,36 +20,15 @@ type Coordinates struct {
|
||||||
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...")
|
||||||
|
|
||||||
url := "http://api.openweathermap.org/geo/1.0/direct?q="
|
lat, lon := getLocation(in.City, s.ApiKey)
|
||||||
city := in.City
|
|
||||||
token := "&appid=" + s.ApiKey
|
|
||||||
|
|
||||||
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 &pb.SendLocation{
|
return &pb.SendLocation{
|
||||||
Latitude: coords[0].Latitude,
|
Latitude: lat,
|
||||||
Longitude: coords[0].Longitude,
|
Longitude: lon,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used internally to fetch precise locations for Current and Extended
|
// Used internally to fetch precise locations
|
||||||
// Receives gRPC requests (interface) and the location (string)
|
// Receives gRPC requests (interface) and the location (string)
|
||||||
// Returns the latitude (float32) and longitude (float32) for a given location
|
// Returns the latitude (float32) and longitude (float32) for a given location
|
||||||
func getLocation(city string, key string) (float32, float32) {
|
func getLocation(city string, key string) (float32, float32) {
|
||||||
|
|
Loading…
Reference in a new issue