-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.go
86 lines (68 loc) · 2.42 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package httputil
import (
"context"
"encoding/base64"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/julienschmidt/httprouter"
)
type timeCtxKey string
// ReqTimeCtxKey is the Context key to retrieve the request time.
const ReqTimeCtxKey = timeCtxKey("request_time")
// AddBasicAuth decorates the provided http.Request with Basic Authorization.
func AddBasicAuth(apiKey, apiSecret string, r *http.Request) {
r.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(apiKey+":"+apiSecret)))
}
// PathParam returns the value from the named path segment.
func PathParam(r *http.Request, name string) string {
v := httprouter.ParamsFromContext(r.Context()).ByName(name)
return strings.TrimLeft(v, "/")
}
// HeaderOrDefault returns the value of an HTTP header or a default value.
func HeaderOrDefault(r *http.Request, key string, defaultValue string) string {
v := r.Header.Get(key)
if v != "" {
return v
}
return defaultValue
}
// QueryStringOrDefault returns the string value of the specified URL query parameter or a default value.
func QueryStringOrDefault(q url.Values, key string, defaultValue string) string {
v := q.Get(key)
if v != "" {
return v
}
return defaultValue
}
// QueryIntOrDefault returns the integer value of the specified URL query parameter or a default value.
func QueryIntOrDefault(q url.Values, key string, defaultValue int) int {
if v, err := strconv.ParseInt(q.Get(key), 10, 64); err == nil && v >= math.MinInt && v <= math.MaxInt {
return int(v)
}
return defaultValue
}
// QueryUintOrDefault returns the unsigned integer value of the specified URL query parameter or a default value.
func QueryUintOrDefault(q url.Values, key string, defaultValue uint) uint {
if v, err := strconv.ParseUint(q.Get(key), 10, 64); err == nil && v <= math.MaxUint {
return uint(v)
}
return defaultValue
}
// WithRequestTime returns a new context with the added request time.
func WithRequestTime(ctx context.Context, t time.Time) context.Context {
return context.WithValue(ctx, ReqTimeCtxKey, t)
}
// GetRequestTimeFromContext returns the request time from the context.
func GetRequestTimeFromContext(ctx context.Context) (time.Time, bool) {
v := ctx.Value(ReqTimeCtxKey)
t, ok := v.(time.Time)
return t, ok
}
// GetRequestTime returns the request time from the http request.
func GetRequestTime(r *http.Request) (time.Time, bool) {
return GetRequestTimeFromContext(r.Context())
}