Skip to content

Commit

Permalink
add post with interface
Browse files Browse the repository at this point in the history
  • Loading branch information
zuoyong8 committed Jul 8, 2022
1 parent 4548662 commit 042e5ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
31 changes: 23 additions & 8 deletions xhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 13 additions & 17 deletions xhttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 042e5ec

Please sign in to comment.