/
customcollection.go
72 lines (60 loc) · 1.8 KB
/
customcollection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2019 The go-shopify AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package shopify
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// api reference: https://help.shopify.com/api/reference/customcollection
type CustomCollectionService service
type CustomCollection struct {
ID int64 `json:"id"`
Handle string `json:"handle"`
Title string `json:"title"`
BodyHTML string `json:"body_html"`
SortOrder string `json:"sort_order"`
PublishedScope string `json:"published_scope"`
Image CustomCollectionImage `json:"image"`
UpdatedAt time.Time `json:"updated_at"`
PublishedAt time.Time `json:"published_at"`
}
type CustomCollectionImage struct {
Src string `json:"src"`
Width int `json:"width"`
Height int `json:"height"`
CreatedAt time.Time `json:"created_at"`
}
type CustomCollectionParam struct {
ProductID int64
}
func (p *CustomCollectionParam) EncodeQuery() string {
if p == nil {
return ""
}
// for now just allow handle
// TODO: support all params
v := url.Values{}
v.Add("product_id", fmt.Sprintf("%d", p.ProductID))
return v.Encode()
}
func (c *CustomCollectionService) Get(ctx context.Context, params *CustomCollectionParam) ([]*CustomCollection, *http.Response, error) {
req, err := c.client.NewRequest("GET", "/admin/custom_collections.json", nil)
if err != nil {
return nil, nil, err
}
// encode param to query
req.URL.RawQuery = params.EncodeQuery()
var wrapper struct {
CustomCollections []*CustomCollection `json:"custom_collections"`
}
resp, err := c.client.Do(ctx, req, &wrapper)
if err != nil {
return nil, resp, err
}
return wrapper.CustomCollections, resp, nil
}