-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAPIClient.go
135 lines (106 loc) · 3.12 KB
/
APIClient.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
package main
import (
"net/http"
"net/url"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"sort"
"strings"
"unicode/utf8"
"crypto/hmac"
"log"
)
type APIClient struct {
Key string
Secret string
}
const (
METHOD_GET = "GET"
METHOD_PUT = "PUT"
METHOD_POST = "POST"
)
const API_URL = "https://api.zadarma.com/"
func (api APIClient) CallMethod(methodName string, params url.Values, methodType string) interface{} {
client := http.Client{}
fullURL := BuildAPIUrl(methodName, params)
sign := Sign(api, methodName, params)
request, err := http.NewRequest(methodType, fullURL, nil)
if err != nil {
log.Printf("Error when creating newRequest for \"%s\": %s", fullURL, err.Error())
return nil
}
request.Header.Set("Authorization", api.Key+":"+sign)
response, requesting_err := client.Do(request)
if requesting_err != nil {
log.Printf("Error when requesting %s: %s", fullURL, requesting_err.Error())
return nil
}
var result interface{}
decoder := json.NewDecoder(response.Body)
decoder.Decode(&result)
return result
}
func (api APIClient) Callback(from, to, sip string, isPredicted bool) interface{} {
params := make(url.Values)
params.Add("from", from)
params.Add("to", to)
if sip != "" {
params.Add("sip", sip)
}
if isPredicted {
params.Add("isPredicted", "1")
}
return api.CallMethod("/v1/request/callback/", params, METHOD_GET)
}
// don't work :(
func (api APIClient) ChangeCallerID(sip, callerID string) interface{} {
params := make(url.Values)
params.Add("id", sip)
params.Add("number", callerID)
return api.CallMethod("/v1/sip/callerid/", params, METHOD_PUT)
}
func (api APIClient) DirectNumbers() interface{} {
return api.CallMethod("/v1/direct_numbers/", nil, METHOD_GET)
}
func (api APIClient) SIMs() interface{} {
return api.CallMethod("/v1/sim/", nil, METHOD_GET)
}
//func (api APIClient) SendSMS(number, message, callerID string) interface{} {
// return nil
//}
func BuildAPIUrl(methodName string, params url.Values) string {
var resultURL string
if strings.HasPrefix(methodName, "/") && strings.HasSuffix(API_URL, "/") {
resultURL = API_URL + strings.TrimLeft(methodName, "/") + "?"
} else {
resultURL = API_URL + methodName + "?"
}
for name, value := range params {
resultURL += name + "=" + value[0] + "&"
}
resultURL = strings.TrimRight(resultURL, "&")
return resultURL
}
func Sign(api APIClient, methodName string, params url.Values) string {
var paramParts []string
for name, value := range params {
paramParts = append(paramParts, name+"="+value[0])
}
sort.Slice(paramParts, func(i, j int) bool {
firstRune, _ := utf8.DecodeRuneInString(paramParts[i])
secondRune, _ := utf8.DecodeRuneInString(paramParts[j])
return firstRune < secondRune
})
paramsUrlStr := strings.Join(paramParts, "&")
md5Params := fmt.Sprintf("%x", md5.Sum([]byte(paramsUrlStr)))
sign := methodName + paramsUrlStr + md5Params
hmacer := hmac.New(sha1.New, []byte(api.Secret))
hmacer.Write([]byte(sign))
sha1Result := hmacer.Sum(nil)
sha1Hash := fmt.Sprintf("%x", sha1Result)
base64Encoded := base64.StdEncoding.EncodeToString([]byte(sha1Hash))
return base64Encoded
}