Add Weather widget and integrate OpenWeatherMap API for dynamic updates
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package openWeatherMap
|
||||
|
||||
import (
|
||||
"ArinDash/config"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const apiBaseURL = "https://api.openweathermap.org/data/2.5/weather"
|
||||
|
||||
type configFile struct {
|
||||
OpenWeatherMap openWeatherMapConfig
|
||||
}
|
||||
|
||||
type openWeatherMapConfig struct {
|
||||
LocationId string
|
||||
ApiKey string
|
||||
}
|
||||
|
||||
func FetchCurrentWeather() CurrentWeather {
|
||||
cfg := &configFile{}
|
||||
config.LoadConfig(cfg)
|
||||
client := &http.Client{}
|
||||
currentWeather := &CurrentWeather{}
|
||||
|
||||
req, err := http.NewRequest("GET", apiBaseURL+"?id="+cfg.OpenWeatherMap.LocationId+"&units=metric&lang=en&APPID="+cfg.OpenWeatherMap.ApiKey, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(respBody, currentWeather)
|
||||
return *currentWeather
|
||||
}
|
||||
Reference in New Issue
Block a user