From 665b7a6793d1a7554c5ba57c9728ddb8005398a8 Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Mon, 3 Jan 2022 11:08:48 +0100 Subject: [PATCH] refactor to scryfall.go --- src/serra/{card.go => scryfall.go} | 51 ++++++++++++++++++++++++ src/serra/sets.go | 62 ------------------------------ 2 files changed, 51 insertions(+), 62 deletions(-) rename src/serra/{card.go => scryfall.go} (79%) delete mode 100644 src/serra/sets.go diff --git a/src/serra/card.go b/src/serra/scryfall.go similarity index 79% rename from src/serra/card.go rename to src/serra/scryfall.go index 861454e..75aa873 100644 --- a/src/serra/card.go +++ b/src/serra/scryfall.go @@ -124,6 +124,29 @@ type PriceEntry struct { Value float64 `bson:"value"` } +type Set struct { + ArenaCode string `json:"arena_code"` + Block string `json:"block"` + BlockCode string `json:"block_code"` + CardCount int64 `json:"card_count"` + Code string `json:"code"` + Digital bool `json:"digital"` + FoilOnly bool `json:"foil_only"` + IconSvgURI string `json:"icon_svg_uri"` + ID string `json:"id"` + MtgoCode string `json:"mtgo_code"` + Name string `json:"name"` + NonfoilOnly bool `json:"nonfoil_only"` + Object string `json:"object"` + PrintedSize int64 `json:"printed_size"` + ReleasedAt string `json:"released_at"` + ScryfallURI string `json:"scryfall_uri"` + SearchURI string `json:"search_uri"` + SetType string `json:"set_type"` + TcgplayerID int64 `json:"tcgplayer_id"` + URI string `json:"uri"` +} + func fetch_card(path string) (*Card, error) { // TODO better URL Building... resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path)) @@ -160,3 +183,31 @@ func fetch_card(path string) (*Card, error) { return val, nil } + +func fetch_set(path string) (*Set, error) { + // TODO better URL Building... + resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets/%s/", path)) + if err != nil { + log.Fatalln(err) + return &Set{}, err + } + + if resp.StatusCode != 200 { + err := errors.New(fmt.Sprintf("set: %s not found", path)) + return &Set{}, err + } + + //We Read the response body on the line below. + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + return &Set{}, err + } + + r := bytes.NewReader(body) + decoder := json.NewDecoder(r) + val := &Set{} + err = decoder.Decode(val) + + return val, nil +} diff --git a/src/serra/sets.go b/src/serra/sets.go deleted file mode 100644 index d6eacfd..0000000 --- a/src/serra/sets.go +++ /dev/null @@ -1,62 +0,0 @@ -package serra - -import ( - "bytes" - "encoding/json" - "errors" - "fmt" - "io/ioutil" - "log" - "net/http" -) - -type Set struct { - ArenaCode string `json:"arena_code"` - Block string `json:"block"` - BlockCode string `json:"block_code"` - CardCount int64 `json:"card_count"` - Code string `json:"code"` - Digital bool `json:"digital"` - FoilOnly bool `json:"foil_only"` - IconSvgURI string `json:"icon_svg_uri"` - ID string `json:"id"` - MtgoCode string `json:"mtgo_code"` - Name string `json:"name"` - NonfoilOnly bool `json:"nonfoil_only"` - Object string `json:"object"` - PrintedSize int64 `json:"printed_size"` - ReleasedAt string `json:"released_at"` - ScryfallURI string `json:"scryfall_uri"` - SearchURI string `json:"search_uri"` - SetType string `json:"set_type"` - TcgplayerID int64 `json:"tcgplayer_id"` - URI string `json:"uri"` -} - -func fetch_set(path string) (*Set, error) { - // TODO better URL Building... - resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/sets/%s/", path)) - if err != nil { - log.Fatalln(err) - return &Set{}, err - } - - if resp.StatusCode != 200 { - err := errors.New(fmt.Sprintf("set: %s not found", path)) - return &Set{}, err - } - - //We Read the response body on the line below. - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatalln(err) - return &Set{}, err - } - - r := bytes.NewReader(body) - decoder := json.NewDecoder(r) - val := &Set{} - err = decoder.Decode(val) - - return val, nil -}