Add Session.RequestJSON(), Session.GetJSON() and Session.PostJSON()

Change-Id: Iba573c8bb1cdfdd42b1c51dd997343937eb29a1b
This commit is contained in:
Dean Troyer
2016-05-05 09:10:02 -05:00
parent 11e0970660
commit e7fceb0b4d

View File

@@ -18,12 +18,16 @@ package openstack
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"encoding/json"
"errors"
"io" "io"
// "io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"git.openstack.org/openstack/golang-client.git/util"
) )
var Debug = new(bool) var Debug = new(bool)
@@ -130,24 +134,66 @@ func (s *Session) Request(
return resp, nil return resp, nil
} }
// Perform a simple get to an endpoint and unmarshall returned JSON
func (s *Session) RequestJSON(
method string,
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
responseContainer interface{},
) (resp *http.Response, err error) {
resp, err = s.Request(method, url, params, headers, body)
if err != nil {
return nil, err
}
err = util.CheckHTTPResponseStatusCode(resp)
if err != nil {
return nil, err
}
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("error reading response body")
}
if err = json.Unmarshal(rbody, &responseContainer); err != nil {
return nil, err
}
return resp, nil
}
func (s *Session) Delete( func (s *Session) Delete(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header) (resp *http.Response, err error) { headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("DELETE", url, params, headers, nil) return s.Request("DELETE", url, params, headers, nil)
} }
func (s *Session) Get( func (s *Session) Get(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header) (resp *http.Response, err error) { headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("GET", url, params, headers, nil) return s.Request("GET", url, params, headers, nil)
} }
func (s *Session) GetJSON(
url string,
params *url.Values,
headers *http.Header,
responseContainer interface{},
) (resp *http.Response, err error) {
return s.RequestJSON("GET", url, params, headers, nil, responseContainer)
}
func (s *Session) Head( func (s *Session) Head(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header) (resp *http.Response, err error) { headers *http.Header,
) (resp *http.Response, err error) {
return s.Request("HEAD", url, params, headers, nil) return s.Request("HEAD", url, params, headers, nil)
} }
@@ -155,43 +201,91 @@ func (s *Session) Post(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header, headers *http.Header,
body *[]byte) (resp *http.Response, err error) { body *[]byte,
) (resp *http.Response, err error) {
return s.Request("POST", url, params, headers, body) return s.Request("POST", url, params, headers, body)
} }
func (s *Session) PostJSON(
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
responseContainer interface{},
) (resp *http.Response, err error) {
return s.RequestJSON("POST", url, params, headers, body, responseContainer)
}
func (s *Session) Put( func (s *Session) Put(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header, headers *http.Header,
body *[]byte) (resp *http.Response, err error) { body *[]byte,
) (resp *http.Response, err error) {
return s.Request("PUT", url, params, headers, body) return s.Request("PUT", url, params, headers, body)
} }
// Delete sends a DELETE request.
func Delete(
url string,
params *url.Values,
headers *http.Header,
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil)
return s.Delete(url, params, headers)
}
// Get sends a GET request. // Get sends a GET request.
func Get( func Get(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header) (resp *http.Response, err error) { headers *http.Header,
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil) s, _ := NewSession(nil, nil, nil)
return s.Get(url, params, headers) return s.Get(url, params, headers)
} }
// GetJSON sends a GET request and unmarshalls returned JSON.
func GetJSON(
url string,
params *url.Values,
headers *http.Header,
responseContainer interface{},
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil)
return s.RequestJSON("GET", url, params, headers, nil, responseContainer)
}
// Post sends a POST request. // Post sends a POST request.
func Post( func Post(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header, headers *http.Header,
body *[]byte) (resp *http.Response, err error) { body *[]byte,
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil) s, _ := NewSession(nil, nil, nil)
return s.Post(url, params, headers, body) return s.Post(url, params, headers, body)
} }
// PostJSON sends a POST request and unmarshalls returned JSON.
func PostJSON(
url string,
params *url.Values,
headers *http.Header,
body *[]byte,
responseContainer interface{},
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil)
return s.RequestJSON("POST", url, params, headers, nil, responseContainer)
}
// Put sends a PUT request. // Put sends a PUT request.
func Put( func Put(
url string, url string,
params *url.Values, params *url.Values,
headers *http.Header, headers *http.Header,
body *[]byte) (resp *http.Response, err error) { body *[]byte,
) (resp *http.Response, err error) {
s, _ := NewSession(nil, nil, nil) s, _ := NewSession(nil, nil, nil)
return s.Put(url, params, headers, body) return s.Put(url, params, headers, body)
} }