TheMathemagicians/sevenkeys/scryfall/methods/bulkdata.go

37 lines
778 B
Go
Raw Normal View History

2024-04-24 13:29:38 +00:00
package methods
import (
"encoding/json"
"errors"
"io"
"net/http"
"sevenkeys/scryfall/types"
)
var BULK_DATA_URI = "https://api.scryfall.com/bulk-data"
2024-05-19 14:01:18 +00:00
func GetBulkDataByType(bulkDataType string) (types.BulkData, error) {
response, err := http.Get(BULK_DATA_URI + "/" + bulkDataType)
2024-04-24 13:29:38 +00:00
if err != nil {
2024-05-19 14:01:18 +00:00
return types.BulkData{}, err
2024-04-24 13:29:38 +00:00
}
if response.StatusCode != http.StatusOK {
2024-05-19 14:01:18 +00:00
return types.BulkData{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
2024-04-24 13:29:38 +00:00
}
defer response.Body.Close()
2024-05-19 14:01:18 +00:00
bulkDataBytes, err := io.ReadAll(response.Body)
2024-04-24 13:29:38 +00:00
if err != nil {
2024-05-19 14:01:18 +00:00
return types.BulkData{}, err
2024-04-24 13:29:38 +00:00
}
2024-05-19 14:01:18 +00:00
var bulkData types.BulkData
err = json.Unmarshal(bulkDataBytes, &bulkData)
2024-04-24 13:29:38 +00:00
if err != nil {
2024-05-19 14:01:18 +00:00
return types.BulkData{}, err
2024-04-24 13:29:38 +00:00
}
2024-05-19 14:01:18 +00:00
return bulkData, nil
2024-04-24 13:29:38 +00:00
}