mirror of
https://codeberg.org/andyscott/OpenWeather-gRPC-API.git
synced 2024-12-21 12:13:09 -05:00
Updated Current and Extended to use precise location
This commit is contained in:
parent
87a9143248
commit
af88be6ef1
3 changed files with 12 additions and 10 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -15,11 +16,12 @@ import (
|
||||||
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
|
func (s *Server) Current(ctx context.Context, in *pb.RequestCurrent) (*pb.SendCurrent, error) {
|
||||||
log.Println("'Current' function called...")
|
log.Println("'Current' function called...")
|
||||||
|
|
||||||
url := "https://pro.openweathermap.org/data/2.5/weather?q="
|
url := "https://pro.openweathermap.org/data/2.5/weather?"
|
||||||
city := in.City
|
lat, lon := getLocation(in, in.City)
|
||||||
|
units := "&units=imperial"
|
||||||
token := "&appid=" + os.Getenv("API_KEY")
|
token := "&appid=" + os.Getenv("API_KEY")
|
||||||
|
|
||||||
url = url + city + "&units=imperial" + token
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + token
|
||||||
|
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -16,12 +16,13 @@ import (
|
||||||
func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) {
|
func (s *Server) Extended(ctx context.Context, in *pb.RequestExtended) (*pb.SendExtended, error) {
|
||||||
log.Println("'Extended' function called...")
|
log.Println("'Extended' function called...")
|
||||||
|
|
||||||
url := "https://api.openweathermap.org/data/2.5/forecast/daily?q="
|
url := "https://api.openweathermap.org/data/2.5/forecast/daily?"
|
||||||
city := in.City
|
lat, lon := getLocation(in, in.City)
|
||||||
days := "&cnt=" + fmt.Sprint(in.Days)
|
days := "&cnt=" + fmt.Sprint(in.Days)
|
||||||
|
units := "&units=imperial"
|
||||||
token := "&appid=" + os.Getenv("API_KEY")
|
token := "&appid=" + os.Getenv("API_KEY")
|
||||||
|
|
||||||
url = url + city + "&units=imperial" + days + token
|
url = url + fmt.Sprintf("lat=%f", lat) + fmt.Sprintf("&lon=%f", lon) + units + days + token
|
||||||
|
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -51,15 +51,14 @@ func (s *Server) Location(ctx context.Context, in *pb.RequestLocation) (*pb.Send
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used internally to fetch precise locations for Current and Extended
|
// Used internally to fetch precise locations for Current and Extended
|
||||||
// Receives gRPC requests as an interface
|
// 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(msg interface{}) (float32, float32) {
|
func getLocation(msg interface{}, loc string) (float32, float32) {
|
||||||
|
|
||||||
url := "http://api.openweathermap.org/geo/1.0/direct?q="
|
url := "http://api.openweathermap.org/geo/1.0/direct?q="
|
||||||
city := in.City
|
|
||||||
token := "&appid=" + os.Getenv("API_KEY")
|
token := "&appid=" + os.Getenv("API_KEY")
|
||||||
|
|
||||||
url = url + city + token
|
url = url + loc + token
|
||||||
|
|
||||||
res, err := http.Get(url)
|
res, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue