39 lines
950 B
Go
39 lines
950 B
Go
package client
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"joethei.xyz/weather/config"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
var baseUrl = "https://api.openweathermap.org/data/2.5/onecall"
|
|
|
|
func GetByCoordinates(longitude, latitude float64, language string, unit config.Units) WeatherResponse {
|
|
return get(baseUrl+"?lat="+strconv.FormatFloat(latitude, 'f', 6, 32)+"&lon="+strconv.FormatFloat(longitude, 'f', 6, 32), language, unit)
|
|
}
|
|
|
|
func get(url string, language string, unit config.Units) WeatherResponse {
|
|
var configuration = config.LoadConfiguration()
|
|
|
|
response, err := http.Get(url + "&appid=" + configuration.ApiKey + "&lang=" + language + "&units=" + unit.String())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
responseData, err := ioutil.ReadAll(response.Body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
var responseObject WeatherResponse
|
|
err = json.Unmarshal(responseData, &responseObject)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return responseObject
|
|
}
|