forked from movio/bramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
49 lines (39 loc) · 1.46 KB
/
context.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
package bramble
import (
"context"
"net/http"
)
type contextKey string
type brambleContextKey int
const permissionsContextKey brambleContextKey = 1
const requestHeaderContextKey brambleContextKey = 2
// AddPermissionsToContext adds permissions to the request context. If
// permissions are set the execution will check them against the query.
func AddPermissionsToContext(ctx context.Context, perms OperationPermissions) context.Context {
return context.WithValue(ctx, permissionsContextKey, perms)
}
// GetPermissionsFromContext returns the permissions stored in the context
func GetPermissionsFromContext(ctx context.Context) (OperationPermissions, bool) {
v := ctx.Value(permissionsContextKey)
if v == nil {
return OperationPermissions{}, false
}
if perm, ok := v.(OperationPermissions); ok {
return perm, true
}
return OperationPermissions{}, false
}
// AddOutgoingRequestsHeaderToContext adds a header to all outgoings requests for the current query
func AddOutgoingRequestsHeaderToContext(ctx context.Context, key, value string) context.Context {
h, ok := ctx.Value(requestHeaderContextKey).(http.Header)
if !ok {
h = make(http.Header)
}
h.Add(key, value)
return context.WithValue(ctx, requestHeaderContextKey, h)
}
// GetOutgoingRequestHeadersFromContext get the headers that should be added to outgoing requests
func GetOutgoingRequestHeadersFromContext(ctx context.Context) http.Header {
h, _ := ctx.Value(requestHeaderContextKey).(http.Header)
return h
}