spaceapid/util/util.go
Bendodroid 4b41acfa7b
Major Refactoring
- Move components to separate packages
- Fix HTTP header for 401 response
- Add documentation
2023-11-09 22:30:03 +01:00

37 lines
748 B
Go

package util
import (
"bytes"
"encoding/json"
"log"
"os"
"slices"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
// ParseTemplate parses the given file and
func ParseTemplate(file string) (resp types.SpaceAPIResponseV14) {
// Read template file
template, err := os.ReadFile(file)
if err != nil {
log.Fatalln("Failed reading file:", err)
}
// Parse JSON
dec := json.NewDecoder(bytes.NewReader(template))
dec.DisallowUnknownFields()
err = dec.Decode(&resp)
if err != nil {
log.Fatalln("Could not parse SpaceAPI response template:", err)
}
// Check if compatible with v14
if !slices.Contains(resp.APICompatibility, "14") {
log.Fatalln("Provided template doesn't specify compatibility with API version 14")
}
return
}