From 042e5ec454db0185bad607fd0583dadb23aae01c Mon Sep 17 00:00:00 2001 From: zuoyong8 Date: Fri, 8 Jul 2022 14:11:12 +0800 Subject: [PATCH] add post with interface --- xhttp.go | 31 +++++++++++++++++++++++-------- xhttp_test.go | 30 +++++++++++++----------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/xhttp.go b/xhttp.go index 0544f23..904b7f7 100644 --- a/xhttp.go +++ b/xhttp.go @@ -10,6 +10,10 @@ import ( "time" ) +const ( + timeoutSecond = 10 +) + //post func Post(url string, headers Values, bs []byte) ([]byte, error) { return post(url, headers, bs) @@ -25,15 +29,26 @@ func PostJson(url string, headers Values, body interface{}) ([]byte, error) { return post(url, headers, bs) } +//post并解析返回值 +func PostWithInterface(url string, headers Values, body interface{}, data interface{}) error { + bs, err := json.Marshal(body) + if err != nil { + return err + } + resultBytes, err := post(url, headers, bs) + if err != nil { + return err + } + return json.Unmarshal(resultBytes, &data) +} + //获取response body数据,并解析 func GetParseData(url string, headers Values, data interface{}) error { - bs, status, err := get(url, headers) - if err == nil && status == 200 && bs != nil { - if err := json.Unmarshal(bs, &data); err != nil { - return err - } + bs, _, err := get(url, headers) + if err != nil { + return err } - return err + return json.Unmarshal(bs, &data) } //get @@ -73,7 +88,7 @@ func get(urlAddr string, headers Values) ([]byte, int, error) { return body, resp.StatusCode, nil } -//post +//post func post(urlAddr string, headers Values, bs []byte) ([]byte, error) { //check url if _, err := url.ParseRequestURI(urlAddr); err != nil { @@ -113,7 +128,7 @@ func httpClient() http.Client { //超时处理 func dialTimeout(network, addr string) (net.Conn, error) { - conn, err := net.DialTimeout(network, addr, time.Second*10) + conn, err := net.DialTimeout(network, addr, time.Second*time.Duration(timeoutSecond)) if err != nil { return conn, err } diff --git a/xhttp_test.go b/xhttp_test.go index d280ad6..9591b08 100644 --- a/xhttp_test.go +++ b/xhttp_test.go @@ -6,25 +6,21 @@ import ( ) func TestGet(t *testing.T) { - url := "api.github.com/repos/ethereum/go-ethereum/releases/latest" + url := "http://ip-api.com/json/" - bs, code, err := Get(url, nil) + bs, status, err := Get(url, nil) + fmt.Println(string(bs), status, err) - fmt.Println(string(bs), code, err) + type data struct { + Code int `json:"code"` + Data struct { + BlockNumber int64 `json:"blockNumber"` + } `json:"data"` + Msg string `json:"msg"` + } - // headers := make(Values) - // headers.Set("Content-Type", "application/json") + var d data + err = GetParseData(url, nil, &d) - // bs, code, err := Get(url, headers) - // fmt.Println(string(bs), code, err) - - // type data struct { - // RS int `json:"rs"` - // Code int `json:"code"` - // Address string `json:"address"` - // IsDomain int `json:"isDomain"` - // } - // var d data - // err = GetParseData(url, nil, &d) - // fmt.Println(d) + fmt.Println(d.Data.BlockNumber, err) }