From d86c77c8ea1224a1973852d56608dab8d50c0475 Mon Sep 17 00:00:00 2001 From: Stepan Pyzhov <32341341+spyzhov@users.noreply.github.com> Date: Thu, 17 Nov 2022 23:12:00 +0100 Subject: [PATCH] Add examples (#3) --- README.md | 12 ++++++---- client_test.go | 13 +++++++++-- json_test.go | 16 ++++++++++++++ middleware/README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 03a1fde..10b47a4 100644 --- a/README.md +++ b/README.md @@ -90,12 +90,14 @@ import ( func main() { var fact struct { - Fact string `json:"fact"` + Data []struct { + Fact string `json:"fact"` + } `json:"data"` } client := chttp.NewJSON(nil) // same as chttp.NewClient(nil).JSON() _ = client.GET(context.TODO(), "https://catfact.ninja/facts?limit=1&max_length=140", nil, &fact) - fmt.Println(fact.Fact) + fmt.Println(fact.Data[0].Fact) } ``` @@ -127,7 +129,9 @@ import ( func main() { var fact struct { - Fact string `json:"fact"` + Data []struct { + Fact string `json:"fact"` + } `json:"data"` } client := chttp.NewJSON(nil) client.With(middleware.JSON(), middleware.Debug(true, nil)) @@ -139,7 +143,7 @@ func main() { }) _ = client.GET(context.TODO(), "https://catfact.ninja/facts?limit=1&max_length=140", nil, &fact) - fmt.Println(fact.Fact) + fmt.Println(fact.Data[0].Fact) } ``` diff --git a/client_test.go b/client_test.go index a0ff343..bca4f93 100644 --- a/client_test.go +++ b/client_test.go @@ -6,10 +6,12 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/http/httptest" "reflect" "testing" + "time" ) func TestClient_Method(t *testing.T) { @@ -229,7 +231,14 @@ func equalResponses(t *testing.T, actual *http.Response, wantBody []byte, status } func ExampleNewClient() { - client := NewClient(nil) + client := NewClient(&http.Client{Timeout: 30 * time.Second}) + client.With(func(request *http.Request, next func(request *http.Request) (*http.Response, error)) (*http.Response, error) { + log.Printf("before request: %s %s", request.Method, request.URL.String()) + response, err := next(request) + log.Printf("after request: %s %s", request.Method, request.URL.String()) + fmt.Print("from middleware -> ") + return response, err + }) response, err := client.HEAD(context.TODO(), "https://go.dev/") if err != nil { panic(err) @@ -239,5 +248,5 @@ func ExampleNewClient() { }() fmt.Printf("status: %d", response.StatusCode) - // Output: status: 200 + // Output: from middleware -> status: 200 } diff --git a/json_test.go b/json_test.go index 00de3d0..556cdde 100644 --- a/json_test.go +++ b/json_test.go @@ -196,3 +196,19 @@ func TestJSONClient_Request_status(t *testing.T) { t.Errorf("Request() wrong error: %q", err.Error()) } } + +func ExampleJSONClient_GET() { + var fact struct { + Data []struct { + Fact string `json:"fact"` + } `json:"data"` + } + client := NewJSON(nil) + client.With(func(request *http.Request, next func(request *http.Request) (*http.Response, error)) (*http.Response, error) { + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Accept", "application/json") + return next(request) + }) + _ = client.GET(context.TODO(), "https://catfact.ninja/facts?limit=1&max_length=140", nil, &fact) + fmt.Println(fact.Data[0].Fact) +} diff --git a/middleware/README.md b/middleware/README.md index b6f8429..13c5dcf 100644 --- a/middleware/README.md +++ b/middleware/README.md @@ -1,5 +1,23 @@ # Middlewares +Middleware can be created with the defining function: + +```go +func(request *http.Request, next func(request *http.Request) (*http.Response, error)) (*http.Response, error) +``` + +**Example:** + +```go +client := chttp.NewClient(nil) +client.With(func(request *http.Request, next func(request *http.Request) (*http.Response, error)) (*http.Response, error) { + // before action + response, err := next(request) + // before action + return response, err +}) +``` + ## CustomHeaders Adds a custom headers based on the request. @@ -55,6 +73,22 @@ client := chttp.NewClient(nil) client.With(middleware.JSON()) ``` +## OpenTracing + +Adds an OpenTracing logs and headers to the request. + +Source: [https://github.com/spyzhov/chttp-middleware-opentracing](https://github.com/spyzhov/chttp-middleware-opentracing) + +```go +import ( + // ... + middleware "github.com/spyzhov/chttp-middleware-opentracing" +) + +client := chttp.NewClient(nil) +client.With(middleware.Opentracing()) +``` + ## Trace Adds short logs on each request. @@ -65,3 +99,21 @@ Adds short logs on each request. client := chttp.NewClient(nil) client.With(middleware.Trace(nil)) ``` + +# TBD + + - [ ] `Cache(interface{Get(string,interface{}), Set(string,interface{})}, GetKey func(*http.Request) string)` + + Client-wide caching layer. + + If `GetKey` return a blank string, then do not cache. + - [ ] `Retry(GetCount func(*http.Request) int, BeforeRetry func (*http.Request, int) string)` + + Automatically send a retry request in case of failure. + + `GetCount` - returns the max retry amount. + + `BeforeRetry` - should be called before retry. + + **TODO**: think about a structure as argument + `struct {GetCount func(*http.Request) int, BeforeRetry func (*http.Request, int) string}`