/
client.go
177 lines (157 loc) · 4.96 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
// Copyright 2021-2023 The sacloud/go-http authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package http
import (
"compress/gzip"
"context"
"fmt"
"net/http"
"runtime"
"time"
"github.com/hashicorp/go-retryablehttp"
)
var (
// DefaultUserAgent デフォルトのユーザーエージェント
DefaultUserAgent = fmt.Sprintf(
"go-http/v%s (%s/%s; +https://github.com/sacloud/go-http)",
Version,
runtime.GOOS,
runtime.GOARCH,
)
// DefaultAcceptLanguage デフォルトのAcceptLanguage
DefaultAcceptLanguage = ""
// DefaultRetryMax デフォルトのリトライ回数
DefaultRetryMax = 10
// DefaultRetryWaitMin デフォルトのリトライ間隔(最小)
DefaultRetryWaitMin = 1 * time.Second
// DefaultRetryWaitMax デフォルトのリトライ間隔(最大)
DefaultRetryWaitMax = 64 * time.Second
)
// Client さくらのクラウドAPI(secure.sakura.ad.jp)向けのHTTPクライアント
//
// レスポンスの状態に応じてリトライする仕組みを持つ
// デフォルトだとレスポンスステータスコード423、または503を受け取った場合にRetryMax回リトライする
//
// リトライ間隔はRetryMinからRetryMaxまで指数的に増加する(Exponential Backoff)
//
// リトライ時にcontext.Canceled、またはcontext.DeadlineExceededの場合はリトライしない
type Client struct {
// AccessToken アクセストークン
AccessToken string `validate:"required"`
// AccessTokenSecret アクセストークンシークレット
AccessTokenSecret string `validate:"required"`
// ユーザーエージェント
UserAgent string
// Accept-Language
AcceptLanguage string
// Gzipを有効にするか
Gzip bool
// CheckRetryFunc リトライすべきか判定するためのfunc
CheckRetryFunc func(ctx context.Context, resp *http.Response, err error) (bool, error)
// リトライ回数
RetryMax int
// リトライ待ち時間(最小)
RetryWaitMin time.Duration
// リトライ待ち時間(最大)
RetryWaitMax time.Duration
// APIコール時に利用される*http.Client 未指定の場合http.DefaultClientが利用される
HTTPClient *http.Client
// RequestCustomizer リクエスト前に*http.Requestのカスタマイズを行うためのfunc
RequestCustomizer RequestCustomizer
}
// NewClient APIクライアント作成
func NewClient(token, secret string) *Client {
c := &Client{
AccessToken: token,
AccessTokenSecret: secret,
}
return c
}
func (c *Client) init() {
if c.UserAgent == "" {
c.UserAgent = DefaultUserAgent
}
if c.AcceptLanguage == "" {
c.AcceptLanguage = DefaultAcceptLanguage
}
if c.CheckRetryFunc == nil {
c.CheckRetryFunc = retryablehttp.DefaultRetryPolicy
}
if c.RetryMax == 0 {
c.RetryMax = DefaultRetryMax
}
if c.RetryWaitMin == 0 {
c.RetryWaitMin = DefaultRetryWaitMin
}
if c.RetryWaitMax == 0 {
c.RetryWaitMax = DefaultRetryWaitMax
}
}
func (c *Client) httpClient() *retryablehttp.Client {
return &retryablehttp.Client{
HTTPClient: c.HTTPClient,
RetryWaitMin: c.RetryWaitMin,
RetryWaitMax: c.RetryWaitMax,
RetryMax: c.RetryMax,
CheckRetry: c.CheckRetryFunc,
Backoff: retryablehttp.DefaultBackoff,
}
}
// Do APIコール実施
func (c *Client) Do(req *http.Request) (*http.Response, error) {
c.init()
// set headers
req.SetBasicAuth(c.AccessToken, c.AccessTokenSecret)
if req.Header.Get("Content-Type") == "" && req.Body != nil {
req.Header.Add("Content-Type", "application/json")
}
if c.Gzip && req.Header.Get("Accept-Encoding") == "" {
req.Header.Add("Accept-Encoding", "gzip")
}
if req.Header.Get("X-Requested-With") == "" {
req.Header.Add("X-Requested-With", "XMLHttpRequest")
}
if req.Header.Get("X-Sakura-Bigint-As-Int") == "" {
req.Header.Add("X-Sakura-Bigint-As-Int", "1") // Use BigInt on resource ids.
}
if req.Header.Get("User-Agent") == "" {
req.Header.Add("User-Agent", c.UserAgent)
}
if req.Header.Get("Accept-Language") == "" && c.AcceptLanguage != "" {
req.Header.Add("Accept-Language", c.AcceptLanguage)
}
if c.RequestCustomizer != nil {
if err := c.RequestCustomizer(req); err != nil {
return nil, err
}
}
request, err := retryablehttp.FromRequest(req)
if err != nil {
return nil, err
}
client := c.httpClient()
// API call
resp, err := client.Do(request)
if err != nil {
return nil, err
}
if c.Gzip && resp.Header.Get("Content-Encoding") == "gzip" {
body, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
resp.Body = body
}
return resp, err
}