-
Notifications
You must be signed in to change notification settings - Fork 152
/
request_tag.go
35 lines (29 loc) · 931 Bytes
/
request_tag.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
package customhttpclient
import "net/http"
// RequestTag is used to tag requests we trigger from WunderGraph
// and avoid circular dependencies
type RequestTag string
const (
headerName = "X-Wg-Request-Tag"
)
const (
// RequestTagUserInfo indicates the request is fetching
// the userInfo endpoint for token based authentication
RequestTagUserInfo RequestTag = "userInfo"
// RequestTagNone indicates the request had no valid tag
RequestTagNone RequestTag = ""
)
// Tag retrieves the tag for an incoming request. If the header missing or
// present but with an unknown value, it returns RequestTagNone.
func Tag(r *http.Request) RequestTag {
value := r.Header.Get(headerName)
switch t := RequestTag(value); t {
case RequestTagUserInfo:
return t
}
return RequestTagNone
}
// SetTag tags an outgoing request with the given tag
func SetTag(r *http.Request, tag RequestTag) {
r.Header.Add(headerName, string(tag))
}