added json body helper

This commit is contained in:
Annika Hannig 2021-03-26 09:58:05 +01:00
parent 553144f9fd
commit 1ccf36db0c
No known key found for this signature in database
GPG Key ID: 62E226E47DDCE58D
2 changed files with 28 additions and 1 deletions

View 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
}

View File

@ -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 {