Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["mail_api_client.go"],
importpath = "go-common/app/service/main/vip/dao/mail-api-client",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["mail_api_client_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//library/net/http/blademaster:go_default_library",
"//library/net/netutil/breaker:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,62 @@
package client
import (
"context"
"encoding/json"
xhttp "net/http"
"strings"
bm "go-common/library/net/http/blademaster"
pkgerr "github.com/pkg/errors"
)
const (
_contentTypeJSON = "application/json"
)
//Client client is http client.
type Client struct {
client *bm.Client
}
// NewClient new a http client.
func NewClient(client *bm.Client) *Client {
cl := new(Client)
cl.client = client
return cl
}
// Get a json req http get.
func (client *Client) Get(c context.Context, uri string, params interface{}, res interface{}) (err error) {
req, err := client.NewRequest(xhttp.MethodGet, uri, params)
if err != nil {
return
}
return client.client.Do(c, req, res)
}
// Post a json req http post.
func (client *Client) Post(c context.Context, uri string, params interface{}, res interface{}) (err error) {
req, err := client.NewRequest(xhttp.MethodPost, uri, params)
if err != nil {
return
}
return client.client.Do(c, req, res)
}
// NewRequest new http request with method, uri, ip, values and headers.
func (client *Client) NewRequest(method, uri string, params interface{}) (req *xhttp.Request, err error) {
marshal, err := json.Marshal(params)
if err != nil {
err = pkgerr.Wrapf(err, "marshal:%v", params)
return
}
req, err = xhttp.NewRequest(method, uri, strings.NewReader(string(marshal)))
if err != nil {
err = pkgerr.Wrapf(err, "method:%s,uri:%s", method, string(marshal))
return
}
req.Header.Set("Content-Type", _contentTypeJSON)
return
}

View File

@@ -0,0 +1,80 @@
package client
import (
"context"
"testing"
"time"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/netutil/breaker"
xtime "go-common/library/time"
"github.com/smartystreets/goconvey/convey"
)
var cl *Client
func TestNewClient(t *testing.T) {
var client = bm.NewClient(&bm.ClientConfig{
App: &bm.App{
Key: "53e2fa226f5ad348",
Secret: "3cf6bd1b0ff671021da5f424fea4b04a",
},
Dial: xtime.Duration(time.Second),
Timeout: xtime.Duration(time.Second),
KeepAlive: xtime.Duration(time.Second),
Breaker: &breaker.Config{
Window: 10 * xtime.Duration(time.Second),
Sleep: 50 * xtime.Duration(time.Millisecond),
Bucket: 10,
Ratio: 0.5,
Request: 100,
},
},
)
convey.Convey("NewEleClient", t, func() {
cl = NewClient(client)
convey.So(cl, convey.ShouldNotBeNil)
})
convey.Convey("Get", t, func() {
err := cl.Get(context.TODO(), "http://api.bilibili.co/x/internal/vip/user/info", nil, nil)
convey.So(err, convey.ShouldBeNil)
})
convey.Convey("Post", t, func() {
err := cl.Post(context.TODO(), "http://api.bilibili.co/x/internal/vip/order/create", nil, nil)
convey.So(err, convey.ShouldBeNil)
})
}
func TestClientNewClient(t *testing.T) {
convey.Convey("NewClient", t, func(convCtx convey.C) {
var (
client = &bm.Client{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := NewClient(client)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestClientNewRequest(t *testing.T) {
convey.Convey("NewRequest", t, func(convCtx convey.C) {
var (
method = ""
uri = ""
params = interface{}(0)
client = &bm.Client{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := NewClient(client)
req, err := p1.NewRequest(method, uri, params)
convCtx.Convey("Then err should be nil.req should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(req, convey.ShouldNotBeNil)
})
})
})
}