Documentation ¶
Index ¶
- Constants
- func HTTPRequestHeaders(ctx context.Context) (http.Header, bool)
- func MethodName(ctx context.Context) (string, bool)
- func PackageName(ctx context.Context) (string, bool)
- func ServiceName(ctx context.Context) (string, bool)
- func SetHTTPResponseHeader(ctx context.Context, key, value string) error
- func StatusCode(ctx context.Context) (string, bool)
- func WithHTTPRequestHeaders(ctx context.Context, h http.Header) (context.Context, error)
- func WithMethodName(ctx context.Context, name string) context.Context
- func WithPackageName(ctx context.Context, name string) context.Context
- func WithResponseWriter(ctx context.Context, w http.ResponseWriter) context.Context
- func WithServiceName(ctx context.Context, name string) context.Context
- func WithStatusCode(ctx context.Context, code int) context.Context
Constants ¶
const ( MethodNameKey contextKey = 1 + iota ServiceNameKey PackageNameKey StatusCodeKey RequestHeaderKey ResponseWriterKey )
Variables ¶
This section is empty.
Functions ¶
func MethodName ¶
MethodName extracts the name of the method being handled in the given context. If it is not known, it returns ("", false).
func PackageName ¶
PackageName extracts the fully-qualified protobuf package name of the service handling the given context. If it is not known, it returns ("", false). If the service comes from a proto file that does not declare a package name, it returns ("", true).
Note that the protobuf package name can be very different than the go package name; the two are unrelated.
func ServiceName ¶
ServiceName extracts the name of the service handling the given context. If it is not known, it returns ("", false).
func SetHTTPResponseHeader ¶
SetHTTPResponseHeader sets an HTTP header key-value pair using a context provided by a generated server, or a child of that context. The server will include the header in its response for that request context.
This can be used to respond with custom HTTP headers like "Cache-Control". But note that HTTP headers are a implementation detail, only visible by middleware, not by the clients or their responses.
The header will be ignored (noop) if the context is invalid (i.e. using a new context.Background() instead of passing the context from the handler).
If called multiple times with the same key, it replaces any existing values associated with that key.
SetHTTPResponseHeader returns an error if the provided header key would overwrite a header that is needed by xservice, like "Content-Type".
func StatusCode ¶
StatusCode retrieves the status code of the response (as string like "200"). If it is known returns (status, true). If it is not known, it returns ("", false).
func WithHTTPRequestHeaders ¶
WithHTTPRequestHeaders stores an http.Header in a context.Context. When using a generated client, you can pass the returned context into any of the request methods, and the stored header will be included in outbound HTTP requests.
This can be used to set custom HTTP headers like authorization tokens or client IDs. But note that HTTP headers are a implementation detail, only visible by middleware, not by the server implementation.
WithHTTPRequestHeaders returns an error if the provided http.Header would overwrite a header that is needed by, like "Content-Type".
func WithResponseWriter ¶
Types ¶
This section is empty.