httputil.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package httputil
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/kubecost/cost-model/pkg/util/mapper"
  9. )
  10. //--------------------------------------------------------------------------
  11. // QueryParams
  12. //--------------------------------------------------------------------------
  13. type QueryParams = mapper.PrimitiveMap
  14. // queryParamsMap is mapper.Map adapter for url.Values
  15. type queryParamsMap struct {
  16. values url.Values
  17. }
  18. // mapper.Getter implementation
  19. func (qpm *queryParamsMap) Get(key string) string {
  20. return qpm.values.Get(key)
  21. }
  22. // mapper.Setter implementation
  23. func (qpm *queryParamsMap) Set(key, value string) error {
  24. qpm.values.Set(key, value)
  25. return nil
  26. }
  27. // NewQueryParams creates a primitive map using the request query parameters
  28. func NewQueryParams(values url.Values) QueryParams {
  29. return mapper.NewMapper(&queryParamsMap{values})
  30. }
  31. //--------------------------------------------------------------------------
  32. // HTTP Context Utilities
  33. //--------------------------------------------------------------------------
  34. const (
  35. ContextWarning string = "Warning"
  36. ContextName string = "Name"
  37. ContextQuery string = "Query"
  38. )
  39. // GetWarning Extracts a warning message from the request context if it exists
  40. func GetWarning(r *http.Request) (warning string, ok bool) {
  41. warning, ok = r.Context().Value(ContextWarning).(string)
  42. return
  43. }
  44. // SetWarning Sets the warning context on the provided request and returns a new instance of the request
  45. // with the new context.
  46. func SetWarning(r *http.Request, warning string) *http.Request {
  47. ctx := context.WithValue(r.Context(), ContextWarning, warning)
  48. return r.WithContext(ctx)
  49. }
  50. // GetName Extracts a name value from the request context if it exists
  51. func GetName(r *http.Request) (name string, ok bool) {
  52. name, ok = r.Context().Value(ContextName).(string)
  53. return
  54. }
  55. // SetName Sets the name value on the provided request and returns a new instance of the request
  56. // with the new context.
  57. func SetName(r *http.Request, name string) *http.Request {
  58. ctx := context.WithValue(r.Context(), ContextName, name)
  59. return r.WithContext(ctx)
  60. }
  61. // GetQuery Extracts a query value from the request context if it exists
  62. func GetQuery(r *http.Request) (name string, ok bool) {
  63. name, ok = r.Context().Value(ContextQuery).(string)
  64. return
  65. }
  66. // SetQuery Sets the query value on the provided request and returns a new instance of the request
  67. // with the new context.
  68. func SetQuery(r *http.Request, query string) *http.Request {
  69. ctx := context.WithValue(r.Context(), ContextQuery, query)
  70. return r.WithContext(ctx)
  71. }
  72. //--------------------------------------------------------------------------
  73. // Package Funcs
  74. //--------------------------------------------------------------------------
  75. // HeaderString writes the request/response http.Header to a string.
  76. func HeaderString(h http.Header) string {
  77. var sb strings.Builder
  78. var first bool = true
  79. sb.WriteString("{ ")
  80. for k, vs := range h {
  81. if first {
  82. first = false
  83. } else {
  84. sb.WriteString(", ")
  85. }
  86. fmt.Fprintf(&sb, "%s: [ ", k)
  87. for idx, v := range vs {
  88. sb.WriteString(v)
  89. if idx != len(vs)-1 {
  90. sb.WriteString(", ")
  91. }
  92. }
  93. sb.WriteString(" ]")
  94. }
  95. sb.WriteString(" }")
  96. return sb.String()
  97. }