forked from badboyd/lura
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
27 lines (21 loc) · 1006 Bytes
/
executor.go
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
/* Package client provides some http helpers to create http clients and executors
*/
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"net/http"
)
// HTTPRequestExecutor defines the interface of the request executor for the HTTP transport protocol
type HTTPRequestExecutor func(ctx context.Context, req *http.Request) (*http.Response, error)
// DefaultHTTPRequestExecutor creates a HTTPRequestExecutor with the received HTTPClientFactory
func DefaultHTTPRequestExecutor(clientFactory HTTPClientFactory) HTTPRequestExecutor {
return func(ctx context.Context, req *http.Request) (*http.Response, error) {
return clientFactory(ctx).Do(req.WithContext(ctx))
}
}
// HTTPClientFactory creates http clients based with the received context
type HTTPClientFactory func(ctx context.Context) *http.Client
// NewHTTPClient just returns the http default client
func NewHTTPClient(ctx context.Context) *http.Client { return defaultHTTPClient }
var defaultHTTPClient = &http.Client{}