From 5bad40c8bea6f8acf6ec1bcd002f9536174333b3 Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Thu, 23 Mar 2023 14:58:57 -0300 Subject: [PATCH] feat: add collectible animation info to data fetched from Opensea --- services/wallet/thirdparty/opensea/client.go | 55 +++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/services/wallet/thirdparty/opensea/client.go b/services/wallet/thirdparty/opensea/client.go index 9f445afd6..a3a824678 100644 --- a/services/wallet/thirdparty/opensea/client.go +++ b/services/wallet/thirdparty/opensea/client.go @@ -106,20 +106,22 @@ type SellOrder struct { } type Asset struct { - ID int `json:"id"` - TokenID *bigint.BigInt `json:"token_id"` - Name string `json:"name"` - Description string `json:"description"` - Permalink string `json:"permalink"` - ImageThumbnailURL string `json:"image_thumbnail_url"` - ImageURL string `json:"image_url"` - Contract Contract `json:"asset_contract"` - Collection Collection `json:"collection"` - Traits []Trait `json:"traits"` - LastSale LastSale `json:"last_sale"` - SellOrders []SellOrder `json:"sell_orders"` - BackgroundColor string `json:"background_color"` - TokenURI string `json:"token_metadata"` + ID int `json:"id"` + TokenID *bigint.BigInt `json:"token_id"` + Name string `json:"name"` + Description string `json:"description"` + Permalink string `json:"permalink"` + ImageThumbnailURL string `json:"image_thumbnail_url"` + ImageURL string `json:"image_url"` + AnimationURL string `json:"animation_url"` + AnimationMediaType string `json:"animation_media_type"` + Contract Contract `json:"asset_contract"` + Collection Collection `json:"collection"` + Traits []Trait `json:"traits"` + LastSale LastSale `json:"last_sale"` + SellOrders []SellOrder `json:"sell_orders"` + BackgroundColor string `json:"background_color"` + TokenURI string `json:"token_metadata"` } type CollectionTrait struct { @@ -215,6 +217,25 @@ func (o *HTTPClient) doGetRequest(url string, apiKey string) ([]byte, error) { return nil, fmt.Errorf("unsuccessful request: %d %s", statusCode, http.StatusText(statusCode)) } +func (o *HTTPClient) doContentTypeRequest(url string) (string, error) { + req, err := http.NewRequest(http.MethodHead, url, nil) + if err != nil { + return "", err + } + + resp, err := o.client.Do(req) + if err != nil { + return "", err + } + defer func() { + if err := resp.Body.Close(); err != nil { + log.Error("failed to close head request body", "err", err) + } + }() + + return resp.Header.Get("Content-Type"), nil +} + type Client struct { client *HTTPClient url string @@ -402,6 +423,12 @@ func (o *Client) fetchAssets(queryParams url.Values, limit int) (*AssetContainer asset.Traits[i].TraitType = strings.Replace(asset.Traits[i].TraitType, "_", " ", 1) asset.Traits[i].Value = TraitValue(strings.Title(string(asset.Traits[i].Value))) } + if len(asset.AnimationURL) > 0 { + asset.AnimationMediaType, err = o.client.doContentTypeRequest(asset.AnimationURL) + if err != nil { + asset.AnimationURL = "" + } + } assets.Assets = append(assets.Assets, asset) } assets.NextCursor = container.NextCursor