...
|
...
|
@@ -4,11 +4,12 @@ import ( |
|
|
"net/http"
|
|
|
"strings"
|
|
|
"net/url"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"sort"
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
"time"
|
|
|
"github.com/antonholmquist/jason"
|
|
|
)
|
|
|
|
|
|
type Http struct {
|
...
|
...
|
@@ -26,30 +27,32 @@ func Api(host string) *Http { |
|
|
return &api
|
|
|
}
|
|
|
|
|
|
func (this *Http) Get(path string, data map[string]interface{}) (interface{}, error) {
|
|
|
func (this *Http) Get(path string, data map[string]interface{}) (*jason.Object, error) {
|
|
|
apiSign(&data)
|
|
|
url := this.url.String() + queryBuild(path, &data)
|
|
|
|
|
|
fmt.Println(url)
|
|
|
|
|
|
t1 := time.Now()
|
|
|
res, err := this.client.Get(url)
|
|
|
|
|
|
t2 := time.Now()
|
|
|
d := t2.Sub(t1)
|
|
|
|
|
|
fmt.Println(d, url)
|
|
|
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
result := new(interface{})
|
|
|
json.NewDecoder(res.Body).Decode(&result)
|
|
|
|
|
|
return result, nil
|
|
|
return jason.NewObjectFromReader(res.Body)
|
|
|
}
|
|
|
|
|
|
func queryBuild(path string, data *map[string]interface{}) string {
|
|
|
var params []string
|
|
|
|
|
|
for k, v := range *data {
|
|
|
params = append(params, k + "=" + v.(string))
|
|
|
params = append(params, k + "=" + ToString(v))
|
|
|
}
|
|
|
|
|
|
query := strings.Join(params, "&")
|
...
|
...
|
@@ -79,10 +82,17 @@ func apiSign(v *map[string]interface{}) { |
|
|
sort.Strings(keys)
|
|
|
|
|
|
for _, k := range keys {
|
|
|
params = append(params, (k + "=" + data[k].(string)))
|
|
|
|
|
|
params = append(params, (k + "=" + ToString(data[k])))
|
|
|
}
|
|
|
|
|
|
value := strings.Join(params, "&")
|
|
|
hash := md5.Sum([]byte(strings.ToLower(value)))
|
|
|
data["client_secret"] = hex.EncodeToString(hash[:])
|
|
|
}
|
|
|
|
|
|
// ToString convert the input to a string.
|
|
|
func ToString(obj interface{}) string {
|
|
|
res := fmt.Sprintf("%v", obj)
|
|
|
return string(res)
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|