-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathmiddleware.go
56 lines (48 loc) · 1.63 KB
/
middleware.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package common
import (
"log"
"net/http"
"net/http/httputil"
"github.com/hashicorp/go-azure-sdk/sdk/client"
)
func correlationRequestIDMiddleware(id string) client.RequestMiddleware {
return func(request *http.Request) (*http.Request, error) {
// ensure the `X-Correlation-ID` field is set
request.Header.Add(HeaderCorrelationRequestID, id)
return request, nil
}
}
func requestLoggerMiddleware(providerName string) client.RequestMiddleware {
return func(request *http.Request) (*http.Request, error) {
// strip the authorization header prior to printing
authHeaderName := "Authorization"
auth := request.Header.Get(authHeaderName)
if auth != "" {
request.Header.Del(authHeaderName)
}
// dump request to wire format
if dump, err := httputil.DumpRequestOut(request, true); err == nil {
log.Printf("[DEBUG] %s Request: \n%s\n", providerName, dump)
} else {
// fallback to basic message
log.Printf("[DEBUG] %s Request: %s to %s\n", providerName, request.Method, request.URL)
}
// add the auth header back
if auth != "" {
request.Header.Add(authHeaderName, auth)
}
return request, nil
}
}
func responseLoggerMiddleware(providerName string) client.ResponseMiddleware {
return func(request *http.Request, response *http.Response) (*http.Response, error) {
// dump response to wire format
if dump, err2 := httputil.DumpResponse(response, true); err2 == nil {
log.Printf("[DEBUG] %s Response for %s: \n%s\n", providerName, request.URL, dump)
} else {
// fallback to basic message
log.Printf("[DEBUG] %s Response: %s for %s\n", providerName, response.Status, request.URL)
}
return response, nil
}
}