forked from r0busta/go-shopify-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
193 lines (171 loc) · 4.58 KB
/
client.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
181
182
183
184
185
186
187
188
189
190
191
192
193
package shopify
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/vinhluan/go-graphql-client"
)
const (
shopifyBaseDomain = "myshopify.com"
defaultAPIProtocol = "https"
defaultAPIBasePath = "admin/api"
defaultAPIEndpoint = "graphql.json"
defaultShopifyAPIVersion = "2023-04"
defaultHttpTimeout = time.Second * 10
)
type Client struct {
gql graphql.GraphQL
accessToken string
apiKey string
apiBasePath string
retries int
timeout time.Duration
transport http.RoundTripper
Product ProductService
Variant VariantService
Inventory InventoryService
Collection CollectionService
Order OrderService
Fulfillment FulfillmentService
Location LocationService
Metafield MetafieldService
BulkOperation BulkOperationService
Webhook WebhookService
}
func NewClient(shopName string, opts ...Option) *Client {
c := &Client{
apiBasePath: defaultAPIBasePath,
timeout: defaultHttpTimeout,
transport: http.DefaultTransport,
}
for _, opt := range opts {
opt(c)
}
if c.gql == nil {
apiEndpoint := buildAPIEndpoint(shopName, c.apiBasePath)
httpClient := &http.Client{
Timeout: c.timeout,
Transport: &transport{
accessToken: c.accessToken,
apiKey: c.apiKey,
apiBasePath: c.apiBasePath,
roundTripper: c.transport,
},
}
c.gql = graphql.NewClient(apiEndpoint, httpClient)
}
c.Product = &ProductServiceOp{client: c}
c.Variant = &VariantServiceOp{client: c}
c.Inventory = &InventoryServiceOp{client: c}
c.Collection = &CollectionServiceOp{client: c}
c.Order = &OrderServiceOp{client: c}
c.Fulfillment = &FulfillmentServiceOp{client: c}
c.Location = &LocationServiceOp{client: c}
c.Metafield = &MetafieldServiceOp{client: c}
c.BulkOperation = &BulkOperationServiceOp{client: c}
c.Webhook = &WebhookServiceOp{client: c}
return c
}
func NewDefaultClient() *Client {
apiKey := os.Getenv("STORE_API_KEY")
accessToken := os.Getenv("STORE_PASSWORD")
storeName := os.Getenv("STORE_NAME")
if apiKey == "" || accessToken == "" || storeName == "" {
log.Fatalln("Shopify Admin API Key and/or Password (aka access token) and/or store name not set")
}
return NewClient(storeName, WithPrivateAppAuth(apiKey, accessToken), WithVersion(defaultShopifyAPIVersion))
}
func NewClientWithToken(accessToken string, storeName string) *Client {
if accessToken == "" || storeName == "" {
log.Fatalln("Shopify Admin API access token and/or store name not set")
}
return NewClient(storeName, WithToken(accessToken), WithVersion(defaultShopifyAPIVersion))
}
func (c *Client) GraphQLClient() graphql.GraphQL {
return c.gql
}
func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) error {
var retries = 0
for {
r, err := c.gql.Mutate(ctx, m, variables)
if err != nil {
if r != nil {
wait := CalculateWaitTime(r.Extensions)
if wait > 0 {
retries++
time.Sleep(wait)
continue
}
}
if IsConnectionError(err) {
retries++
if retries > c.retries {
return fmt.Errorf("after %v tries: %w", retries, err)
}
time.Sleep(time.Duration(retries) * time.Second)
continue
}
return err
}
break
}
return nil
}
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
var retries = 0
for {
r, err := c.gql.Query(ctx, q, variables)
if err != nil {
if r != nil {
wait := CalculateWaitTime(r.Extensions)
if wait > 0 {
retries++
time.Sleep(wait)
continue
}
}
if uerr, isURLErr := err.(*url.Error); isURLErr && (uerr.Timeout() || uerr.Temporary()) || IsConnectionError(err) {
retries++
if retries > c.retries {
return fmt.Errorf("after %v tries: %w", retries, err)
}
time.Sleep(time.Duration(retries) * time.Second)
continue
}
return err
}
break
}
return nil
}
func (c *Client) QueryString(ctx context.Context, q string, variables map[string]interface{}, out interface{}) error {
var retries = 0
for {
r, err := c.gql.QueryString(ctx, q, variables, out)
if err != nil {
if r != nil {
wait := CalculateWaitTime(r.Extensions)
if wait > 0 {
retries++
time.Sleep(wait)
continue
}
}
if uerr, isURLErr := err.(*url.Error); isURLErr && (uerr.Timeout() || uerr.Temporary()) || IsConnectionError(err) {
retries++
if retries > c.retries {
return fmt.Errorf("after %v tries: %w", retries, err)
}
time.Sleep(time.Duration(retries) * time.Second)
continue
}
return err
}
break
}
return nil
}