|
@@ -4,11 +4,12 @@ import ( |
|
@@ -4,11 +4,12 @@ import ( |
4
|
"net/http"
|
4
|
"net/http"
|
5
|
"strings"
|
5
|
"strings"
|
6
|
"net/url"
|
6
|
"net/url"
|
7
|
- "encoding/json"
|
|
|
8
|
"fmt"
|
7
|
"fmt"
|
9
|
"sort"
|
8
|
"sort"
|
10
|
"crypto/md5"
|
9
|
"crypto/md5"
|
11
|
"encoding/hex"
|
10
|
"encoding/hex"
|
|
|
11
|
+ "time"
|
|
|
12
|
+ "github.com/antonholmquist/jason"
|
12
|
)
|
13
|
)
|
13
|
|
14
|
|
14
|
type Http struct {
|
15
|
type Http struct {
|
|
@@ -26,30 +27,32 @@ func Api(host string) *Http { |
|
@@ -26,30 +27,32 @@ func Api(host string) *Http { |
26
|
return &api
|
27
|
return &api
|
27
|
}
|
28
|
}
|
28
|
|
29
|
|
29
|
-func (this *Http) Get(path string, data map[string]interface{}) (interface{}, error) {
|
30
|
+func (this *Http) Get(path string, data map[string]interface{}) (*jason.Object, error) {
|
30
|
apiSign(&data)
|
31
|
apiSign(&data)
|
31
|
url := this.url.String() + queryBuild(path, &data)
|
32
|
url := this.url.String() + queryBuild(path, &data)
|
32
|
|
33
|
|
33
|
- fmt.Println(url)
|
|
|
34
|
-
|
34
|
+ t1 := time.Now()
|
35
|
res, err := this.client.Get(url)
|
35
|
res, err := this.client.Get(url)
|
|
|
36
|
+
|
|
|
37
|
+ t2 := time.Now()
|
|
|
38
|
+ d := t2.Sub(t1)
|
|
|
39
|
+
|
|
|
40
|
+ fmt.Println(d, url)
|
|
|
41
|
+
|
36
|
if err != nil {
|
42
|
if err != nil {
|
37
|
return nil, err
|
43
|
return nil, err
|
38
|
}
|
44
|
}
|
39
|
|
45
|
|
40
|
defer res.Body.Close()
|
46
|
defer res.Body.Close()
|
41
|
|
47
|
|
42
|
- result := new(interface{})
|
|
|
43
|
- json.NewDecoder(res.Body).Decode(&result)
|
|
|
44
|
-
|
|
|
45
|
- return result, nil
|
48
|
+ return jason.NewObjectFromReader(res.Body)
|
46
|
}
|
49
|
}
|
47
|
|
50
|
|
48
|
func queryBuild(path string, data *map[string]interface{}) string {
|
51
|
func queryBuild(path string, data *map[string]interface{}) string {
|
49
|
var params []string
|
52
|
var params []string
|
50
|
|
53
|
|
51
|
for k, v := range *data {
|
54
|
for k, v := range *data {
|
52
|
- params = append(params, k + "=" + v.(string))
|
55
|
+ params = append(params, k + "=" + ToString(v))
|
53
|
}
|
56
|
}
|
54
|
|
57
|
|
55
|
query := strings.Join(params, "&")
|
58
|
query := strings.Join(params, "&")
|
|
@@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) { |
|
@@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) { |
79
|
sort.Strings(keys)
|
82
|
sort.Strings(keys)
|
80
|
|
83
|
|
81
|
for _, k := range keys {
|
84
|
for _, k := range keys {
|
82
|
- params = append(params, (k + "=" + data[k].(string)))
|
85
|
+
|
|
|
86
|
+ params = append(params, (k + "=" + ToString(data[k])))
|
83
|
}
|
87
|
}
|
84
|
|
88
|
|
85
|
value := strings.Join(params, "&")
|
89
|
value := strings.Join(params, "&")
|
86
|
hash := md5.Sum([]byte(strings.ToLower(value)))
|
90
|
hash := md5.Sum([]byte(strings.ToLower(value)))
|
87
|
data["client_secret"] = hex.EncodeToString(hash[:])
|
91
|
data["client_secret"] = hex.EncodeToString(hash[:])
|
88
|
}
|
92
|
}
|
|
|
93
|
+
|
|
|
94
|
+// ToString convert the input to a string.
|
|
|
95
|
+func ToString(obj interface{}) string {
|
|
|
96
|
+ res := fmt.Sprintf("%v", obj)
|
|
|
97
|
+ return string(res)
|
|
|
98
|
+} |