grpc接口调用
func (d *Dao) DynamicSearch(ctx context.Context, mid, vmid int64, keyword string, pn int, ps int) (dynamicIDs []int64, total int32, err error) {
start := int32((pn - 1) * ps)
req := &dynamicsearchgrpc.PersonalSearchReq{
Keywords: keyword,
Start: start,
Limit: int32(ps),
Mid: mid,
UpId: vmid,
}
reply, err := d.dynamicSearchClient.PersonalSearch(ctx, req)
if err != nil {
return nil, 0, err
}
var ids []int64
for _, v := range reply.GetDynamics() {
ids = append(ids, v.DynamicId)
}
return ids, reply.GetTotal(), nil
}
dynamicSearchClient dynamicSearchgrpc.DynamicSearchServiceClient 注意初始化NewClient()防止空指针
http接口调用(透传)
发送请求调用http接口,不关心透传对象内容,只需返回结果。
注意配置中心配置
func (d *Dao) DynamicDetail(ctx context.Context, mid int64, dynamicIDs []int64) (json.RawMessage, error) {
params := url.Values{}
params.Set("uid", strconv.FormatInt(mid, 10))
for _, id := range dynamicIDs {
params.Add("dynamic_ids[]", strconv.FormatInt(id, 10))
}
var res struct {
Code int `json:"code"`
Data struct {
Cards json.RawMessage `json:"cards"`
} `json:"data"`
}
if err := d.httpDynamic.Get(ctx, d.dynamicInfoURL, "", params, &res); err != nil {
return nil, err
}
if res.Code != ecode.OK.Code() {
return nil, errors.Wrap(ecode.Int(res.Code), d.dynamicInfoURL+"?"+params.Encode())
}
return res.Data.Cards, nil
}