45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"../config"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func GetByCityName(city string) WeatherResponse {
|
|
return get("https://api.openweathermap.org/data/2.5/weather?q=" + city)
|
|
}
|
|
|
|
func GetByCoordinates(longitude, latitude float64) WeatherResponse {
|
|
return get("https://api.openweathermap.org/data/2.5/weather?lat=" + strconv.FormatFloat(latitude, 'f', 6, 32) + "&lon=" + strconv.FormatFloat(longitude, 'f', 6, 32))
|
|
}
|
|
|
|
func GetByZipCode(zip int, country string) WeatherResponse {
|
|
return get("https://api.openweathermap.org/data/2.5/weather?zip=" + string(zip) + "," + country)
|
|
}
|
|
|
|
func get(url string) WeatherResponse {
|
|
var configuration = config.LoadConfiguration()
|
|
|
|
response, err := http.Get(url + "&appid=" + configuration.ApiKey + "&lang=" + configuration.Language + "&units=" + configuration.Units)
|
|
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
|
|
}
|