-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopify.go
180 lines (147 loc) · 3.72 KB
/
shopify.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package shopify
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/cswank/store/internal/config"
)
/*
{
"product": {
"title": "Burton Custom Freestyle 151",
"body_html": "<strong>Good snowboard!<\/strong>",
"vendor": "Burton",
"product_type": "Snowboard",
"images": [
{
"src": "http:\/\/example.com\/rails_logo.gif"
}
]
}
}
*/
var (
ShopifyAPI string
ShopifyJSKey string
ShopifyDomain string
productsURL string
imagesURL string
variantsURL string
deleteURL string
discountURL string
cfg config.Config
)
func Init(c config.Config) {
cfg = c
if cfg.ShopifyJSKey == "" || cfg.ShopifyDomain == "" || cfg.ShopifyAPI == "" {
log.Fatal("you must set SHOPIFY_API and SHOPIFY_JS_KEY and SHOPIFY_DOMAIN")
}
productsURL = fmt.Sprintf("%s/%s", cfg.ShopifyAPI, "admin/products.json")
imagesURL = fmt.Sprintf("%s/%s", cfg.ShopifyAPI, "admin/products/%s/images.json")
variantsURL = fmt.Sprintf("%s/%s", cfg.ShopifyAPI, "admin/variants/%d.json")
deleteURL = fmt.Sprintf("%s/%s", cfg.ShopifyAPI, "admin/products/%s.json")
discountURL = fmt.Sprintf("%s/%s", cfg.ShopifyAPI, "admin/discounts.json")
}
type Img struct {
Src string `json:"src,omitempty"`
}
type Variant struct {
ID int `json:"id"`
Price string `json:"price"`
}
type Product struct {
ID int `json:"id,omitempty"`
Title string `json:"title"`
ProductType string `json:"product_type"`
PublishedAt string `json:"published_at"`
PublishedScope string `json:"published_scope"`
Images []Img `json:"images"`
Variants []Variant `json:"variants"`
}
func Create(title, category, price string) (string, error) {
m := map[string]Product{
"product": {
Title: title,
ProductType: category,
PublishedAt: time.Now().Format("2006-01-02T15:04:05-0700"),
PublishedScope: "global",
},
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(m); err != nil {
return "", err
}
resp, err := http.Post(productsURL, "application/json", &buf)
if err != nil {
return "", err
}
defer resp.Body.Close()
var r map[string]Product
err = json.NewDecoder(resp.Body).Decode(&r)
p, ok := r["product"]
if !ok {
return "", errors.New("unexpected response from shopify")
}
if len(p.Variants) != 1 {
return "", errors.New("unexpected response from shopify")
}
v := p.Variants[0]
if err := updatePrice(v.ID, price); err != nil {
return "", err
}
return fmt.Sprintf("%d", p.ID), nil
}
func Delete(id string) error {
uri := fmt.Sprintf(deleteURL, id)
req, err := http.NewRequest("DELETE", uri, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return err
}
func AddImage(id string, img []byte) error {
s := base64.StdEncoding.EncodeToString(img)
buf := bytes.NewBufferString(fmt.Sprintf(`{"image": {"attachment": "%s"}}`, s))
u := fmt.Sprintf(imagesURL, id)
resp, err := http.Post(u, "application/json", buf)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status when adding image to spotify: %d", resp.StatusCode)
}
return nil
}
func updatePrice(id int, price string) error {
m := map[string]Variant{
"variant": {
ID: id,
Price: price,
},
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(m); err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf(variantsURL, id), &buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
return resp.Body.Close()
}