@@ -48,9 +48,8 @@ import (
48
48
var clientVersion = fmt .Sprintf ("%s" , metadata .Version )
49
49
50
50
var userAgent = "weep/" + clientVersion + " Go-http-client/1.1"
51
-
52
- type Account struct {
53
- }
51
+ var clientFactoryOverride ClientFactory
52
+ var preflightFunctions = make ([]RequestPreflight , 0 )
54
53
55
54
// HTTPClient is the interface we expect HTTP clients to implement.
56
55
type HTTPClient interface {
@@ -67,13 +66,39 @@ type Client struct {
67
66
Region string
68
67
}
69
68
69
+ type ClientFactory func () (* http.Client , error )
70
+
71
+ // RegisterClientFactory overrides Weep's standard config-based ConsoleMe client
72
+ // creation with a ClientFactory. This function will be called during the creation
73
+ // of all ConsoleMe clients.
74
+ func RegisterClientFactory (factory ClientFactory ) {
75
+ clientFactoryOverride = factory
76
+ }
77
+
78
+ type RequestPreflight func (req * http.Request ) error
79
+
80
+ // RegisterRequestPreflight adds a RequestPreflight function which will be called in the
81
+ // order of registration during the creation of a ConsoleMe request.
82
+ func RegisterRequestPreflight (preflight RequestPreflight ) {
83
+ preflightFunctions = append (preflightFunctions , preflight )
84
+ }
85
+
70
86
// GetClient creates an authenticated ConsoleMe client
71
87
func GetClient (region string ) (* Client , error ) {
72
88
var client * Client
73
89
consoleMeUrl := viper .GetString ("consoleme_url" )
74
90
authenticationMethod := viper .GetString ("authentication_method" )
75
91
76
- if authenticationMethod == "mtls" {
92
+ if clientFactoryOverride != nil {
93
+ customClient , err := clientFactoryOverride ()
94
+ if err != nil {
95
+ return client , err
96
+ }
97
+ client , err = NewClient (consoleMeUrl , "" , customClient )
98
+ if err != nil {
99
+ return client , err
100
+ }
101
+ } else if authenticationMethod == "mtls" {
77
102
mtlsClient , err := mtls .NewHTTPClient ()
78
103
if err != nil {
79
104
return client , err
@@ -122,6 +147,18 @@ func NewClient(hostname string, region string, httpc *http.Client) (*Client, err
122
147
return c , nil
123
148
}
124
149
150
+ func runPreflightFunctions (req * http.Request ) error {
151
+ var err error
152
+ if preflightFunctions != nil {
153
+ for _ , preflight := range preflightFunctions {
154
+ if err = preflight (req ); err != nil {
155
+ return err
156
+ }
157
+ }
158
+ }
159
+ return nil
160
+ }
161
+
125
162
func (c * Client ) buildRequest (method string , resource string , body io.Reader , apiPrefix string ) (* http.Request , error ) {
126
163
urlStr := c .Host + apiPrefix + resource
127
164
req , err := http .NewRequest (method , urlStr , body )
@@ -130,6 +167,10 @@ func (c *Client) buildRequest(method string, resource string, body io.Reader, ap
130
167
}
131
168
req .Header .Set ("User-Agent" , userAgent )
132
169
req .Header .Add ("Content-Type" , "application/json" )
170
+ err = runPreflightFunctions (req )
171
+ if err != nil {
172
+ return nil , err
173
+ }
133
174
134
175
return req , nil
135
176
}
0 commit comments