-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrequest_builder_hook.go
More file actions
34 lines (28 loc) · 1.04 KB
/
request_builder_hook.go
File metadata and controls
34 lines (28 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package fastshot
import (
"net/http"
)
// BuilderHook is the interface that wraps the basic methods for setting request hooks.
var _ BuilderHook[RequestBuilder] = (*RequestHookBuilder)(nil)
// RequestHookBuilder allows for setting pre-request and post-response hooks at the request level.
type RequestHookBuilder struct {
parentBuilder *RequestBuilder
requestConfig *RequestConfigBase
}
// Hook returns a new RequestHookBuilder for setting request hooks.
func (b *RequestBuilder) Hook() *RequestHookBuilder {
return &RequestHookBuilder{
parentBuilder: b,
requestConfig: b.request.config,
}
}
// OnBeforeRequest adds a pre-request hook to the request.
func (b *RequestHookBuilder) OnBeforeRequest(hook func(*http.Request) error) *RequestBuilder {
b.requestConfig.AddBeforeRequestHook(hook)
return b.parentBuilder
}
// OnAfterResponse adds a post-response hook to the request.
func (b *RequestHookBuilder) OnAfterResponse(hook func(*http.Request, *http.Response)) *RequestBuilder {
b.requestConfig.AddAfterResponseHook(hook)
return b.parentBuilder
}