2022-07-26 01:07:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-26 17:28:33 -04:00
|
|
|
"context"
|
2022-07-26 01:07:04 -04:00
|
|
|
"encoding/json"
|
2022-09-01 02:09:45 -04:00
|
|
|
"fmt"
|
2022-07-26 01:07:04 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-09-02 22:52:53 -04:00
|
|
|
"strconv"
|
2022-07-26 01:07:04 -04:00
|
|
|
|
2024-06-19 12:51:47 -04:00
|
|
|
pb "codeberg.org/andyscott/OpenWeather-gRPC-API/proto"
|
2022-09-01 20:15:21 -04:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-07-26 01:07:04 -04:00
|
|
|
)
|
|
|
|
|
2022-07-26 17:28:33 -04:00
|
|
|
type Coordinates struct {
|
|
|
|
Latitude float32 `json:"lat"`
|
|
|
|
Longitude float32 `json:"lon"`
|
2022-07-26 01:07:04 -04:00
|
|
|
}
|
|
|
|
|
2022-09-02 22:52:53 -04:00
|
|
|
// Receives a RequestLocation message
|
2022-07-26 19:41:53 -04:00
|
|
|
// Returns a SendLocation message with the Latitude and Longitude
|
2022-07-26 17:28:33 -04:00
|
|
|
func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.SendLocation, error) {
|
2022-09-01 22:48:38 -04:00
|
|
|
log.Printf("'Location' called, location: %v\n", in.Location)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var lat, lon float32
|
|
|
|
|
|
|
|
switch in.LocationType {
|
|
|
|
case pb.LocationType_LOCATION_TYPE_CITY:
|
2022-09-02 22:52:53 -04:00
|
|
|
lat, lon, err = fetchCityCoords(in.Location.GetCity(), s.ApiKey)
|
2022-09-01 22:48:38 -04:00
|
|
|
case pb.LocationType_LOCATION_TYPE_ZIP_CODE:
|
2022-09-02 22:52:53 -04:00
|
|
|
lat, lon, err = fetchZipCoords(in.Location.GetZipCode(), s.ApiKey)
|
2022-09-01 22:48:38 -04:00
|
|
|
default:
|
2022-09-02 22:52:53 -04:00
|
|
|
_, err = strconv.Atoi(in.Location.GetZipCode())
|
|
|
|
if err != nil {
|
|
|
|
lat, lon, err = fetchCityCoords(in.Location.GetCity(), s.ApiKey)
|
|
|
|
} else {
|
|
|
|
lat, lon, err = fetchZipCoords(in.Location.GetZipCode(), s.ApiKey)
|
|
|
|
}
|
2022-09-01 22:48:38 -04:00
|
|
|
}
|
2022-09-01 02:09:45 -04:00
|
|
|
if err != nil {
|
2022-09-01 22:48:38 -04:00
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
fmt.Sprintf("Invalid location or location type: %s, %s\n",
|
|
|
|
in.Location.String(),
|
|
|
|
in.LocationType.String()),
|
|
|
|
)
|
2022-09-01 02:09:45 -04:00
|
|
|
}
|
2022-07-26 17:28:33 -04:00
|
|
|
return &pb.SendLocation{
|
2022-07-27 02:58:00 -04:00
|
|
|
Latitude: lat,
|
|
|
|
Longitude: lon,
|
2022-07-26 17:28:33 -04:00
|
|
|
}, nil
|
2022-07-26 01:07:04 -04:00
|
|
|
}
|
2022-07-26 19:41:53 -04:00
|
|
|
|
2022-07-27 02:58:00 -04:00
|
|
|
// Used internally to fetch precise locations
|
2022-09-02 22:52:53 -04:00
|
|
|
// Receives a city and the OpenWeather API key
|
|
|
|
// Returns the latitude and longitude for the given location, otherwise an error
|
|
|
|
func fetchCityCoords(city string, key string) (float32, float32, error) {
|
|
|
|
log.Printf("'fetchCityCoords' called, location: %v\n", city)
|
2022-07-26 19:41:53 -04:00
|
|
|
|
|
|
|
url := "http://api.openweathermap.org/geo/1.0/direct?q="
|
2022-07-27 02:42:40 -04:00
|
|
|
token := "&appid=" + key
|
2022-09-02 22:52:53 -04:00
|
|
|
url += city + token
|
2022-07-26 19:41:53 -04:00
|
|
|
|
|
|
|
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 {
|
2022-09-01 02:09:45 -04:00
|
|
|
log.Printf("Error decoding geolocation JSON: %v\n", err)
|
2022-07-26 19:41:53 -04:00
|
|
|
}
|
2022-09-01 02:09:45 -04:00
|
|
|
|
2022-09-01 20:15:21 -04:00
|
|
|
if len(coords) < 1 {
|
|
|
|
return 0, 0, status.Error(codes.NotFound, "Location not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return coords[0].Latitude, coords[0].Longitude, nil
|
|
|
|
}
|
|
|
|
|
2022-09-02 22:52:53 -04:00
|
|
|
// Used internally to fetch precise locations
|
|
|
|
// Receives a zip code and the OpenWeather API key
|
|
|
|
// Returns the latitude and longitude for the given location, otherwise an error
|
|
|
|
func fetchZipCoords(zip string, key string) (float32, float32, error) {
|
|
|
|
log.Printf("'fetchZipCoords' called, zip code: %v\n", zip)
|
2022-09-01 20:15:21 -04:00
|
|
|
|
|
|
|
url := "https://api.openweathermap.org/geo/1.0/zip?zip="
|
|
|
|
token := "&appid=" + key
|
2022-09-02 22:52:53 -04:00
|
|
|
url += zip + token
|
2022-09-01 20:15:21 -04:00
|
|
|
|
|
|
|
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 geolocation JSON: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if coords.Latitude == 0 && coords.Longitude == 0 {
|
|
|
|
return 0, 0, status.Error(codes.NotFound, "Location not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return coords.Latitude, coords.Longitude, nil
|
2022-07-26 19:41:53 -04:00
|
|
|
}
|