2021-07-22学习笔记总结(grpc和client接口调用)
Posted 程序彤
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-07-22学习笔记总结(grpc和client接口调用)相关的知识,希望对你有一定的参考价值。
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
}
以上是关于2021-07-22学习笔记总结(grpc和client接口调用)的主要内容,如果未能解决你的问题,请参考以下文章