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,48 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["ele_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",
],
)
go_library(
name = "go_default_library",
srcs = ["ele_client.go"],
importpath = "go-common/app/service/main/vip/dao/ele-api-client",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/github.com/satori/go.uuid: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"],
)

View File

@@ -0,0 +1,130 @@
package client
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
xhttp "net/http"
"strconv"
"strings"
"time"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
pkgerr "github.com/pkg/errors"
"github.com/satori/go.uuid"
)
const (
_contentTypeJSON = "application/json"
)
//EleClient client is http client, for third ele server.
type EleClient struct {
client *bm.Client
conf *Config
}
// Config is http client conf.
type Config struct {
*App
}
// App bilibili intranet authorization.
type App struct {
Key string
Secret string
}
// NewEleClient new a http client.
func NewEleClient(c *Config, client *bm.Client) *EleClient {
cl := new(EleClient)
cl.conf = c
// check appkey
if c.Key == "" || c.Secret == "" {
panic("http client must config appkey and appsecret")
}
cl.client = client
return cl
}
// Get a json req http get.
func (cl *EleClient) Get(c context.Context, host, path string, args interface{}, res interface{}) (err error) {
req, err := cl.newRequest(xhttp.MethodGet, host, path, args)
if err != nil {
return
}
return cl.client.Do(c, req, res)
}
// Post a json req http post.
func (cl *EleClient) Post(c context.Context, host, path string, args interface{}, res interface{}) (err error) {
req, err := cl.newRequest(xhttp.MethodPost, host, path, args)
if err != nil {
return
}
return cl.client.Do(c, req, res)
}
// IsSuccess check ele api is success.
func IsSuccess(message string) bool {
return message == "ok"
}
// newRequest new http request with host, path, method, ip, values and headers, without sign.
func (cl *EleClient) newRequest(method, host, path string, args interface{}) (req *xhttp.Request, err error) {
consumerKey := cl.conf.Key
nonce := UUID4() //TODO uuid 有问题?
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
sign := eleSign(consumerKey, nonce, timestamp, path, cl.conf.Secret)
params := map[string]interface{}{}
params["consumer_key"] = consumerKey
params["nonce"] = nonce
params["timestamp"] = timestamp
params["sign"] = sign
params["args"] = args
url := host + path
marshal, err := json.Marshal(params)
if err != nil {
err = pkgerr.Wrapf(err, "marshal:%v", params)
return
}
rj := string(marshal)
log.Info("ele_client req method(%s) url(%s) rj(%s)", method, url, rj)
req, err = xhttp.NewRequest(method, url, strings.NewReader(rj))
if err != nil {
err = pkgerr.Wrapf(err, "uri:%s", url+" "+rj)
return
}
req.Header.Set("Content-Type", _contentTypeJSON)
return
}
func eleSign(consumerKey, nonce, timestamp, path, secret string) string {
var b bytes.Buffer
b.WriteString(path)
b.WriteString("&")
b.WriteString("consumer_key=")
b.WriteString(consumerKey)
b.WriteString("&nonce=")
b.WriteString(nonce)
b.WriteString("&timestamp=")
b.WriteString(timestamp)
return computeHmac256(b, secret)
}
func computeHmac256(b bytes.Buffer, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write(b.Bytes())
return hex.EncodeToString(h.Sum(nil))
}
// UUID4 is generate uuid
func UUID4() string {
return uuid.NewV4().String()
}

View File

@@ -0,0 +1,92 @@
package client
import (
"bytes"
"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 *EleClient
func TestNewEleClient(t *testing.T) {
var c = &Config{
App: &App{
Key: "sdfsdf",
Secret: "sdfsdf",
},
}
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 = NewEleClient(c, 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)
})
convey.Convey("newRequest", t, func() {
req, err := cl.newRequest("POST", "http://api.bilibili.co", "/x/internal/vip/user/info", nil)
convey.So(err, convey.ShouldBeNil)
convey.So(req, convey.ShouldNotBeNil)
})
}
func TestIsSuccess(t *testing.T) {
convey.Convey("IsSuccess", t, func() {
p1 := IsSuccess("ok")
convey.So(p1, convey.ShouldNotBeNil)
})
}
func TestEleSign(t *testing.T) {
convey.Convey("eleSign", t, func() {
p1 := eleSign("", "", "", "", "")
convey.So(p1, convey.ShouldNotBeNil)
})
}
func TestComputeHmac256(t *testing.T) {
convey.Convey("computeHmac256", t, func() {
var b bytes.Buffer
b.WriteString("http://bilibili.com/x/vip")
b.WriteString("&")
b.WriteString("consumer_key=")
p1 := computeHmac256(b, "xxx")
convey.So(p1, convey.ShouldNotBeNil)
})
}
func TestUUID4(t *testing.T) {
convey.Convey("UUID4", t, func() {
p1 := UUID4()
convey.So(p1, convey.ShouldNotBeNil)
})
}