You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/libs/tools/tools.go

45 lines
748 B
Go

3 years ago
package tools
import (
"encoding/json"
"github.com/veypi/OneBD"
"net/http"
"net/url"
)
type Initer interface {
Init(OneBD.Meta) error
}
func MultiIniter(m OneBD.Meta, is ...Initer) (err error) {
for _, i := range is {
err = i.Init(m)
if err != nil {
return
}
}
return
}
func Query(addr string, query map[string]string, res interface{}) error {
u, err := url.Parse(addr)
if err != nil {
return err
}
paras := &url.Values{}
//设置请求参数
for k, v := range query {
paras.Set(k, v)
}
u.RawQuery = paras.Encode()
resp, err := http.Get(u.String())
//关闭资源
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
return json.NewDecoder(resp.Body).Decode(res)
}