Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioabreu committed Apr 17, 2024
1 parent 7075c3c commit 4f49b26
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"context"
"log"
"time"

"github.com/globocom/httpclient"
"golang.org/x/oauth2/clientcredentials"
)
Expand All @@ -22,21 +22,21 @@ func main() {
timeout := 200 * time.Millisecond
contextTimeout, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

credentials := clientcredentials.Config{
ClientID: "client_id",
ClientSecret: "client_secret",
TokenURL: "client_url/token",
Scopes: []string{"grant_permissions:client_credentials"},
}

client := httpclient.NewHTTPClient(log.Writer(),
client := httpclient.NewHTTPClient(httpclient.LoggerAdapter{Writer: log.Writer()},
httpclient.WithOAUTHTransport(credentials, timeout))

resp, err := client.NewRequest().
SetContext(contextTimeout).
Put("/authorize")

log.Printf("resp: %#v", resp)
log.Printf("err: %s", err)
}
Expand All @@ -48,32 +48,32 @@ package example
import (
"log"
"time"

"github.com/slok/goresilience/circuitbreaker"
"github.com/globocom/httpclient"
)
func main() {
cbConfig := circuitbreaker.Config {
ErrorPercentThresholdToOpen: 5,
MinimumRequestToOpen: 50,
SuccessfulRequiredOnHalfOpen: 50,
WaitDurationInOpenState: 30 * time.Second,
MetricsSlidingWindowBucketQuantity: 5,
ErrorPercentThresholdToOpen: 5,
MinimumRequestToOpen: 50,
SuccessfulRequiredOnHalfOpen: 50,
WaitDurationInOpenState: 30 * time.Second,
MetricsSlidingWindowBucketQuantity: 5,
MetricsBucketDuration: 5 * time.Second,
}

timeout := 200 * time.Millisecond
retries := 1
backoff := 5 * time.Millisecond
maxBackoff := 10 * time.Millisecond
client := httpclient.NewHTTPClient(log.Writer(),

client := httpclient.NewHTTPClient(httpclient.LoggerAdapter{Writer: log.Writer()},
httpclient.WithDefaultTransport(timeout),
httpclient.WithTimeout(timeout),
httpclient.WithCircuitBreaker(cbConfig),
httpclient.WithRetries(retries, backoff, maxBackoff),
)

resp, err := client.NewRequest().
Get("/example")

Expand All @@ -94,7 +94,7 @@ import (

func main() {
client := httpclient.NewHTTPClient(
log.Writer(),
httpclient.LoggerAdapter{Writer: log.Writer()},
httpclient.WithChainCallback(loggerCallback),
)
resp, err := client.NewRequest().Get("example.com")
Expand All @@ -104,7 +104,7 @@ func main() {

func loggerCallback(fn func() (*httpclient.Response, error)) (*httpclient.Response, error) {
resp, err := fn()

if resp != nil {
restyRequest := resp.Request().RestyRequest()
requestURL := restyRequest.RawRequest.URL
Expand All @@ -113,12 +113,12 @@ if resp != nil {
if requestHostURL := resp.Request().HostURL(); requestHostURL != nil {
host = requestHostURL.Host
}

responseTime := resp.ResponseTime().Microseconds()
log.Printf("%s [%s] %d -- %s (%dμs)", host, restyRequest.Method,
resp.StatusCode(), requestURL.String(), responseTime)
}

return resp, err
}
```
```

0 comments on commit 4f49b26

Please sign in to comment.