Reorganize scryfall package

This commit is contained in:
The Magician 2024-05-27 18:32:54 +01:00
parent 5ddf405e4c
commit 2f005d7775
8 changed files with 105 additions and 109 deletions

View File

@ -0,0 +1,54 @@
package scryfall
import (
"encoding/json"
"errors"
"io"
"net/http"
)
type BulkData struct {
Id string `json:"id"`
Uri string `json:"uri"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
DownloadUri string `json:"download_uri"`
UpdatedAt string `json:"updated_at"`
Size int `json:"size"`
ContentType string `json:"content_type"`
ContentEncoding string `json:"content_encoding"`
}
const BULK_DATA_URI = "https://api.scryfall.com/bulk-data"
const BulkDataTypeOracleCards string = "oracle_cards"
const BulkDataTypeUniqueArtwork string = "unique_artwork"
const BulkDataTypeDefaultCards string = "default_cards"
const BulkDataTypeAllCards string = "all_cards"
const BulkDataTypeRulings string = "rulings"
func GetBulkDataByType(bulkDataType string) (BulkData, error) {
response, err := http.Get(BULK_DATA_URI + "/" + bulkDataType)
if err != nil {
return BulkData{}, err
}
if response.StatusCode != http.StatusOK {
return BulkData{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
}
defer response.Body.Close()
bulkDataBytes, err := io.ReadAll(response.Body)
if err != nil {
return BulkData{}, err
}
var bulkData BulkData
err = json.Unmarshal(bulkDataBytes, &bulkData)
if err != nil {
return BulkData{}, err
}
return bulkData, nil
}

View File

@ -1,4 +1,4 @@
package types
package scryfall
type Card struct {
Id string `json:"id"` // GUID

View File

@ -0,0 +1,49 @@
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"
func GetSets() ([]Set, error) {
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
}

View File

@ -1,3 +1,3 @@
package types
package scryfall
const ScryfallTimestampFormat = "2006-01-02T15:04:05.999-07:00"

View File

@ -1,36 +0,0 @@
package methods
import (
"encoding/json"
"errors"
"io"
"net/http"
"sevenkeys/scryfall/types"
)
var BULK_DATA_URI = "https://api.scryfall.com/bulk-data"
func GetBulkDataByType(bulkDataType string) (types.BulkData, error) {
response, err := http.Get(BULK_DATA_URI + "/" + bulkDataType)
if err != nil {
return types.BulkData{}, err
}
if response.StatusCode != http.StatusOK {
return types.BulkData{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
}
defer response.Body.Close()
bulkDataBytes, err := io.ReadAll(response.Body)
if err != nil {
return types.BulkData{}, err
}
var bulkData types.BulkData
err = json.Unmarshal(bulkDataBytes, &bulkData)
if err != nil {
return types.BulkData{}, err
}
return bulkData, nil
}

View File

@ -1,36 +0,0 @@
package methods
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
}

View File

@ -1,20 +0,0 @@
package types
const BulkDataTypeOracleCards string = "oracle_cards"
const BulkDataTypeUniqueArtwork string = "unique_artwork"
const BulkDataTypeDefaultCards string = "default_cards"
const BulkDataTypeAllCards string = "all_cards"
const BulkDataTypeRulings string = "rulings"
type BulkData struct {
Id string `json:"id"`
Uri string `json:"uri"`
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description"`
DownloadUri string `json:"download_uri"`
UpdatedAt string `json:"updated_at"`
Size int `json:"size"`
ContentType string `json:"content_type"`
ContentEncoding string `json:"content_encoding"`
}

View File

@ -1,15 +0,0 @@
package types
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"`
}