ArinDash/apis/nina/main.go

90 lines
1.9 KiB
Go

package nina
import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
)
const apiBaseURL = "https://warnung.bund.de/api31"
type configFile struct {
Nina ninaConfig
}
type ninaConfig struct {
GebietsCode string
}
type Warning struct {
Id string `json:"id"`
Identifier string `json:"identifier"`
Sender string `json:"sender"`
Sent string `json:"sent"`
Status string `json:"status"`
MsgType string `json:"msgType"`
Scope string `json:"scope"`
Code []string `json:"code"`
Reference string `json:"reference"`
Info []Info `json:"info"`
}
type Info struct {
Language string `json:"language"`
Category []string `json:"category"`
Event string `json:"event"`
Urgency string `json:"urgency"`
Severity string `json:"severity"`
Expires string `json:"expires"`
Headline string `json:"headline"`
Description string `json:"description"`
}
func FetchWarnings() []Warning {
cfg := &configFile{}
config.LoadConfig(cfg)
client := &http.Client{}
req, err := http.NewRequest("GET", apiBaseURL+"/dashboard/"+cfg.Nina.GebietsCode+".json", 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)
}
warnings := make([]Warning, 0)
err = json.Unmarshal(respBody, &warnings)
if err != nil {
log.Fatal(err)
}
result := make([]Warning, 0)
for _, warning := range warnings {
req, err := http.NewRequest("GET", apiBaseURL+"/warnings/"+warning.Id+".json", 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)
}
warning := Warning{}
err = json.Unmarshal(respBody, &warning)
result = append(result, warning)
}
return result
}