initial commit
This commit is contained in:
commit
501d579a90
35 changed files with 11357 additions and 0 deletions
45
helper.go
Normal file
45
helper.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// 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())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue