Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add headers for requesting and receiving the server version and other…
… debug information
- Loading branch information
1 parent
0bc3020
commit 03c6df6
Showing
2 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package requestmeta | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
// RequestMetadataHeaderKey defines a key in the request metadata header. | ||
type RequestMetadataHeaderKey string | ||
|
||
// BoolRequestMetadataHeaderKey defines a key for a boolean vlaue in the request metadata header. | ||
type BoolRequestMetadataHeaderKey RequestMetadataHeaderKey | ||
|
||
const ( | ||
// RequestServerVersion, if specified in a request header, asks SpiceDB to return its | ||
// server version in the response header (if supported). | ||
// Value: `1` | ||
RequestServerVersion BoolRequestMetadataHeaderKey = "io.spicedb.requestversion" | ||
|
||
// RequestDebugInformation, if specified in a request header, asks SpiceDB to return fully debug | ||
// information. | ||
// Value: the special key to retrieve debug information | ||
RequestDebugInformation RequestMetadataHeaderKey = "io.spicedb.requestdebuginfo" | ||
) | ||
|
||
// AddRequestHeaders returns a new context with the given values as request headers. | ||
func AddRequestHeaders(ctx context.Context, keys ...BoolRequestMetadataHeaderKey) context.Context { | ||
values := make(map[RequestMetadataHeaderKey]string, len(keys)) | ||
for _, key := range keys { | ||
values[RequestMetadataHeaderKey(key)] = "1" | ||
} | ||
return SetRequestHeaders(ctx, values) | ||
} | ||
|
||
// SetRequestHeaders returns a new context with the given values as request headers. | ||
func SetRequestHeaders(ctx context.Context, values map[RequestMetadataHeaderKey]string) context.Context { | ||
pairs := make([]string, 0, len(values)*2) | ||
for key, value := range values { | ||
pairs = append(pairs, string(key)) | ||
pairs = append(pairs, value) | ||
} | ||
return metadata.NewOutgoingContext(ctx, metadata.Pairs(pairs...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters