added json body helper
This commit is contained in:
parent
553144f9fd
commit
1ccf36db0c
27
pkg/decoders/json_response.go
Normal file
27
pkg/decoders/json_response.go
Normal file
@ -0,0 +1,27 @@
|
||||
package decoders
|
||||
|
||||
// Helper for decoding json bodies from responses
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ReadJSONResponse reads a json blob from a
|
||||
// http response and decodes it into a map
|
||||
func ReadJSONResponse(res *http.Response) (map[string]interface{}, error) {
|
||||
// Read body
|
||||
defer res.Body.Close()
|
||||
data, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Parse JSON
|
||||
payload := make(map[string]interface{})
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payload, nil
|
||||
}
|
@ -79,7 +79,7 @@ func Duration(value interface{}, fallback time.Duration) time.Duration {
|
||||
// According to https://github.com/openbgpd-portable/openbgpd-openbsd/blob/master/src/usr.sbin/bgpctl/bgpctl.c#L586-L591
|
||||
// we have to parse %02lluw%01ud%02uh, %01ud%02uh%02um and %02u:%02u:%02u.
|
||||
// This yields three formats:
|
||||
// 01w03d01h
|
||||
// 01w3d01h
|
||||
// 1d02h03m
|
||||
// 01:02:03
|
||||
func DurationTimeframe(value interface{}, fallback time.Duration) time.Duration {
|
||||
|
Loading…
x
Reference in New Issue
Block a user