-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
73 lines (59 loc) · 1.93 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
package shopifygraphql
import (
"net/http"
"github.com/shurcooL/graphql"
)
/* ------------------------- */
/* Admin */
/* ------------------------- */
// AdminAccessTokenHeader admin access token header
var AdminAccessTokenHeader = "X-Shopify-Access-Token"
// AdminTransport admin transport
type AdminTransport struct {
AccessToken string
}
// RoundTrip http Transport RoundTrip interface
func (t *AdminTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set(AdminAccessTokenHeader, t.AccessToken)
return http.DefaultTransport.RoundTrip(req)
}
// NewAdminClient new admin graphql client
func NewAdminClient(domain, accessToken string) *graphql.Client {
// New http client with transport
httpClient := &http.Client{
Transport: &AdminTransport{
AccessToken: accessToken,
},
}
// Build graphql url
url := "https://" + domain + "/admin/api/graphql.json"
// New graphql client
return graphql.NewClient(url, httpClient)
}
/* ------------------------------ */
/* Storefront */
/* ------------------------------ */
// StorefrontAccessTokenHeader storefront access token header
var StorefrontAccessTokenHeader = "X-Shopify-Storefront-Access-Token"
// StorefrontTransport storefront transport
type StorefrontTransport struct {
AccessToken string
}
// RoundTrip http Transport RoundTrip interface
func (t *StorefrontTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set(StorefrontAccessTokenHeader, t.AccessToken)
return http.DefaultTransport.RoundTrip(req)
}
// NewStorefrontClient new storefront graphql client
func NewStorefrontClient(domain, accessToken string) *graphql.Client {
// New http client with transport
httpClient := &http.Client{
Transport: &StorefrontTransport{
AccessToken: accessToken,
},
}
// Build graphql url
url := "https://" + domain + "/api/graphql.json"
// New graphql client
return graphql.NewClient(url, httpClient)
}