-
Notifications
You must be signed in to change notification settings - Fork 609
/
esapi.response.go
105 lines (92 loc) · 2.54 KB
/
esapi.response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package esapi
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
// Response represents the API response.
//
type Response struct {
StatusCode int
Header http.Header
Body io.ReadCloser
}
// String returns the response as a string.
//
// The intended usage is for testing or debugging only.
//
func (r *Response) String() string {
var (
out = new(bytes.Buffer)
b1 = bytes.NewBuffer([]byte{})
b2 = bytes.NewBuffer([]byte{})
tr io.Reader
)
if r != nil && r.Body != nil {
tr = io.TeeReader(r.Body, b1)
defer r.Body.Close()
if _, err := io.Copy(b2, tr); err != nil {
out.WriteString(fmt.Sprintf("<error reading response body: %v>", err))
return out.String()
}
defer func() { r.Body = ioutil.NopCloser(b1) }()
}
if r != nil {
out.WriteString(fmt.Sprintf("[%d %s]", r.StatusCode, http.StatusText(r.StatusCode)))
if r.StatusCode > 0 {
out.WriteRune(' ')
}
} else {
out.WriteString("[0 <nil>]")
}
if r != nil && r.Body != nil {
out.ReadFrom(b2) // errcheck exclude (*bytes.Buffer).ReadFrom
}
return out.String()
}
// Status returns the response status as a string.
//
func (r *Response) Status() string {
var b strings.Builder
if r != nil {
b.WriteString(strconv.Itoa(r.StatusCode))
b.WriteString(" ")
b.WriteString(http.StatusText(r.StatusCode))
}
return b.String()
}
// IsError returns true when the response status indicates failure.
//
func (r *Response) IsError() bool {
return r.StatusCode > 299
}
// Warnings returns the deprecation warnings from response headers.
//
func (r *Response) Warnings() []string {
return r.Header["Warning"]
}
// HasWarnings returns true when the response headers contain deprecation warnings.
//
func (r *Response) HasWarnings() bool {
return len(r.Warnings()) > 0
}