Skip to content

Commit

Permalink
feat(httpx): add PostP
Browse files Browse the repository at this point in the history
  • Loading branch information
peakxie committed Apr 9, 2020
1 parent 271ec67 commit 4cd30b4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 49 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/peakxie/utils

go 1.13

require github.com/gin-gonic/gin v1.5.0
require (
github.com/gin-gonic/gin v1.5.0
google.golang.org/protobuf v1.20.1
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rm
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4 h1:+EOh4OY6tjM6ZueeUKinl1f0U2820HzQOuf1iqMnsks=
github.com/golang/protobuf v1.4.0-rc.4/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand All @@ -34,6 +43,14 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.20.1 h1:ESRXHgpUBG5D2I5mmsQIyYxB/tQIZfSZ8wLyFDf/N/U=
google.golang.org/protobuf v1.20.1/go.mod h1:KqelGeouBkcbcuB3HCk4/YH2tmNLk6YSWA5LIWeI/lY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
Expand Down
44 changes: 0 additions & 44 deletions httpsw/http.go

This file was deleted.

79 changes: 79 additions & 0 deletions httpx/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package httpx

import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"

"google.golang.org/protobuf/proto"
)

// http post远程调用,使用json协议
func PostJ(client *http.Client, url string, request interface{}, response interface{}) ([]byte, error) {
var reqBody []byte
var err error
var rsp *http.Response

if request != nil {
reqBody, err = json.Marshal(request)
if err != nil {
return nil, err
}
}

if client != nil {
rsp, err = client.Post(url, "application/json", bytes.NewReader(reqBody))
} else {
rsp, err = http.Post(url, "application/json", bytes.NewReader(reqBody))
}

if err != nil {
return nil, err
}
defer rsp.Body.Close()

rspBody, _ := ioutil.ReadAll(rsp.Body)
if response != nil {
err = json.Unmarshal(rspBody, response)
if err != nil {
return rspBody, err
}
}

return rspBody, nil
}

// http post远程调用,使用protobuf协议
func PostP(client *http.Client, url string, request proto.Message, response proto.Message) ([]byte, error) {
var reqBody []byte
var err error
var rsp *http.Response

if request != nil {
reqBody, err = proto.Marshal(request)
if err != nil {
return nil, err
}
}

if client != nil {
rsp, err = client.Post(url, "application/x-protobuf", bytes.NewReader(reqBody))
} else {
rsp, err = http.Post(url, "application/x-protobuf", bytes.NewReader(reqBody))
}

if err != nil {
return nil, err
}
defer rsp.Body.Close()

rspBody, _ := ioutil.ReadAll(rsp.Body)
if response != nil {
err = proto.Unmarshal(rspBody, response)
if err != nil {
return nil, err
}
}
return rspBody, nil
}
7 changes: 3 additions & 4 deletions httpsw/http_test.go → httpx/http_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package httpsw_test
package httpx_test

import (
"context"
"fmt"
"net/http"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/peakxie/utils/httpsw"
"github.com/peakxie/utils/httpx"
)

//test http接口
Expand Down Expand Up @@ -43,7 +42,7 @@ func TestHttpJ(t *testing.T) {

var rsp JsonTest
//发请求,自动打印请求包和回包,不需要自己序列化和反序列化协议
_, e := httpsw.PostJ(context.Background(), "http://localhost:8080/test", &JsonTest{Name: "name", Picture: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}, &rsp)
_, e := httpx.PostJ(nil, "http://localhost:8080/test", &JsonTest{Name: "name", Picture: "xxxxxxxxxxxxxxx"}, &rsp)
if e != nil {
fmt.Println("err", e)
} else {
Expand Down

0 comments on commit 4cd30b4

Please sign in to comment.