-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
192 lines (158 loc) · 4.26 KB
/
context.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
package core
import (
"errors"
"github.com/valyala/fasthttp"
)
type ContextParam struct {
Tenant string
TenantId string
Token string
AK string
SK string
Schema string
HostHeader string
Hosts []string
Headers map[string]string
Region Region
UseAirAuth bool
}
func (receiver *ContextParam) checkRequiredField(param *ContextParam) error {
if param.Tenant == "" {
return errors.New("application id is null")
}
if param.TenantId == "" {
return errors.New("tenant id is null")
}
if err := receiver.checkAuthRequiredField(param); err != nil {
return err
}
if param.Region == RegionUnknown {
return errors.New("region is null")
}
return nil
}
func (receiver *ContextParam) checkAuthRequiredField(param *ContextParam) error {
if param.UseAirAuth && param.Token == "" {
return errors.New("token is null")
}
if !param.UseAirAuth && (param.AK == "" || param.SK == "") {
return errors.New("ak or sk is null")
}
return nil
}
func NewContext(param *ContextParam) (*Context, error) {
err := param.checkRequiredField(param)
if err != nil {
return nil, err
}
result := &Context{
tenant: param.Tenant,
tenantId: param.TenantId,
token: param.Token,
schema: param.Schema,
hostHeader: param.HostHeader,
hosts: param.Hosts,
customerHeaders: param.Headers,
useAirAuth: param.UseAirAuth,
}
result.fillHosts(param)
result.fillVolcCredentials(param)
result.defaultHTTPCli = &fasthttp.Client{}
result.fillDefault()
return result, nil
}
type Context struct {
// A unique token assigned by bytedance, which is used to
// generate an authenticated signature when building a request.
// It is sometimes called "secret".
tenant string
// A unique token assigned by bytedance, which is used to
// generate an authenticated signature when building a request.
// It is sometimes called "secret".
tenantId string
// A unique identity assigned by Bytedance, which is need to fill in URL.
// It is sometimes called "company".
token string
volcCredentials Credential
// Schema of URL, server supports both "HTTPS" and "HTTP",
// in order to ensure communication security, please use "HTTPS"
schema string
// This field will be used when use ips to request server,
// it will be set in http header, which named "Host"
hostHeader string
// Server address, china use "rec-b.volcengineapi.com",
// other area use "tob.sgsnssdk.com" in default
hosts []string
// Customer-defined http headers, all requests will include these headers
customerHeaders map[string]string
defaultHTTPCli *fasthttp.Client
// use air auth, otherwise use volc auth
useAirAuth bool
}
func (receiver *Context) Tenant() string {
return receiver.tenant
}
func (receiver *Context) TenantId() string {
return receiver.tenantId
}
func (receiver *Context) Token() string {
return receiver.token
}
func (receiver *Context) AK() string {
return receiver.volcCredentials.AccessKeyID
}
func (receiver *Context) SK() string {
return receiver.volcCredentials.SecretAccessKey
}
func (receiver *Context) Schema() string {
return receiver.schema
}
func (receiver *Context) HostHeader() string {
return receiver.hostHeader
}
func (receiver *Context) Hosts() []string {
return receiver.hosts
}
func (receiver *Context) UseAirAuth() bool {
return receiver.useAirAuth
}
func (receiver *Context) UseVolcAuth() bool {
return !receiver.useAirAuth
}
func (receiver *Context) CustomerHeaders() map[string]string {
return receiver.customerHeaders
}
func (receiver *Context) fillHosts(param *ContextParam) {
if len(param.Hosts) > 0 {
receiver.hosts = param.Hosts
return
}
if param.Region == RegionAirCn {
receiver.hosts = airCnHosts
return
}
if param.Region == RegionAirSg {
receiver.hosts = airSgHosts
return
}
}
func (receiver *Context) fillDefault() {
if receiver.schema == "" {
receiver.schema = "https"
}
}
func (receiver *Context) fillVolcCredentials(param *ContextParam) {
c := Credential{
AccessKeyID: param.AK,
SecretAccessKey: param.SK,
Service: volcAuthService,
}
// fill region
switch param.Region {
case RegionAirSg:
c.Region = "ap-singapore-1"
default: //Region "CN" and "AIR_CN" belong to "cn-north-1"
c.Region = "cn-north-1"
}
receiver.volcCredentials = c
}