-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
44 lines (37 loc) · 1023 Bytes
/
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
package httpext
import (
"encoding/json"
"io/ioutil"
"reflect"
"github.com/go-logr/logr"
"github.com/pkg/errors"
)
// ReadBodyAsJSON will try to read the body as JSON into the provided object
func (d *Request) ReadBodyAsJSON(obj interface{}) error {
bytes, err := d.ReadBody()
if err != nil {
return errors.Wrap(err, "unable to read body")
}
if len(bytes) == 0 {
return errors.New("body is empty")
}
if err := json.Unmarshal(bytes, obj); err != nil {
return errors.Wrapf(err, "could not unmarshal body to: %v", reflect.TypeOf(obj))
}
return nil
}
// ReadBody will read the whole body, close it and return the contents
func (d *Request) ReadBody() ([]byte, error) {
defer func() {
if err := d.Body.Close(); err != nil {
d.logger.Error(err, "could not close body")
}
}()
bytes, err := ioutil.ReadAll(d.Body)
err = errors.Wrap(err, "could not read request body")
return bytes, err
}
// Logger returns the logger for the request
func (d *Request) Logger() logr.Logger {
return d.logger
}