generated from mythrnr/template-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
252 lines (204 loc) · 6.18 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package paypayopa
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/mythrnr/paypayopa-sdk-go/internal"
)
// opaClient is the client for handling requests/responses to the PayPay API.
//
// opaClient は PayPay API へのリクエスト/レスポンスのハンドリングを行うクライアント.
type opaClient struct{ http *http.Client }
func newClient(creds *Credentials) *opaClient {
return newClientWithHTTPClient(creds, &http.Client{})
}
func newClientWithHTTPClient(creds *Credentials, hc *http.Client) *opaClient {
if hc == nil {
panic("*http.Client must not be nil")
}
if hc.Timeout == 0 {
hc.Timeout = timeout
}
tr := hc.Transport
if tr == nil {
tr = http.DefaultTransport
}
hc.Transport = newAuthenticateInterceptor(creds, tr)
return &opaClient{http: hc}
}
// GET sends a GET request.
// response is stored in res, and returns
// the processing result and error object.
//
// GET は GET リクエストを送信する.
// res にレスポンスを格納し, 処理結果とエラーオブジェクトを返す.
func (c *opaClient) GET(
ctx context.Context,
path string,
res interface{},
) (*ResultInfo, error) {
req, err := c.Request(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
return c.Do(req, res)
}
// DELETE sends a DELETE request, and returns
// the processing result and error object.
//
// DELETE は DELETE リクエストを送信し, 処理結果とエラーオブジェクトを返す.
func (c *opaClient) DELETE(
ctx context.Context,
path string,
) (*ResultInfo, error) {
req, err := c.Request(ctx, http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return c.Do(req, nil)
}
// POST sends a POST request.
// response is stored in res, and returns
// the processing result and error object.
//
// POST は POST リクエストを送信する.
// res にレスポンスを格納し, 処理結果とエラーオブジェクトを返す.
func (c *opaClient) POST(
ctx context.Context,
path string,
res interface{},
req interface{},
) (*ResultInfo, error) {
rq, err := c.Request(ctx, http.MethodPost, path, req)
if err != nil {
return nil, err
}
return c.Do(rq, res)
}
// Request creates a *http.Request from arguments.
// Basically, you can simply execute the GET, DELETE, and POST methods,
// but if you want to configure the request further, call this method.
// The configured request is sent by calling the Do method.
//
// Requestは、引数から*http.Requestを作成する.
// 基本的には単純に GET, DELETE, POST のメソッドを実行すればいいが,
// リクエストを更に設定したい場合はこのメソッドを呼ぶ.
// 設定をしたリクエストは Do メソッドを呼んで送信する.
func (c *opaClient) Request(
ctx context.Context,
method, path string,
req interface{},
) (*http.Request, error) {
var reader io.Reader
if req != nil {
b, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
reader = bytes.NewReader(b)
}
rq, err := http.NewRequestWithContext(ctx, method, path, reader)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
return rq, nil
}
// Do sends a request.
// response is stored in res, and returns
// the processing result and error object.
//
// Do はリクエストを送信する.
// res にレスポンスを格納し, 処理結果とエラーオブジェクトを返す.
func (c *opaClient) Do(req *http.Request, res interface{}) (*ResultInfo, error) {
ctx, cancel := context.WithTimeout(
req.Context(),
getTimeout(req.Context()),
)
req = req.WithContext(ctx)
defer func() {
cancel()
if req.Body != nil {
req.Body.Close()
}
}()
rs, err := c.http.Do(req)
if errors.Is(err, context.DeadlineExceeded) {
return nil, fmt.Errorf("request timeout: %w", err)
}
if err != nil {
return nil, fmt.Errorf("an error occurred during request: %w", err)
}
defer rs.Body.Close()
b, err := io.ReadAll(rs.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
body := &response{}
if err := json.Unmarshal(b, body); err != nil {
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
}
info := body.Result
info.StatusCode = rs.StatusCode
if res == nil || len(body.Data) == 0 {
return info, nil
}
if err := json.Unmarshal(body.Data, res); err != nil {
return nil, fmt.Errorf("failed to unmarshal response.data: %w", err)
}
return info, nil
}
// timeout is the maximum timeout setting for the client.
// It is recommended to set it to 30s or more, so double it.
// Normally, no response is expected if you wait longer than this.
//
// timeout はクライアントの最長タイムアウト設定.
// 30s 以上に設定することが推奨されている為, 倍を設定.
// 通常, これ以上待機してもレスポンスは期待できない.
const timeout = 60 * time.Second
type ctxkeyTimeout struct{}
func ctxWithTimeout(ctx context.Context, d time.Duration) context.Context {
return context.WithValue(ctx, &ctxkeyTimeout{}, d)
}
func getTimeout(ctx context.Context) time.Duration {
if d, ok := ctx.Value(&ctxkeyTimeout{}).(time.Duration); ok {
return d
}
return timeout
}
type authInterceptor struct {
creds *Credentials
next http.RoundTripper
}
var _ http.RoundTripper = (*authInterceptor)(nil)
func newAuthenticateInterceptor(
creds *Credentials,
next http.RoundTripper,
) http.RoundTripper {
if creds == nil {
panic("*Credentials must not be nil")
}
return &authInterceptor{creds: creds, next: next}
}
// RoundTrip intercepts the request and sets the authentication information.
//
// RoundTrip はリクエストに割り込んで認証情報を設定する.
func (i *authInterceptor) RoundTrip(req *http.Request) (*http.Response, error) {
s, err := internal.NewSigner(i.creds.apiKey, i.creds.apiKeySecret, req)
if err != nil {
return nil, err
}
u, _ := url.Parse(string(i.creds.env) + req.URL.String())
req.URL = u
req.Header.Set("Content-Type", s.ContentType())
req.Header.Set("Authorization", s.Sign())
if i.creds.merchantID != "" {
req.Header.Set("X-Assume-Merchant", i.creds.merchantID)
}
return i.next.RoundTrip(req)
}