1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

improvements to the x/client package

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-01-23 00:47:26 +02:00
parent f633ab4b99
commit 4a1f0b6e9e
5 changed files with 157 additions and 4 deletions

View File

@@ -0,0 +1,32 @@
package client
import (
"context"
"net/http"
"sync"
)
// RequestHandler can be set to each Client instance and it should be
// responsible to handle the begin and end states of each request.
// Its BeginRequest fires right before the client talks to the server
// and its EndRequest fires right after the client receives a response from the server.
// If one of them return a non-nil error then the execution of client will stop and return that error.
type RequestHandler interface {
BeginRequest(context.Context, *http.Request) error
EndRequest(context.Context, *http.Response, error) error
}
var (
defaultRequestHandlers []RequestHandler
mu sync.Mutex
)
// RegisterRequestHandler registers one or more request handlers
// to be ran before and after of each request on all newly created Iris HTTP Clients.
// Useful for Iris HTTP Client 3rd-party libraries
// e.g. on init register a custom request-response lifecycle logging.
func RegisterRequestHandler(reqHandlers ...RequestHandler) {
mu.Lock()
defaultRequestHandlers = append(defaultRequestHandlers, reqHandlers...)
mu.Unlock()
}