61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package scryfall
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const SCRYFALL_API_CARDS = "/cards/"
|
|
const CARD_IMAGEURIS_KEY_PNG = "png"
|
|
|
|
type Card struct {
|
|
Id string `json:"id"`
|
|
Name string `json:"name"`
|
|
Set string `json:"set"`
|
|
Games []string `json:"games"`
|
|
Foil bool `json:"foil"`
|
|
NonFoil bool `json:"nonfoil"`
|
|
Promo bool `json:"promo"`
|
|
CollectorNumber string `json:"collector_number"`
|
|
ImageUris map[string]string `json:"image_uris"`
|
|
Language string `json:"lang"`
|
|
}
|
|
|
|
func (c *ScryfallClient) GetCardById(id string) (Card, error) {
|
|
var card Card
|
|
|
|
response, err := c.httpClient.Get(c.baseURL + SCRYFALL_API_CARDS + id)
|
|
if err != nil {
|
|
return card, err
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return card, errors.New("HTTP request failed with code: " + fmt.Sprint(response.StatusCode))
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
cardBytes, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return card, err
|
|
}
|
|
|
|
err = json.Unmarshal(cardBytes, &card)
|
|
if err != nil {
|
|
return card, err
|
|
}
|
|
|
|
return card, nil
|
|
}
|
|
|
|
func (c *ScryfallClient) GetCardImageUrlById(id string) (string, error) {
|
|
card, err := c.GetCardById(id)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return card.ImageUris[CARD_IMAGEURIS_KEY_PNG], nil
|
|
}
|