forked from fcgmedia/gorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
224 lines (197 loc) · 6.41 KB
/
client.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright Siyabonga Dlamini ([email protected]).
All rights reserved.
Modified by HourlyHost, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package gorest
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
var sharedClient *http.Client
//Use this if you have a *http.Client instance that you specifically want to use.
//Otherwise just use NewRequestBuilder(), which uses the http.Client maintained by GoRest.
func NewRequestBuilderFromClient(client *http.Client, url string) (*RequestBuilder, error) {
req, err := http.NewRequest(GET, url, nil)
if err != nil {
return nil, err
}
rb := RequestBuilder{client, Application_Json, req}
return &rb, nil
}
//This creates a new RequestBuilder, backed by GoRest's internally managed http.Client.
//Although http.Client is useable concurrently, an instance of RequestBuilder is not safe for this.
//Because of http.Client's persistent(cached TCP connections) and concurrent nature,
//this can be used safely multiple times from different go routines.
func NewRequestBuilder(url string) (*RequestBuilder, error) {
if sharedClient == nil {
sharedClient = new(http.Client) //DefaultClient
}
req, err := http.NewRequest(GET, url, nil)
if err != nil {
return nil, err
}
rb := RequestBuilder{sharedClient, Application_Json, req}
return &rb, nil
}
type RequestBuilder struct {
client *http.Client
defaultContentType string
_req *http.Request
}
func (this *RequestBuilder) Request() *http.Request {
return this._req
}
func (this *RequestBuilder) UseContentType(mime string) *RequestBuilder {
this.defaultContentType = mime
return this
}
func (this *RequestBuilder) CacheNoCache() *RequestBuilder {
this.setCache("no-cache")
return this
}
func (this *RequestBuilder) CacheNoStore() *RequestBuilder {
this.setCache("no-store")
return this
}
func (this *RequestBuilder) CacheMaxAge(seconds int) *RequestBuilder {
this.setCache("max-age = " + strconv.Itoa(seconds))
return this
}
func (this *RequestBuilder) CacheStale(seconds int) *RequestBuilder {
this.setCache("max-stale = " + strconv.Itoa(seconds))
return this
}
func (this *RequestBuilder) CacheMinFresh(seconds int) *RequestBuilder {
this.setCache("min-fresh = " + strconv.Itoa(seconds))
return this
}
func (this *RequestBuilder) CacheOnlyIfCached() *RequestBuilder {
this.setCache("only-if-cached")
return this
}
func (this *RequestBuilder) CacheClearAllOptions() *RequestBuilder {
this._req.Header.Del("Cache-control")
return this
}
func (this *RequestBuilder) setCache(option string) {
this._req.Header.Add("Cache-control", option)
}
func (this *RequestBuilder) Accept(mime string) *RequestBuilder {
this._req.Header.Add("Accept", mime)
return this
}
func (this *RequestBuilder) AcceptClear() *RequestBuilder {
this._req.Header.Del("Accept")
return this
}
func (this *RequestBuilder) AcceptCharSet(set string) *RequestBuilder {
this._req.Header.Add("Accept-Charset", set)
return this
}
func (this *RequestBuilder) AcceptCharSetClear() *RequestBuilder {
this._req.Header.Del("Accept-Charset")
return this
}
func (this *RequestBuilder) AcceptEncoding(option string) *RequestBuilder {
this._req.Header.Add("Accept-Encoding", option)
return this
}
func (this *RequestBuilder) AcceptEncodingClear() *RequestBuilder {
this._req.Header.Del("Accept-Encoding")
return this
}
func (this *RequestBuilder) AcceptLanguage(lang string) *RequestBuilder {
this._req.Header.Add("Accept-Language", lang)
return this
}
func (this *RequestBuilder) AcceptLanguageClear() *RequestBuilder {
this._req.Header.Del("Accept-Language")
return this
}
func (this *RequestBuilder) ConnectionKeepAlive() *RequestBuilder {
this._req.Header.Set("Connection", "keep-alive")
return this
}
func (this *RequestBuilder) ConnectionClose() *RequestBuilder {
this._req.Header.Set("Connection", "close")
return this
}
func (this *RequestBuilder) AddCookie(cookie *http.Cookie) *RequestBuilder {
this._req.AddCookie(cookie)
return this
}
func (this *RequestBuilder) Delete() (*http.Response, error) {
// u, err := url.Parse(url_)
// if err != nil {
// return nil, err
// }
//
// //this._req.URL = u
this._req.Method = DELETE
return this.client.Do(this._req)
}
func (this *RequestBuilder) Head() (*http.Response, error) {
this._req.Method = HEAD
return this.client.Do(this._req)
}
func (this *RequestBuilder) Options(opts *[]string) (*http.Response, error) {
this._req.Method = OPTIONS
res, err := this.client.Do(this._req)
if err != nil {
return res, err
}
for _, str := range res.Header["Allow"] {
*opts = append(*opts, strings.Trim(str, " "))
}
return res, err
}
func (this *RequestBuilder) Get(i interface{}, expecting int) (*http.Response, error) {
//this._req.URL = u
this._req.Method = GET
res, err := this.client.Do(this._req)
if err != nil {
return res, err
}
if res.StatusCode == expecting {
buf := new(bytes.Buffer)
io.Copy(buf, res.Body)
res.Body.Close()
err = BytesToInterface(buf, i, this.defaultContentType)
return res, nil
}
return res, errors.New(res.Status)
}
func (this *RequestBuilder) Post(i interface{}) (*http.Response, error) {
this._req.Method = POST
bb, err := InterfaceToBytes(i, this.defaultContentType)
if err != nil {
return nil, err
}
this._req.Body = ioutil.NopCloser(bytes.NewBuffer(bb))
return this.client.Do(this._req)
}