forked from gpmd/bigcommerce-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimages.go
59 lines (53 loc) · 1.42 KB
/
images.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
package bigcommerce
import (
"encoding/json"
"log"
"net/http"
"strconv"
)
// Image is entry for BC product images
type Image struct {
ID int64 `json:"id"`
ProductID int64 `json:"product_id"`
IsThumbnail bool `json:"is_thumbnail"`
SortOrder int64 `json:"sort_order"`
Description string `json:"description"`
ImageFile string `json:"image_file"`
URLZoom string `json:"url_zoom"`
URLStandard string `json:"url_standard"`
URLThumbnail string `json:"url_thumbnail"`
URLTiny string `json:"url_tiny"`
DateModified string `json:"date_modified"`
}
// GetMainThumbnailURL returns the main thumbnail URL for a product
// this is due to the fact that the Product API does not return the main thumbnail URL
func (bc *Client) GetMainThumbnailURL(productID int64) (string, error) {
url := "/v3/catalog/products/" + strconv.FormatInt(productID, 10) + "/images"
req := bc.getAPIRequest(http.MethodGet, url, nil)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
body, err := processBody(res)
if err != nil {
return "", err
}
var pp struct {
Data []Image `json:"data"`
Meta struct {
Pagination Pagination `json:"pagination"`
} `json:"meta"`
}
err = json.Unmarshal(body, &pp)
if err != nil {
log.Println(err)
return "", err
}
for _, p := range pp.Data {
if p.IsThumbnail {
return p.URLThumbnail, nil
}
}
return "", ErrNoMainThumbnail
}