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,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["relation.go"],
importpath = "go-common/app/interface/main/account/dao/relation",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/account/conf:go_default_library",
"//app/interface/main/account/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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 = ["relation_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/account/conf:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,112 @@
package relation
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/account/conf"
"go-common/app/interface/main/account/model"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
// Dao is
type Dao struct {
conf *conf.Config
httpClient *bm.Client
recommendURL string
}
const (
_recommendURI = "/main/recommend"
)
// New is
func New(conf *conf.Config) *Dao {
return &Dao{
conf: conf,
httpClient: bm.NewClient(conf.HTTPClient.Normal),
recommendURL: conf.Host.Search + _recommendURI,
}
}
func paltform(device *bm.Device) string {
if device.RawMobiApp == "" {
return "web"
}
if device.IsAndroid() {
return "android"
}
if device.IsIOS() {
return "ios"
}
return "web"
}
func buvid(device *bm.Device) string {
if device.RawMobiApp == "" {
return device.Buvid3
}
return device.Buvid
}
// Recommend is
func (d *Dao) Recommend(ctx context.Context, mid int64, serviceArea string, mainTids string, subTids string, device *bm.Device, pagesize int64, ip string) (*model.RecommendResponse, error) {
params := url.Values{}
params.Set("platform", paltform(device))
params.Set("mobi_app", device.MobiApp())
params.Set("device", device.Device)
params.Set("build", strconv.FormatInt(device.Build, 10))
params.Set("clientip", ip)
params.Set("userid", strconv.FormatInt(mid, 10))
params.Set("buvid", buvid(device))
params.Set("pagesize", strconv.FormatInt(pagesize, 10))
params.Set("rec_type", "up_rec")
params.Set("service_area", serviceArea)
if mainTids != "" {
params.Set("main_tids", mainTids)
}
if subTids != "" {
params.Set("sub_tids", subTids)
}
resp := &model.RecommendResponse{}
if err := d.httpClient.Get(ctx, d.recommendURL, ip, params, resp); err != nil {
return nil, err
}
if resp.Code != 0 {
log.Error("Failed to call recommendation service with error code: %d, params: %+v, trackid(%s)", resp.Code, params, resp.TrackID)
return nil, ecode.Int(int(resp.Code))
}
return resp, nil
}
// TagSuggestRecommend is
func (d *Dao) TagSuggestRecommend(ctx context.Context, mid int64, contextID string, tagname string, device *bm.Device, pagesize int64, ip string) (*model.TagSuggestRecommendResponse, error) {
params := url.Values{}
params.Set("platform", paltform(device))
params.Set("mobi_app", device.MobiApp())
params.Set("device", device.Device)
params.Set("build", strconv.FormatInt(device.Build, 10))
params.Set("clientip", ip)
params.Set("userid", strconv.FormatInt(mid, 10))
params.Set("buvid", buvid(device))
params.Set("pagesize", strconv.FormatInt(pagesize, 10))
params.Set("rec_type", "tagup_rec")
params.Set("service_area", "tag_suggest")
params.Set("context_id", contextID)
if tagname != "" {
params.Set("tagname", tagname)
}
resp := &model.TagSuggestRecommendResponse{}
if err := d.httpClient.Get(ctx, d.recommendURL, ip, params, resp); err != nil {
return nil, err
}
if resp.Code != 0 {
log.Error("Failed to call recommendation service with error code: %d, params: %+v, trackid(%s)", resp.Code, params, resp.TrackID)
return nil, ecode.Int(int(resp.Code))
}
return resp, nil
}

View File

@@ -0,0 +1,117 @@
package relation
import (
"context"
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/account/conf"
bm "go-common/library/net/http/blademaster"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.account-interface")
flag.Set("conf_token", "967eef77ad40b478234f11b0d489d6d6")
flag.Set("tree_id", "3815")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/account-interface-example.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.httpClient.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}
func TestRelationpaltform(t *testing.T) {
var (
device = &bm.Device{}
)
convey.Convey("paltform", t, func(ctx convey.C) {
p1 := paltform(device)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestRelationbuvid(t *testing.T) {
var (
device = &bm.Device{}
)
convey.Convey("buvid", t, func(ctx convey.C) {
p1 := buvid(device)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestRelationRecommend(t *testing.T) {
var (
ctx = context.Background()
mid = int64(0)
serviceArea = ""
mainTids = ""
subTids = ""
device = &bm.Device{}
pagesize = int64(0)
ip = ""
)
convey.Convey("Recommend", t, func(c convey.C) {
httpMock("GET", d.recommendURL).Reply(200).JSON(`{"code":0,"message":"0","data":[]}`)
defer gock.OffAll()
p1, err := d.Recommend(ctx, mid, serviceArea, mainTids, subTids, device, pagesize, ip)
c.Convey("Then err should be nil.p1 should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(p1, convey.ShouldNotBeNil)
})
})
}
func TestRelationTagSuggestRecommend(t *testing.T) {
var (
ctx = context.Background()
mid = int64(0)
contextID = ""
tagname = ""
device = &bm.Device{}
pagesize = int64(0)
ip = ""
)
convey.Convey("TagSuggestRecommend", t, func(c convey.C) {
httpMock("GET", d.recommendURL).Reply(200).JSON(`{"code":0,"message":"0","data":[]}`)
defer gock.OffAll()
p1, err := d.TagSuggestRecommend(ctx, mid, contextID, tagname, device, pagesize, ip)
c.Convey("Then err should be nil.p1 should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(p1, convey.ShouldNotBeNil)
})
})
}