-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtmeapi.go
34 lines (27 loc) · 930 Bytes
/
tmeapi.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
package tmeapi
import (
"strings"
"net/url"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"net/http"
)
type TMEApi struct {
Token string
Secret string
}
func Client(token string, secret string) *TMEApi {
return &TMEApi{token, secret}
}
func (tmeApi TMEApi) Request(requestUrl string, formValues url.Values) (*http.Response, error) {
formValues.Add("Token", tmeApi.Token)
formValues.Add("ApiSignature", CalculateSignature("POST", requestUrl, formValues, tmeApi.Secret))
return http.PostForm(requestUrl, formValues)
}
func CalculateSignature(method string, requestUrl string, urlValues url.Values, secret string) string {
signatureBase := strings.Join([]string{method, url.QueryEscape(requestUrl), url.QueryEscape(urlValues.Encode())}, "&")
mac := hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(signatureBase))
return base64.StdEncoding.EncodeToString([]byte(mac.Sum(nil)))
}