当前位置:   article > 正文

2021-07-22学习笔记总结(grpc和client接口调用)_@grpcclient 调用grpc接口

@grpcclient 调用grpc接口

grpc接口调用
 

  1. func (d *Dao) DynamicSearch(ctx context.Context, mid, vmid int64, keyword string, pn int, ps int) (dynamicIDs []int64, total int32, err error) {
  2. start := int32((pn - 1) * ps)
  3. req := &dynamicsearchgrpc.PersonalSearchReq{
  4. Keywords: keyword,
  5. Start: start,
  6. Limit: int32(ps),
  7. Mid: mid,
  8. UpId: vmid,
  9. }
  10. reply, err := d.dynamicSearchClient.PersonalSearch(ctx, req)
  11. if err != nil {
  12. return nil, 0, err
  13. }
  14. var ids []int64
  15. for _, v := range reply.GetDynamics() {
  16. ids = append(ids, v.DynamicId)
  17. }
  18. return ids, reply.GetTotal(), nil
  19. }

dynamicSearchClient dynamicSearchgrpc.DynamicSearchServiceClient 注意初始化NewClient()防止空指针

http接口调用(透传)

发送请求调用http接口,不关心透传对象内容,只需返回结果。

注意配置中心配置
  1. func (d *Dao) DynamicDetail(ctx context.Context, mid int64, dynamicIDs []int64) (json.RawMessage, error) {
  2. params := url.Values{}
  3. params.Set("uid", strconv.FormatInt(mid, 10))
  4. for _, id := range dynamicIDs {
  5. params.Add("dynamic_ids[]", strconv.FormatInt(id, 10))
  6. }
  7. var res struct {
  8. Code int `json:"code"`
  9. Data struct {
  10. Cards json.RawMessage `json:"cards"`
  11. } `json:"data"`
  12. }
  13. if err := d.httpDynamic.Get(ctx, d.dynamicInfoURL, "", params, &res); err != nil {
  14. return nil, err
  15. }
  16. if res.Code != ecode.OK.Code() {
  17. return nil, errors.Wrap(ecode.Int(res.Code), d.dynamicInfoURL+"?"+params.Encode())
  18. }
  19. return res.Data.Cards, nil
  20. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/145951
推荐阅读
相关标签
  

闽ICP备14008679号