-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
228 lines (186 loc) · 6.27 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
/*
Package uspeedo is a package of utilities to setup uspeedo sdk and improve using experience
*/
package uspeedo
import (
"fmt"
"time"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/version"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/utils"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/auth"
uerr "github.com/uspeedo/uspeedo-sdk-go/uspeedo/error"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/http"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/log"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/request"
"github.com/uspeedo/uspeedo-sdk-go/uspeedo/response"
)
// Version is the version of sdk
const Version = version.Version
const headerKeyRequestUUID = "X-Speedo-Request-Uuid"
type ClientMeta struct {
Product string
}
type ServiceClient interface {
AddRequestHandler(h RequestHandler) error
AddResponseHandler(h ResponseHandler) error
AddHttpRequestHandler(h HttpRequestHandler) error
AddHttpResponseHandler(h HttpResponseHandler) error
}
// Client 客户端
type Client struct {
// configurations
credential *auth.Credential
config *Config
// composited instances
httpClient http.Client
logger log.Logger
// internal properties
requestHandlers []RequestHandler
httpRequestHandlers []HttpRequestHandler
responseHandlers []ResponseHandler
httpResponseHandlers []HttpResponseHandler
// client information injection
meta ClientMeta
}
// NewClient will create an client of uspeedo sdk
func NewClient(config *Config, credential *auth.Credential) *Client {
client := Client{
credential: credential,
config: config,
meta: ClientMeta{},
}
client.requestHandlers = append(client.requestHandlers, defaultRequestHandlers...)
client.httpRequestHandlers = append(client.httpRequestHandlers, defaultHttpRequestHandlers...)
client.responseHandlers = append(client.responseHandlers, defaultResponseHandlers...)
client.httpResponseHandlers = append(client.httpResponseHandlers, defaultHttpResponseHandlers...)
client.logger = log.New()
if config != nil {
client.logger.SetLevel(config.LogLevel)
}
return &client
}
func NewClientWithMeta(config *Config, credential *auth.Credential, meta ClientMeta) *Client {
client := NewClient(config, credential)
client.meta = meta
return client
}
// SetHttpClient will setup a http client
func (c *Client) SetHttpClient(httpClient http.Client) error {
c.httpClient = httpClient
return nil
}
// GetCredential will return the credential config of client.
func (c *Client) GetCredential() *auth.Credential {
return c.credential
}
// GetConfig will return the config of client.
func (c *Client) GetConfig() *Config {
return c.config
}
// GetMeta will return the meta data of client.
func (c *Client) GetMeta() ClientMeta {
return c.meta
}
// SetLogger will set the logger of client
func (c *Client) SetLogger(logger log.Logger) {
c.logger = logger
}
// GetLogger will set the logger of client
func (c *Client) GetLogger() log.Logger {
return c.logger
}
// InvokeAction will do an action request from a request struct and set response value into res struct pointer
func (c *Client) InvokeAction(action string, req request.Common, resp response.Common) error {
return c.InvokeActionWithPatcher(action, req, resp, utils.RetCodePatcher)
}
// InvokeActionWithPatcher will invoke action by patchers
func (c *Client) InvokeActionWithPatcher(action string, req request.Common, resp response.Common, patches ...utils.Patch) error {
var err error
_ = req.SetAction(action)
req.SetRequestTime(time.Now())
resp.SetRequest(req)
if c.GetCredential() == nil {
return uerr.NewClientError(
uerr.ErrNullCredential,
fmt.Errorf("null credential error, please set it before request"),
)
}
if c.GetConfig() == nil {
return uerr.NewClientError(
uerr.ErrNullConfig,
fmt.Errorf("null config error, please set it before request"),
)
}
if c.credential.CanExpire && c.credential.IsExpired() {
return uerr.NewClientError(
uerr.ErrCredentialExpired,
fmt.Errorf("credential is expired at %s", c.credential.Expires.Format(time.RFC3339)),
)
}
for _, handler := range c.requestHandlers {
req, err = handler(c, req)
if err != nil {
return uerr.NewClientError(uerr.ErrInvalidRequest, err)
}
}
httpReq, err := c.buildHTTPRequest(req)
if err != nil {
return uerr.NewClientError(uerr.ErrInvalidRequest, err)
}
for _, handler := range c.httpRequestHandlers {
httpReq, err = handler(c, httpReq)
if err != nil {
return uerr.NewClientError(uerr.ErrInvalidRequest, err)
}
}
if c.httpClient == nil {
httpClient := http.NewHttpClient()
c.httpClient = &httpClient
}
httpResp, err := c.httpClient.Send(httpReq)
// use response middleware to handle http response
// such as convert some http status to error
for _, handler := range c.httpResponseHandlers {
httpResp, err = handler(c, httpReq, httpResp, err)
}
if err == nil {
// use patch object to resolve the http response body
// in general, it will be fix common server error before server bug fix is released.
body := httpResp.GetBody()
for _, patch := range patches {
body = patch.Patch(body)
}
err = c.unmarshalHTTPResponse(body, resp)
}
// try to get request uuid
if httpResp != nil {
requestUUID := httpResp.GetHeaders().Get(headerKeyRequestUUID)
resp.SetRequestUUID(requestUUID)
}
// use response middle to build and convert response when response has been created.
// such as retry, report traceback, print log and etc.
for _, handler := range c.responseHandlers {
resp, err = handler(c, req, resp, err)
}
return err
}
// AddHttpRequestHandler will append a response handler to client
func (c *Client) AddHttpRequestHandler(h HttpRequestHandler) error {
c.httpRequestHandlers = append([]HttpRequestHandler{h}, c.httpRequestHandlers...)
return nil
}
// AddRequestHandler will append a response handler to client
func (c *Client) AddRequestHandler(h RequestHandler) error {
c.requestHandlers = append([]RequestHandler{h}, c.requestHandlers...)
return nil
}
// AddHttpResponseHandler will append a http response handler to client
func (c *Client) AddHttpResponseHandler(h HttpResponseHandler) error {
c.httpResponseHandlers = append(c.httpResponseHandlers, h)
return nil
}
// AddResponseHandler will append a response handler to client
func (c *Client) AddResponseHandler(h ResponseHandler) error {
c.responseHandlers = append(c.responseHandlers, h)
return nil
}