-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
176 lines (140 loc) · 4.38 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
// Package client provides the base for all requests and responses to the
// Paddle API.
package client
import (
"bytes"
"context"
"embed"
"encoding/json"
"io"
"net/http"
"net/url"
"reflect"
"strings"
"github.com/PaddleHQ/paddle-go-sdk/internal/response"
"github.com/ggicci/httpin"
"github.com/ggicci/httpin/core"
)
// Client provides the base for all requests and responses to the Paddle API.
type Client struct {
client HTTPDoer
apiKey string
baseURL *url.URL
version string
}
// HTTPDoer is an interface that expects the Do method for making HTTP requests.
type HTTPDoer interface {
Do(req *http.Request) (*http.Response, error)
}
//go:embed version.txt
var versionFs embed.FS
func init() {
core.RegisterDirective("paddle_include", &DirectiveInclude{})
core.RegisterDirective("query", &DirectiveQuery{}, true)
}
// New returns a new Client with the provided http.Client, apiKey, and baseURL.
func New(httpClient HTTPDoer, apiKey string, baseURL *url.URL) (*Client, error) {
version, err := versionFs.ReadFile("version.txt")
if err != nil {
return nil, err
}
return &Client{
client: httpClient,
apiKey: apiKey,
baseURL: baseURL,
version: strings.TrimSuffix(string(version), "\n"),
}, nil
}
// Wanter is an interface that can be implemented by a response struct.
// It is used to inject the client into to the response struct after a request
// has been successfully made.
type Wanter interface {
Wants(Doer)
}
// Doer is an interface that expects the Do method for making API requests.
type Doer interface {
Do(ctx context.Context, method, path string, src, dst any) (err error)
}
// Do sends an API request and returns the API response. The src field is a
// request which contains `json` or `in` struct tags (see github.com/ggicci/httpin)
// and will be encoded as the request body. The dst field is a response which will
// be decoded from the response body. The dst field should be given as a pointer.
// If the src field is nil, no request body will be sent. If the dst field is nil,
// no response body will be decoded.
func (c *Client) Do(ctx context.Context, method, path string, src, dst any) (err error) { //nolint:cyclop // flat function
if ctx == nil {
ctx = context.Background()
}
u, err := url.Parse(path)
if err != nil {
return err
}
u.Host = c.baseURL.Host
u.Scheme = c.baseURL.Scheme
req, err := encodeRequest(ctx, method, u, src)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("User-Agent", "PaddleSDK/go "+c.version)
if transitID := TransitIDFromContext(ctx); transitID != "" {
req.Header.Set("X-Transaction-Id", transitID) // Deprecated.
req.Header.Set("X-Transit-Id", transitID)
}
if err := attachRequestBody(req, method, src); err != nil {
return err
}
res, requestErr := c.client.Do(req)
defer func() {
if requestErr != nil {
return
}
if dErr := res.Body.Close(); dErr != nil {
err = dErr
}
}()
if err := response.HandleError(req, res, requestErr); err != nil {
return err
}
// We don't return the error from response.Handle here, as we want to return
// the error from the response body closing in the defer func above.
err = response.Handle(req, res, dst)
if dst != nil {
v := reflect.ValueOf(dst).Elem().Interface()
if wanter, ok := v.(Wanter); ok {
wanter.Wants(c)
}
}
return
}
// encodeRequest encodes a request with the given method, URL, and source.
// If the source is nil, then we completely skip `httpin`'s encoder. This is
// because the `httpin` package does not support nil bodies, and empty request
// payloads will cause URL to be completely rewritten.
func encodeRequest(ctx context.Context, method string, u *url.URL, src any) (*http.Request, error) {
if src == nil {
return http.NewRequestWithContext(ctx, method, u.String(), http.NoBody)
}
return httpin.NewRequestWithContext(ctx, method, u.String(), src)
}
func attachRequestBody(req *http.Request, method string, src any) error {
if src == nil {
return nil
}
body := bytes.NewBuffer([]byte{})
if err := json.NewEncoder(body).Encode(src); err != nil {
return err
}
if method == http.MethodGet || method == http.MethodDelete {
return nil
}
if strings.TrimSpace(body.String()) == "{}" {
return nil
}
req.Body = io.NopCloser(body)
req.Header.Set("Content-Type", "application/json")
req.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(body), nil
}
return nil
}