https://github.com/tiny-go/interceptor
Simple HTTP request interceptor
https://github.com/tiny-go/interceptor
Last synced: 5 days ago
JSON representation
Simple HTTP request interceptor
- Host: GitHub
- URL: https://github.com/tiny-go/interceptor
- Owner: tiny-go
- License: mit
- Created: 2021-09-06T09:02:27.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-16T10:07:05.000Z (over 1 year ago)
- Last Synced: 2024-07-16T12:41:56.052Z (over 1 year ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# interceptor
Simple HTTP request interceptor
### The concept
- define the interceptor to use for every outgoing request
- create a request
- provide an HTTP client
- send the request with interceptor
### Examples
- retry while destination service is unavailable
```go
interceptor.New(
interceptor.Retry(serviceConfig.Client.MaxRetries),
interceptor.WithInterval(time.Duration(serviceConfig.Client.RetryInterval)),
interceptor.WhileFails(func(r *http.Response) error {
if r.StatusCode == 503 {
return errors.ServiceUnavailable("service is unavailable")
}
return nil
}),
).Then(http.DefaultClient).Do(req)
```
- throttle
```go
var (
ticker = time.NewTicker(duration)
icp = interceptor.New(interceptor.Throttle(ticker.C)).Then(http.DefaultClient)
)
icp.Do(req)
icp.Do(req)
. . .
icp.Do(req)
```