Mensa-CLI/internal/getMenu.go

40 lines
748 B
Go
Raw Normal View History

2024-09-20 21:31:19 +00:00
package internal
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/LeRoid-hub/Mensa-CLI/models"
2024-09-21 14:58:56 +00:00
"github.com/spf13/viper"
2024-09-20 21:31:19 +00:00
)
func GetMenu(mensa string) (models.Mensa, error) {
2024-09-21 14:58:56 +00:00
server := viper.Get("Server").(string)
http, err := http.Get(server + "/mensa/" + mensa)
2024-09-20 21:31:19 +00:00
if err != nil {
return models.Mensa{}, err
}
defer http.Body.Close()
body, err := io.ReadAll(http.Body)
if err != nil {
return models.Mensa{}, err
}
var data models.Mensa
err = json.Unmarshal([]byte(body), &data)
if err != nil {
fmt.Println(err)
return models.Mensa{}, err
}
return data, nil
}
func GetSearchMenu(city string, mensa string) (models.Mensa, error) {
return GetMenu(city + "/" + mensa)
}