TheMathemagicians/sevenkeys/scryfall/methods/sets.go

37 lines
708 B
Go
Raw Normal View History

2024-04-24 13:29:38 +00:00
package methods
2024-05-20 16:17:45 +00:00
import (
"encoding/json"
"errors"
"io"
"net/http"
"sevenkeys/scryfall/types"
)
const SETS_API_URL string = "https://api.scryfall.com/sets"
func GetSets() ([]types.Set, error) {
response, err := http.Get(SETS_API_URL)
if err != nil {
return []types.Set{}, nil
}
if response.StatusCode != http.StatusOK {
return []types.Set{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
}
defer response.Body.Close()
setsBytes, err := io.ReadAll(response.Body)
if err != nil {
return []types.Set{}, err
}
var setList types.SetList
err = json.Unmarshal(setsBytes, &setList)
if err != nil {
return []types.Set{}, err
}
return setList.Data, nil
}