-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiUtil.go
71 lines (65 loc) · 1.62 KB
/
apiUtil.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
package ylyOpenApi
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type apiResponse struct {
Error int
Error_description string
Body interface{}
}
type APIError struct {
Code string
Message string
}
func (e APIError) Error() string {
return e.Code + " " + e.Message
}
func APIInterface(config *Config, strAction string, params map[string]interface{}) (interface{}, error) {
tokenBody := config.token.Body
t := time.Now().Unix()
timestamp := strconv.FormatInt(t, 10)
sign := config.GetSign(timestamp)
reqParams := url.Values{}
params["client_id"] = config.clientId
params["access_token"] = tokenBody.Access_token
params["timestamp"] = timestamp
params["sign"] = sign
params["id"] = GetUUID4()
for k, v := range params {
v := v.(string)
reqParams.Set(k, v)
}
config.info(strAction + "[req]:" + reqParams.Encode())
host := config.GetHost()
reqUrl := host + strAction
resp, _ := http.PostForm(reqUrl, reqParams)
body, _ := ioutil.ReadAll(resp.Body)
config.info(strAction + "[res]:" + string(body))
defer resp.Body.Close()
var apiResp apiResponse
json.Unmarshal([]byte(body), &apiResp)
if apiResp.Error_description != "success" {
errorInfo := make(map[string]string)
errorInfo["code"] = strconv.Itoa(apiResp.Error)
errorInfo["message"] = apiResp.Error_description
tmp, _ := json.Marshal(errorInfo)
config.error(string(tmp))
var apiErr APIError
json.Unmarshal(tmp, &apiErr)
return nil, apiErr
}
fmt.Println(apiResp.Body)
return apiResp.Body , nil
}
//获取UUID4
func GetUUID4() string {
u4 := uuid.New()
return u4.String()
}