|
|
|
@ -7,15 +7,18 @@ import (
|
|
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// StatusCreated HTTP 201
|
|
|
|
|
const StatusCreated = 201
|
|
|
|
|
|
|
|
|
|
const contentTypeJson = "application/json"
|
|
|
|
|
const contentTypeJSON = "application/json"
|
|
|
|
|
|
|
|
|
|
// Client an http client with built-in JSON (de)serialization
|
|
|
|
|
type Client struct {
|
|
|
|
|
client http.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) JsonGet(url string, response interface{}) (*http.Response, error) {
|
|
|
|
|
// JSONGet perform a GET request and serialize response body into given interface if any
|
|
|
|
|
func (c *Client) JSONGet(url string, response interface{}) (*http.Response, error) {
|
|
|
|
|
logrus.Tracef("GET %s", url)
|
|
|
|
|
|
|
|
|
|
r, err := c.client.Get(url)
|
|
|
|
@ -30,19 +33,20 @@ func (c *Client) JsonGet(url string, response interface{}) (*http.Response, erro
|
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) JsonPost(url string, body, response interface{}) (*http.Response, error) {
|
|
|
|
|
// JSONPost perform a POST request and serialize request/response body into given interface if any
|
|
|
|
|
func (c *Client) JSONPost(url string, request, response interface{}) (*http.Response, error) {
|
|
|
|
|
logrus.Tracef("POST %s", url)
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
var b []byte
|
|
|
|
|
if body != nil {
|
|
|
|
|
b, err = json.Marshal(body)
|
|
|
|
|
if request != nil {
|
|
|
|
|
b, err = json.Marshal(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := c.client.Post(url, contentTypeJson, bytes.NewBuffer(b))
|
|
|
|
|
r, err := c.client.Post(url, contentTypeJSON, bytes.NewBuffer(b))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|