Skip to content

Commit

Permalink
Add examples (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
spyzhov authored Nov 17, 2022
1 parent ec6e3f2 commit d86c77c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
```

Expand Down
13 changes: 11 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)

func TestClient_Method(t *testing.T) {
Expand Down Expand Up @@ -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)
Expand All @@ -239,5 +248,5 @@ func ExampleNewClient() {
}()

fmt.Printf("status: %d", response.StatusCode)
// Output: status: 200
// Output: from middleware -> status: 200
}
16 changes: 16 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
52 changes: 52 additions & 0 deletions middleware/README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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}`

0 comments on commit d86c77c

Please sign in to comment.