项目作者: nmrshll

项目描述 :
Add middleware to your HTTP clients in Go
高级语言: Go
项目地址: git://github.com/nmrshll/go-httpclient-middl.git
创建时间: 2018-09-22T11:26:49Z
项目社区:https://github.com/nmrshll/go-httpclient-middl

开源协议:MIT License

下载


Build Status
Go Report Card

go-httpclient-middl

Add middleware to your HTTP clients in Go

Why ?

If you need an HTTP client that automatically logs requests, tracks metrics, joins a token with each request, validates HTTP status codes, or does any other custom action for every request/reponse, this library might be for you.

How ?

Download the library using go get -u github.com/nmrshll/go-httpclient-middl

Then use it this way:

  1. func main() {
  2. httpClient := http.Client{Timeout: 30 * time.Second}
  3. client, err := middl.NewClient(&httpClient)
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. // add middleware to you client (classic examples provided in this library or custom)
  8. client.UseMiddleware(logger.New())
  9. client.UseMiddleware(statusvalidator.New())
  10. // then do your requests as usual
  11. resp, err := client.Get("https://google.com")
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. if resp == nil {
  16. log.Fatalf("no response from server")
  17. } // else
  18. defer resp.Body.Close()
  19. // do something with the response here
  20. }