forked from labd/commercetools-go-sdk
/
client.go
137 lines (111 loc) · 3.56 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
package connect
// Generated file, please do not change!!!
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"runtime"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
// Version identifies the current library version. Should match the git tag
const Version = "1.3.0"
type Client struct {
httpClient *http.Client
url *url.URL
}
type ClientConfig struct {
URL string
Credentials *clientcredentials.Config
LogLevel int
HTTPClient *http.Client
UserAgent string
}
type SetUserAgentTransport struct {
T http.RoundTripper
userAgent string
}
func (sat *SetUserAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", sat.userAgent)
if sat.T != nil {
return sat.T.RoundTrip(req)
}
return http.DefaultTransport.RoundTrip(req)
}
// NewClient creates a new client based on the provided ClientConfig
func NewClient(cfg *ClientConfig) (*Client, error) {
userAgent := cfg.UserAgent
if userAgent == "" {
userAgent = GetUserAgent()
}
httpClient := cfg.HTTPClient
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Transport = &SetUserAgentTransport{
T: httpClient.Transport, userAgent: userAgent}
if cfg.Credentials != nil {
httpClient = cfg.Credentials.Client(
context.WithValue(context.TODO(), oauth2.HTTPClient, httpClient))
}
url, err := url.Parse(cfg.URL)
if err != nil {
return nil, err
}
client := &Client{
url: url,
httpClient: httpClient,
}
return client, nil
}
func (c *Client) createEndpoint(p string) (*url.URL, error) {
url, err := url.Parse(p)
if err != nil {
return nil, err
}
return c.url.ResolveReference(url), nil
}
func (c *Client) head(ctx context.Context, path string, queryParams url.Values, headers http.Header) (*http.Response, error) {
return c.execute(ctx, http.MethodHead, path, queryParams, headers, nil)
}
func (c *Client) get(ctx context.Context, path string, queryParams url.Values, headers http.Header) (*http.Response, error) {
return c.execute(ctx, http.MethodGet, path, queryParams, headers, nil)
}
func (c *Client) post(ctx context.Context, path string, queryParams url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
return c.execute(ctx, http.MethodPost, path, queryParams, headers, body)
}
func (c *Client) put(ctx context.Context, path string, queryParams url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
return c.execute(ctx, http.MethodPut, path, queryParams, headers, body)
}
func (c *Client) delete(ctx context.Context, path string, queryParams url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
return c.execute(ctx, http.MethodDelete, path, queryParams, headers, body)
}
func (c *Client) execute(ctx context.Context, method string, path string, params url.Values, headers http.Header, body io.Reader) (*http.Response, error) {
endpoint, err := c.createEndpoint(path)
if err != nil {
return nil, err
}
if params != nil {
endpoint.RawQuery = params.Encode()
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), body)
if err != nil {
return nil, fmt.Errorf("creating new request: %w", err)
}
if headers != nil {
req.Header = headers
}
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func GetUserAgent() string {
return fmt.Sprintf("commercetools-go-sdk/%s Go/%s (%s; %s)",
Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}