pve-go/helper.go
2020-06-01 16:11:25 +02:00

45 lines
739 B
Go

// Copyright (C) 2020 Marius Schellenberger
package pve
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
)
type data struct {
Data json.RawMessage `json:"data"`
Errors json.RawMessage `json:"errors"`
}
func unwrapData(resp *http.Response, v interface{}) error {
if resp.Body == nil {
return nil
}
var d data
err := json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return err
}
if d.Errors != nil {
return errors.New(string(d.Errors))
}
if v == nil {
return nil
}
return json.Unmarshal(d.Data, &v)
}
type httpbody map[string]string
func (b httpbody) Reader() io.Reader {
data := url.Values{}
for k, v := range b {
data.Set(k, v)
}
return strings.NewReader(data.Encode())
}