breaking change: modify API for working with response metadata (#213)
* implement functions to append/get metadata and set/get status code * сhanged behavior to return nil instead of empty metadata for getResponseMetadata() * сhanged work with HTTP headers to use direct array assignment instead of for-range * fix linters * fix meter handler * fix uninitialized response metadata for incoming context * removed a useless test * metrics handler has been fixed to work with compressed data
This commit is contained in:
29
response.go
Normal file
29
response.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type (
|
||||
rspStatusCodeKey struct{}
|
||||
rspStatusCodeVal struct {
|
||||
code int
|
||||
}
|
||||
)
|
||||
|
||||
// SetResponseStatusCode sets the status code in the context.
|
||||
func SetResponseStatusCode(ctx context.Context, code int) {
|
||||
if rsp, ok := ctx.Value(rspStatusCodeKey{}).(*rspStatusCodeVal); ok {
|
||||
rsp.code = code
|
||||
}
|
||||
}
|
||||
|
||||
// GetResponseStatusCode retrieves the response status code from the context.
|
||||
func GetResponseStatusCode(ctx context.Context) int {
|
||||
code := http.StatusOK
|
||||
if rsp, ok := ctx.Value(rspStatusCodeKey{}).(*rspStatusCodeVal); ok {
|
||||
code = rsp.code
|
||||
}
|
||||
return code
|
||||
}
|
Reference in New Issue
Block a user