|
| 1 | +package goshopify |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +const assetsBasePath = "admin/themes" |
| 9 | + |
| 10 | +// AssetService is an interface for interfacing with the asset endpoints |
| 11 | +// of the Shopify API. |
| 12 | +// See: https://help.shopify.com/api/reference/asset |
| 13 | +type AssetService interface { |
| 14 | + List(int, interface{}) ([]Asset, error) |
| 15 | + Get(int, string) (*Asset, error) |
| 16 | + Update(int, Asset) (*Asset, error) |
| 17 | + Delete(int, string) error |
| 18 | +} |
| 19 | + |
| 20 | +// AssetServiceOp handles communication with the asset related methods of |
| 21 | +// the Shopify API. |
| 22 | +type AssetServiceOp struct { |
| 23 | + client *Client |
| 24 | +} |
| 25 | + |
| 26 | +// Asset represents a Shopify asset |
| 27 | +type Asset struct { |
| 28 | + Attachment string `json:"attachment"` |
| 29 | + ContentType string `json:"content_type"` |
| 30 | + Key string `json:"key"` |
| 31 | + PublicURL string `json:"public_url"` |
| 32 | + Size int `json:"size"` |
| 33 | + SourceKey string `json:"source_key"` |
| 34 | + Src string `json:"src"` |
| 35 | + ThemeID int `json:"theme_id"` |
| 36 | + Value string `json:"value"` |
| 37 | + CreatedAt *time.Time `json:"created_at"` |
| 38 | + UpdatedAt *time.Time `json:"updated_at"` |
| 39 | +} |
| 40 | + |
| 41 | +// AssetResource is the result from the themes/x/assets.json?asset[key]= endpoint |
| 42 | +type AssetResource struct { |
| 43 | + Asset *Asset `json:"asset"` |
| 44 | +} |
| 45 | + |
| 46 | +// AssetsResource is the result from the themes/x/assets.json endpoint |
| 47 | +type AssetsResource struct { |
| 48 | + Assets []Asset `json:"assets"` |
| 49 | +} |
| 50 | + |
| 51 | +type assetGetOptions struct { |
| 52 | + Key string `url:"asset[key]"` |
| 53 | + ThemeID int `url:"theme_id"` |
| 54 | +} |
| 55 | + |
| 56 | +// List the metadata for all assets in the given theme |
| 57 | +func (s *AssetServiceOp) List(themeID int, options interface{}) ([]Asset, error) { |
| 58 | + path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID) |
| 59 | + resource := new(AssetsResource) |
| 60 | + err := s.client.Get(path, resource, options) |
| 61 | + return resource.Assets, err |
| 62 | +} |
| 63 | + |
| 64 | +// Get an asset by key from the given theme |
| 65 | +func (s *AssetServiceOp) Get(themeID int, key string) (*Asset, error) { |
| 66 | + path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID) |
| 67 | + options := assetGetOptions{ |
| 68 | + Key: key, |
| 69 | + ThemeID: themeID, |
| 70 | + } |
| 71 | + resource := new(AssetResource) |
| 72 | + err := s.client.Get(path, resource, options) |
| 73 | + return resource.Asset, err |
| 74 | +} |
| 75 | + |
| 76 | +// Update an asset |
| 77 | +func (s *AssetServiceOp) Update(themeID int, asset Asset) (*Asset, error) { |
| 78 | + path := fmt.Sprintf("%s/%d/assets.json", assetsBasePath, themeID) |
| 79 | + wrappedData := AssetResource{Asset: &asset} |
| 80 | + resource := new(AssetResource) |
| 81 | + err := s.client.Put(path, wrappedData, resource) |
| 82 | + return resource.Asset, err |
| 83 | +} |
| 84 | + |
| 85 | +// Delete an asset |
| 86 | +func (s *AssetServiceOp) Delete(themeID int, key string) error { |
| 87 | + path := fmt.Sprintf("%s/%d/assets.json?asset[key]=%s", assetsBasePath, themeID, key) |
| 88 | + return s.client.Delete(path) |
| 89 | +} |
0 commit comments