Mensa-CLI/internal/getMensa.go

40 lines
663 B
Go
Raw Permalink Normal View History

2024-09-20 21:31:19 +00:00
package internal
import (
"encoding/json"
"io"
"net/http"
2024-09-21 14:58:56 +00:00
"github.com/spf13/viper"
2024-09-20 21:31:19 +00:00
)
func GetMensen(city string) (string, error) {
2024-09-21 14:58:56 +00:00
server := viper.Get("Server").(string)
http, err := http.Get(server + "/city/" + city)
2024-09-20 21:31:19 +00:00
if err != nil {
return "", err
}
defer http.Body.Close()
body, err := io.ReadAll(http.Body)
if err != nil {
return "", err
}
data := string(body)
var mensen []string
err = json.Unmarshal([]byte(data), &mensen)
if err != nil {
return "", err
}
var result string
for _, mensa := range mensen {
result += mensa + ","
}
mensa := result[:len(result)-1]
return mensa, nil
}