forked from trivago/gollum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
131 lines (116 loc) · 2.98 KB
/
request.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 Matthew Baird
// Licensed 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 elastigo
import (
"bytes"
"encoding/json"
"fmt"
hostpool "github.com/bitly/go-hostpool"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
type Request struct {
*http.Client
*http.Request
hostResponse hostpool.HostPoolResponse
}
func (r *Request) SetBodyJson(data interface{}) error {
body, err := json.Marshal(data)
if err != nil {
return err
}
r.SetBodyBytes(body)
r.Header.Set("Content-Type", "application/json")
return nil
}
func (r *Request) SetBodyString(body string) {
r.SetBody(strings.NewReader(body))
}
func (r *Request) SetBodyBytes(body []byte) {
r.SetBody(bytes.NewReader(body))
}
func (r *Request) SetBody(body io.Reader) {
rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
rc = ioutil.NopCloser(body)
}
r.Body = rc
if body != nil {
switch v := body.(type) {
case *strings.Reader:
r.ContentLength = int64(v.Len())
case *bytes.Reader:
r.ContentLength = int64(v.Len())
case *bytes.Buffer:
r.ContentLength = int64(v.Len())
}
}
}
func (r *Request) Do(v interface{}) (int, []byte, error) {
response, bodyBytes, err := r.DoResponse(v)
if err != nil {
return -1, nil, err
}
return response.StatusCode, bodyBytes, err
}
func (r *Request) DoResponse(v interface{}) (*http.Response, []byte, error) {
var client = r.Client
if client == nil {
client = http.DefaultClient
}
res, err := client.Do(r.Request)
// Inform the HostPool of what happened to the request and allow it to update
r.hostResponse.Mark(err)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, nil, err
}
if res.StatusCode == 404 {
return nil, bodyBytes, RecordNotFound
}
if res.StatusCode > 304 && v != nil {
jsonErr := json.Unmarshal(bodyBytes, v)
if jsonErr != nil {
return nil, nil, jsonErr
}
}
return res, bodyBytes, err
}
func Escape(args map[string]interface{}) (s string, err error) {
vals := url.Values{}
for key, val := range args {
switch v := val.(type) {
case string:
vals.Add(key, v)
case bool:
vals.Add(key, strconv.FormatBool(v))
case int, int32, int64:
vals.Add(key, strconv.Itoa(v.(int)))
case float32, float64:
vals.Add(key, strconv.FormatFloat(v.(float64), 'f', -1, 64))
case []string:
vals.Add(key, strings.Join(v, ","))
default:
err = fmt.Errorf("Could not format URL argument: %s", key)
return
}
}
s = vals.Encode()
return
}