Skip to content

nozzle/fetcher

Repository files navigation

fetcher CircleCI GoDoc Codecov Go Report Card

HTTP Client - Simplified - Mockable

  1. Create your client
  • A Client is required to make any http calls - good thing it's dead easy to create:
  • Simple Client example:
  c := context.Background()
  cl, err := github.NewClient(c)
  • Advanced Client example:
  c := context.Background()
  cl, err := fetcher.NewClient(c,
    fetcher.WithRequestOptions([]fetcher.RequestOption{
      fetcher.WithAcceptJSONHeader(),
      fetcher.WithHeader("API-Token", os.Getenv("API_TOKEN")),
    }),
  )
  • This client can now be used as much as needed.
  1. Pass your client to the function as a fetcher.Fetcher interface object:
  func sharedCount(c context.Context, f fetcher.Fetcher, uri string) (int, error) {
	
    ...

    return countResp.Count, nil
  }
  1. Use your client to make a call:
func sharedCount(c context.Context, f fetcher.Fetcher, uri string) (int, error) {
  apiURL := "http://www.linkedin.com/countserv/count/share?format=json&url=" + url.QueryEscape(uri)
  resp, err := f.Get(c, apiURL, fetcher.WithMaxAttempts(3))
  if err != nil {
    return 0, err
  }
  1. Handle the response
  defer resp.Close()

  switch {
  case resp.StatusCode() == 400:
    return 0, errors.New("invalid url")
  case resp.StatusCode() > 300:
    return 0, errors.New("bad status code")
  }

  type countResponse struct {
    Count int    `json:"count"`
    URL   string `json:"url"`
  }

  countResp := &countResponse{}
  if err = resp.Decode(c, countResp, fetcher.WithJSONBody()); err != nil {
    return 0, err
  }

  return countResp.Count, nil`
}
  1. Write your test
  • you can now use fetchermock to create a testing client that satisfies the fetcher.Fetcher interface - including the expected response body, status code, and/or error.

Advanced features:

  1. Retry loop
  2. Copied response body for easier debugging
  3. Rate Limiting
  4. Max Idle Connections Per Host
  5. Custom Debug/Error Logging
  6. Request Backoff Options

About

HTTP Client - Simplified - Mockable

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages