Преглед изворни кода

Add ContextWarning to the util package in cost-model

Matt Bolt пре 5 година
родитељ
комит
f6cd3167c0
1 измењених фајлова са 22 додато и 0 уклоњено
  1. 22 0
      pkg/util/http.go

+ 22 - 0
pkg/util/http.go

@@ -1,6 +1,7 @@
 package util
 
 import (
+	"context"
 	"fmt"
 	"net/http"
 	"net/url"
@@ -36,6 +37,27 @@ func NewQueryParams(values url.Values) QueryParams {
 	return mapper.NewMapper(&queryParamsMap{values})
 }
 
+//--------------------------------------------------------------------------
+//  HTTP Context Utilities
+//--------------------------------------------------------------------------
+
+const (
+	ContextWarning string = "Warning"
+)
+
+// GetWarning Extracts a warning message from the request context if it exists
+func GetWarning(r *http.Request) (warning string, ok bool) {
+	warning, ok = r.Context().Value(ContextWarning).(string)
+	return
+}
+
+// SetWarning Sets the warning context on the provided request and returns a new instance of the request
+// with the new context.
+func SetWarning(r *http.Request, warning string) *http.Request {
+	ctx := context.WithValue(r.Context(), ContextWarning, warning)
+	return r.WithContext(ctx)
+}
+
 //--------------------------------------------------------------------------
 //  Package Funcs
 //--------------------------------------------------------------------------