37 lines
752 B
Go
37 lines
752 B
Go
package servcli
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
type MiddlewareFunc func(next http.RoundTripper) http.RoundTripper
|
|
|
|
type Client struct {
|
|
httpClient *http.Client
|
|
userAgent string
|
|
}
|
|
|
|
func NewClient(transport http.RoundTripper, mwFuncs ...MiddlewareFunc) *Client {
|
|
if transport == nil {
|
|
transport = NewDefaultTransport()
|
|
}
|
|
for _, mwFunc := range mwFuncs {
|
|
transport = mwFunc(transport)
|
|
}
|
|
httpClient := &http.Client{
|
|
Transport: transport,
|
|
}
|
|
return &Client{
|
|
httpClient: httpClient,
|
|
userAgent: "proxyClient/1.0",
|
|
}
|
|
}
|
|
|
|
func (cli *Client) SetTransport(transport http.RoundTripper) {
|
|
cli.httpClient.Transport = transport
|
|
}
|
|
|
|
func (cli *Client) UseMiddleware(mwFunc MiddlewareFunc) {
|
|
cli.httpClient.Transport = mwFunc(cli.httpClient.Transport)
|
|
}
|