-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.go
52 lines (39 loc) · 956 Bytes
/
query.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
package stuber
import (
"encoding/json"
"net/http"
"github.com/bavix/features"
"github.com/google/uuid"
)
const (
RequestInternalFlag features.Flag = iota
)
type Query struct {
ID *uuid.UUID `json:"id,omitempty"`
Service string `json:"service"`
Method string `json:"method"`
Headers map[string]any `json:"headers"`
Data map[string]any `json:"data"`
toggles features.Toggles
}
func toggles(r *http.Request) features.Toggles {
var flags []features.Flag
if len(r.Header.Values("X-Gripmock-Requestinternal")) > 0 {
flags = append(flags, RequestInternalFlag)
}
return features.New(flags...)
}
func NewQuery(r *http.Request) (Query, error) {
q := Query{
toggles: toggles(r),
}
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()
if err := decoder.Decode(&q); err != nil {
return q, err
}
return q, nil
}
func (q Query) RequestInternal() bool {
return q.toggles.Has(RequestInternalFlag)
}