| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"io/ioutil" |
| 10 |
"net/http" |
| 11 |
) |
| 12 |
|
| 13 |
// Response represents a response from Elasticsearch. |
| 14 |
type Response struct { |
| 15 |
// StatusCode is the HTTP status code, e.g. 200. |
| 16 |
StatusCode int |
| 17 |
// Header is the HTTP header from the HTTP response. |
| 18 |
// Keys in the map are canonicalized (see http.CanonicalHeaderKey). |
| 19 |
Header http.Header |
| 20 |
// Body is the deserialized response body. |
| 21 |
Body json.RawMessage |
| 22 |
} |
| 23 |
|
| 24 |
// newResponse creates a new response from the HTTP response. |
| 25 |
func (c *Client) newResponse(res *http.Response) (*Response, error) { |
| 26 |
r := &Response{ |
| 27 |
StatusCode: res.StatusCode, |
| 28 |
Header: res.Header, |
| 29 |
} |
| 30 |
if res.Body != nil { |
| 31 |
slurp, err := ioutil.ReadAll(res.Body) |
| 32 |
if err != nil { |
| 33 |
return nil, err |
| 34 |
} |
| 35 |
// HEAD requests return a body but no content |
| 36 |
if len(slurp) > 0 { |
| 37 |
r.Body = json.RawMessage(slurp) |
| 38 |
} |
| 39 |
} |
| 40 |
return r, nil |
| 41 |
} |