TheMathemagicians/sevenkeys/logic/scryfall/sets.go

50 lines
972 B
Go
Raw Normal View History

2024-05-27 17:32:54 +00:00
package scryfall
import (
"encoding/json"
"errors"
"io"
"net/http"
)
type SetList struct {
Object string `json:"object"`
HasMore bool `json:"has_more"`
Data []Set `json:"data"`
}
type Set struct {
Code string `json:"code"`
Name string `json:"name"`
CardCount int `json:"card_count"`
IconSvgUri string `json:"icon_svg_uri"`
Digital bool `json:"digital"`
}
const SETS_API_URL string = "https://api.scryfall.com/sets"
2024-05-29 19:55:38 +00:00
func GetAllSets() ([]Set, error) {
2024-05-27 17:32:54 +00:00
response, err := http.Get(SETS_API_URL)
if err != nil {
return []Set{}, nil
}
if response.StatusCode != http.StatusOK {
return []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 []Set{}, err
}
var setList SetList
err = json.Unmarshal(setsBytes, &setList)
if err != nil {
return []Set{}, err
}
return setList.Data, nil
}