/
configuration.go
183 lines (164 loc) · 5.61 KB
/
configuration.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
/*
* Tecent Cloud Archive Storage Golang SDK.
*
* Tecent Cloud Archive Storage Golang SDK.
*
* API version: 1.5.0
* Contact: chennqqi@qq.com
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package cas
import (
"fmt"
"net/http"
"strings"
"time"
)
// contextKeys are used to identify the type of value in the context.
// Since these are string, it is possible to get a short description of the
// context key for logging and debugging using key.String().
type contextKey string
func (c contextKey) String() string {
return "auth " + string(c)
}
var (
// ContextOAuth2 takes an oauth2.TokenSource as authentication for the request.
ContextOAuth2 = contextKey("token")
// ContextBasicAuth takes BasicAuth as authentication for the request.
ContextBasicAuth = contextKey("basic")
// ContextAccessToken takes a string oauth2 access token as authentication for the request.
ContextAccessToken = contextKey("accesstoken")
// ContextAPIKey takes an APIKey as authentication for the request
ContextAPIKey = contextKey("apikey")
)
// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
type BasicAuth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
}
// APIKey provides API key based authentication to a request passed via context using ContextAPIKey
type APIKey struct {
Key string
Prefix string
}
// ServerVariable stores the information about a server variable
type ServerVariable struct {
Description string
DefaultValue string
EnumValues []string
}
// ServerConfiguration stores the information about a server
type ServerConfiguration struct {
Url string
Description string
Variables map[string]ServerVariable
}
// Configuration stores the configuration of the API client
type Configuration struct {
Region string `json:"region"`
AppId string `json:"appid"`
AccessKey string `json:"access_key"`
AccessSecret string `json:"access_secret"`
SignExpire time.Duration `json:"secret_expire"`
SignKey string `json:"sign_key"`
SignKeyStart int64 `json:"sign_key_start"`
SignKeyEnd int64 `json:"sing_key_end"`
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Debug bool `json:"debug,omitempty"`
Servers []ServerConfiguration
HTTPClient *http.Client
}
// NewConfiguration returns a new Configuration object
func NewConfiguration() *Configuration {
cfg := &Configuration{
BasePath: "http://cas.ap-beijing.myqcloud.com/-",
DefaultHeader: make(map[string]string),
UserAgent: "OpenAPI-Generator/1.0.0/go",
Debug: false,
Servers: []ServerConfiguration{
{
Url: "http://cas.ap-{region}.myqcloud.com/{basePath}",
Description: "The production API server",
Variables: map[string]ServerVariable{
"basePath": ServerVariable{
Description: "appId or as named UID",
DefaultValue: "-",
},
"region": ServerVariable{
Description: "cas region, options [chengdu/beijing/guangzhou/shanghai]",
DefaultValue: "beijing",
EnumValues: []string{
"beijing",
"chengdu",
"guangzhou",
"shanghai",
},
},
// "appid": ServerVariable{
// Description: "appid of cas resource owner, if api secret is main user,this is opiton param",
// DefaultValue: "-",
// },
// "access_key": ServerVariable{
// Description: "tecent cloud api key, required",
// DefaultValue: "",
// },
// "access_secret": ServerVariable{
// Description: "api secret, secret mode, secret_expire is required.",
// DefaultValue: "",
// },
// "secret_expire": ServerVariable{
// Description: "tecent cloud api secret",
// DefaultValue: "86400s",
// },
// "sign_key": ServerVariable{
// Description: "sign_key, if use sign_key mode, sign_key_start,sign_key_end are required",
// DefaultValue: "",
// },
// "sign_key_start": ServerVariable{
// Description: "unix timestamp of sign_key start time",
// DefaultValue: "0",
// },
// "sign_key_end": ServerVariable{
// Description: "unix timestamp of sign_key end time",
// DefaultValue: "0",
// },
},
},
},
}
return cfg
}
// AddDefaultHeader adds a new HTTP header to the default header in the request
func (c *Configuration) AddDefaultHeader(key string, value string) {
c.DefaultHeader[key] = value
}
// ServerUrl returns URL based on server settings
func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) {
if index < 0 || len(c.Servers) <= index {
return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers)-1)
}
server := c.Servers[index]
url := server.Url
// go through variables and replace placeholders
for name, variable := range server.Variables {
if value, ok := variables[name]; ok {
found := bool(len(variable.EnumValues) == 0)
for _, enumValue := range variable.EnumValues {
if value == enumValue {
found = true
}
}
if !found {
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
}
url = strings.Replace(url, "{"+name+"}", value, -1)
} else {
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
}
}
return url, nil
}