How to write an interface for a complex http client like gorequest with methods that returns itself for chaining

372 views Asked by At

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

There are 1 answers

0
mkopriva On

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.