iedc-go/dri/tencent/company.go

120 lines
2.7 KiB
Go
Raw Normal View History

2022-01-22 21:13:19 +00:00
package tencent
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/common"
2022-01-22 21:13:19 +00:00
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
gourl "net/url"
2022-02-19 13:38:17 +00:00
2022-01-22 21:13:19 +00:00
"strings"
"time"
)
2022-03-12 17:48:54 +00:00
type company struct {
secretId string
secretKey string
}
var Company = company{}
func (that *company) Init(secretId, secretKey string) {
// 云市场分配的密钥Id
//secretId := "xxxx"
//// 云市场分配的密钥Key
//secretKey := "xxxx"
that.secretId = secretId
that.secretKey = secretKey
}
func (that *company) calcAuthorization(source string) (auth string, datetime string, err error) {
2022-02-19 13:38:17 +00:00
2022-01-22 21:13:19 +00:00
timeLocation, _ := time.LoadLocation("Etc/GMT")
datetime = time.Now().In(timeLocation).Format("Mon, 02 Jan 2006 15:04:05 GMT")
signStr := fmt.Sprintf("x-date: %s\nx-source: %s", datetime, source)
// hmac-sha1
2022-03-12 17:48:54 +00:00
mac := hmac.New(sha1.New, []byte(that.secretKey))
2022-01-22 21:13:19 +00:00
mac.Write([]byte(signStr))
sign := base64.StdEncoding.EncodeToString(mac.Sum(nil))
auth = fmt.Sprintf("hmac id=\"%s\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"%s\"",
2022-03-12 17:48:54 +00:00
that.secretId, sign)
2022-01-22 21:13:19 +00:00
return auth, datetime, nil
}
2022-03-12 17:48:54 +00:00
func (that *company) urlencode(params map[string]string) string {
2022-01-22 21:13:19 +00:00
var p = gourl.Values{}
for k, v := range params {
p.Add(k, v)
}
return p.Encode()
}
2022-03-12 17:48:54 +00:00
func (that *company) GetCompany(name string) Map {
2022-01-22 21:13:19 +00:00
// 云市场分配的密钥Id
//secretId := "xxxx"
//// 云市场分配的密钥Key
//secretKey := "xxxx"
source := "market"
// 签名
2022-03-12 17:48:54 +00:00
auth, datetime, _ := that.calcAuthorization(source)
2022-01-22 21:13:19 +00:00
// 请求方法
method := "GET"
// 请求头
headers := map[string]string{"X-Source": source, "X-Date": datetime, "Authorization": auth}
// 查询参数
queryParams := make(map[string]string)
queryParams["keyword"] = name
// body参数
bodyParams := make(map[string]string)
// url参数拼接
url := "https://service-3jnh3ku8-1256140209.gz.apigw.tencentcs.com/release/business4/geet"
if len(queryParams) > 0 {
2022-03-12 17:48:54 +00:00
url = fmt.Sprintf("%s?%s", url, that.urlencode(queryParams))
2022-01-22 21:13:19 +00:00
}
bodyMethods := map[string]bool{"POST": true, "PUT": true, "PATCH": true}
var body io.Reader = nil
if bodyMethods[method] {
2022-03-12 17:48:54 +00:00
body = strings.NewReader(that.urlencode(bodyParams))
2022-01-22 21:13:19 +00:00
headers["Content-Type"] = "application/x-www-form-urlencoded"
}
client := &http.Client{
Timeout: 5 * time.Second,
}
request, err := http.NewRequest(method, url, body)
if err != nil {
fmt.Println(err)
return nil
}
for k, v := range headers {
request.Header.Set(k, v)
}
response, err := client.Do(request)
if err != nil {
fmt.Println(err)
return nil
}
defer response.Body.Close()
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
return nil
}
res := string(bodyBytes)
fmt.Println(res)
return ObjToMap(res)
}