diff --git a/sevenkeys/logic/scryfall/bulkdata.go b/sevenkeys/logic/scryfall/bulkdata.go new file mode 100644 index 0000000..fb80be7 --- /dev/null +++ b/sevenkeys/logic/scryfall/bulkdata.go @@ -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 +} diff --git a/sevenkeys/scryfall/types/card.go b/sevenkeys/logic/scryfall/card.go similarity index 96% rename from sevenkeys/scryfall/types/card.go rename to sevenkeys/logic/scryfall/card.go index 17cf224..bf226cb 100644 --- a/sevenkeys/scryfall/types/card.go +++ b/sevenkeys/logic/scryfall/card.go @@ -1,4 +1,4 @@ -package types +package scryfall type Card struct { Id string `json:"id"` // GUID diff --git a/sevenkeys/logic/scryfall/sets.go b/sevenkeys/logic/scryfall/sets.go new file mode 100644 index 0000000..1702d20 --- /dev/null +++ b/sevenkeys/logic/scryfall/sets.go @@ -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 +} diff --git a/sevenkeys/scryfall/types/timestamp.go b/sevenkeys/logic/scryfall/timestamp.go similarity index 79% rename from sevenkeys/scryfall/types/timestamp.go rename to sevenkeys/logic/scryfall/timestamp.go index 0c93965..bd81025 100644 --- a/sevenkeys/scryfall/types/timestamp.go +++ b/sevenkeys/logic/scryfall/timestamp.go @@ -1,3 +1,3 @@ -package types +package scryfall const ScryfallTimestampFormat = "2006-01-02T15:04:05.999-07:00" diff --git a/sevenkeys/scryfall/methods/bulkdata.go b/sevenkeys/scryfall/methods/bulkdata.go deleted file mode 100644 index e775ac0..0000000 --- a/sevenkeys/scryfall/methods/bulkdata.go +++ /dev/null @@ -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 -} diff --git a/sevenkeys/scryfall/methods/sets.go b/sevenkeys/scryfall/methods/sets.go deleted file mode 100644 index e9466fe..0000000 --- a/sevenkeys/scryfall/methods/sets.go +++ /dev/null @@ -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 -} diff --git a/sevenkeys/scryfall/types/bulkdata.go b/sevenkeys/scryfall/types/bulkdata.go deleted file mode 100644 index f341951..0000000 --- a/sevenkeys/scryfall/types/bulkdata.go +++ /dev/null @@ -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"` -} diff --git a/sevenkeys/scryfall/types/set.go b/sevenkeys/scryfall/types/set.go deleted file mode 100644 index 81154b7..0000000 --- a/sevenkeys/scryfall/types/set.go +++ /dev/null @@ -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"` -}