douji9734 2017-11-20 18:13
浏览 42

如何为复杂的http客户端(如gorequest)编写一个接口,该接口将返回自身以进行链接

I'm writing a package which needs to pass an instance of a *gorequest.SuperAgent to a method in a subpackage

// main.go
func main() {
  req := gorequest.New()
  result := subpackage.Method(req)
  fmt.Println(result)
}

// subpackage.go
func Method(req *gorequest.SuperAgent) string {
  req.Get("http://www.foo.com").Set("bar", "baz")
  _, body, _ := req.End()
  return body
}

I keep going in circles trying to write an interface for the gorequest superagent so I can properly isolate and test my subpackage methods with a stub of gorequest.

type Getter Interface {
  Get(url string) Getter
  // In the previous Method, Get() returns a *gorequest.SuperAgent
  // which allows chaining of methods
  // Here I tried returning the interface itself
  // But I get a 'wrong type for Get method' error when passing a gorequest instance
  // have Get(string) *gorequest.SuperAgent
  // want Get(string) Getter

  End(callback ...func(response *gorequest.Response, body string, errs []error)) (*gorequest.Response, string, []error)
  // I have no idea how to handle the param and returned *gorequest.Response here
  // Put another interface ?
  // Tried replacing it with *http.Response but not quite understanding it
}

func Method(req Getter) string {
  ...
}

So as you can see I'm tripping up on several points here and haven't been able to find a good source to learn from. Any pointers would be greatly appreciated

  • 写回答

1条回答 默认 最新

  • doubiankang2845 2017-11-20 19:47
    关注

    Besides defining the Getter interface you can also define a thin wrapper around *gorequest.SuperAgent that implements the Getter interface.

    type saGetter struct {
        sa *gorequest.SuperAgent
    }
    
    func (g *saGetter) Get(url string) Getter {
        g.sa = g.sa.Get(url)
        return g
    }
    
    func (g *saGetter) Set(param string, value string) Getter {
        g.sa = g.sa.Set(param, value)
        return g
    }
    
    func (g *saGetter) End(callback ...func(response *gorequest.Response, body string, errs []error)) (*gorequest.Response, string, []error) {
        return g.sa.End(callback...)
    }
    

    Then with your Method defined as:

    // subpackage.go
    func Method(req Getter) string {
        req.Get("http://www.foo.com").Set("bar", "baz")
        _, body, _ := req.End()
        return body
    }
    

    You can use the saGetter in main like so:

    // main.go
    func main() {
        req := gorequest.New()
        result := subpackage.Method(&saGetter{req})
        fmt.Println(result)
    }
    

    Then mocking Getter for testing the Method implementation is easy.

    That said, I agree with @JimB's comments that you probably don't need gorequest and using net/http is generally the better choice.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口