1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-18 01:57:02 +00:00

Complete REST client for #43

- Add source, delete and purge
This commit is contained in:
James Hillyerd
2017-01-08 22:11:22 +00:00
parent d8255382da
commit c8fd56ca90
5 changed files with 206 additions and 15 deletions

View File

@@ -37,9 +37,9 @@ func (c *restClient) do(method, uri string) (*http.Response, error) {
return c.client.Do(req)
}
// doGet performs a GET request with this client and marshalls the JSON response into v
func (c *restClient) doGet(uri string, v interface{}) error {
resp, err := c.do("GET", uri)
// doGet performs an HTTP request with this client and marshalls the JSON response into v
func (c *restClient) doJSON(method string, uri string, v interface{}) error {
resp, err := c.do(method, uri)
if err != nil {
return err
}
@@ -48,8 +48,12 @@ func (c *restClient) doGet(uri string, v interface{}) error {
_ = resp.Body.Close()
}()
if resp.StatusCode == http.StatusOK {
// Decode response body
return json.NewDecoder(resp.Body).Decode(v)
if v == nil {
return nil
} else {
// Decode response body
return json.NewDecoder(resp.Body).Decode(v)
}
}
return fmt.Errorf("Unexpected HTTP response status %v: %s", resp.StatusCode, resp.Status)