-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
250 lines (209 loc) · 5.78 KB
/
util.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
const (
apiEndpoint = "https://apiexplorer.cn-north-4.myhuaweicloud.com"
apiDetailEndpoint = "https://console.huaweicloud.com/apiexplorer"
MaxPageSize = 100
)
type ListAPIOpts struct {
Offset int
Limit int
ProductShort string
Version string
}
// ToListQuery formats a ListAPIOpts into a query string.
func (opts ListAPIOpts) ToListQuery() string {
params := url.Values{}
params.Add("offset", strconv.Itoa(opts.Offset))
params.Add("limit", strconv.Itoa(opts.Limit))
if opts.ProductShort != "" {
params.Add("product_short", opts.ProductShort)
}
if opts.Version != "" {
params.Add("info_version", opts.Version)
}
return params.Encode()
}
type APIGroup struct {
ID string `json:"id"`
Name string `json:"name"`
Products []APIProduct `json:"products"`
}
type APIProduct struct {
Name string `json:"name"`
ProductShort string `json:"productshort"`
Link string `json:"link"`
APICount int `json:"api_count"`
HasDate bool `json:"has_data"`
Recommend bool `json:"is_recommend"`
Global bool `json:"is_global"`
}
type APIBasicInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Alias string `json:"alias_name"`
Method string `json:"method"`
Summary string `json:"summary"`
Tags string `json:"tags"`
ProductShort string `json:"product_short"`
Version string `json:"info_version"`
}
type ProductVersion struct {
Name string `json:"name"`
Description string `json:"description"`
}
func getAllProducts() ([]APIGroup, error) {
url := fmt.Sprintf("%s/v4/products", apiEndpoint)
fmt.Printf("[DEBUG] list products url: %s\n", url)
body, err := httpRequest("GET", url, nil, nil)
if err != nil {
fmt.Printf("[ERROR] request %s failed, reason: %s\n", url, err)
return nil, err
}
//fmt.Printf("[DEBUG] api response: %v\n", string(body))
response := struct {
Groups []APIGroup `json:"groups"`
}{}
if err := json.Unmarshal(body, &response); err != nil {
fmt.Printf("[ERROR] Unmarshal failed: %s\n", err)
return nil, err
}
return response.Groups, nil
}
func getProductVersions(product string) ([]string, error) {
result := []string{}
url := fmt.Sprintf("%s/v2/versions?productshort=%s", apiEndpoint, product)
body, err := httpRequest("GET", url, nil, nil)
if err != nil {
fmt.Printf("[ERROR] request %s failed, reason: %s\n", url, err)
return nil, err
}
response := struct {
Count int `json:"count"`
IsMultiple bool `json:"is_multiple_version"`
Versions []ProductVersion `json:"versions"`
}{}
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("[ERROR] Unmarshal failed: %s\n", err)
return nil, err
}
for _, v := range response.Versions {
result = append(result, v.Name)
}
// add an empty version if not found
if len(result) == 0 {
return []string{""}, nil
}
return result, nil
}
func getProductAPIs(product, version string) ([]APIBasicInfo, error) {
result := []APIBasicInfo{}
baseURL := fmt.Sprintf("%s/v3/apis", apiEndpoint)
pageSize := MaxPageSize
offset := 0
listOpts := ListAPIOpts{
Offset: offset,
Limit: MaxPageSize,
ProductShort: product,
Version: version,
}
for pageSize == MaxPageSize {
query := listOpts.ToListQuery()
url := fmt.Sprintf("%s?%s", baseURL, query)
//fmt.Printf("[DEBUG] list url: %s\n", url)
body, err := httpRequest("GET", url, nil, nil)
if err != nil {
fmt.Printf("[ERROR] request %s failed, reason: %s\n", url, err)
return nil, err
}
response := struct {
Count int `json:"count"`
APIs []APIBasicInfo `json:"api_basic_infos"`
}{}
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("[ERROR] Unmarshal failed: %s\n", err)
return nil, err
}
result = append(result, response.APIs...)
pageSize = len(response.APIs)
listOpts.Offset += pageSize
if listOpts.Offset == response.Count {
break
}
}
return result, nil
}
func getAPIDetail(product, apiName, apiVersion, region string) ([]byte, error) {
url := fmt.Sprintf("%s/v4/apis/detail?product_short=%s&name=%s",
apiDetailEndpoint, product, apiName)
if apiVersion != "" {
url += fmt.Sprintf("&info_version=%s", apiVersion)
}
if region != "" {
url += fmt.Sprintf("®ion_id=%s", region)
}
body, err := httpRequest("GET", url, nil, nil)
if err != nil {
fmt.Printf("[ERROR] request %s failed, reason: %s\n", url, err)
return nil, err
}
return body, nil
}
func httpRequest(method, url string, jsonBody interface{}, headers map[string]string) ([]byte, error) {
var body io.Reader
var contentType string
if jsonBody != nil {
rendered, err := json.Marshal(jsonBody)
if err != nil {
return nil, err
}
body = bytes.NewReader(rendered)
contentType = "application/json; charset=UTF-8"
}
client := &http.Client{
Timeout: 60 * time.Second,
}
request, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
authToken := os.Getenv("HW_TOKEN")
if authToken != "" {
request.Header.Set("X-Auth-Token", authToken)
}
if contentType != "" {
request.Header.Set("Content-Type", contentType)
}
if eLanguage {
request.Header.Set("X-Language", "en-us")
}
request.Header.Set("Accept", "application/json, text/plain, */*")
request.Header.Set("X-Requested-With", "XMLHttpRequest")
for k, v := range headers {
request.Header.Add(k, v)
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
rst, err := ioutil.ReadAll(response.Body)
response.Body.Close()
defer client.CloseIdleConnections()
if response.StatusCode != http.StatusOK {
return rst, fmt.Errorf("Response Code %d", response.StatusCode)
}
return rst, nil
}