This repository has been archived by the owner on May 20, 2021. It is now read-only.
/
client.go
286 lines (255 loc) · 7.32 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2015 Kerem Güneş
// <k-gun@mail.com>
//
// Apache License, Version 2.0
// <http://www.apache.org/licenses/LICENSE-2.0>
//
// 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 couch
// @uses fmt, strings, regexp
// @uses couch.util, couch.http
// @author Kerem Güneş <k-gun@mail.com>
package couch
import (
_fmt "fmt"
_str "strings"
_rex "regexp"
)
import (
"couch/util"
"couch/http"
)
// @object couch.Client
type Client struct {
Scheme string
Host string
Port uint16
Username string
Password string
Request *http.Request
Response *http.Response
Config map[string]interface{}
}
// Default config options
// @var mixed
var (
Scheme = "http"
Host = "localhost"
Port uint16 = 5984
Username = ""
Password = ""
)
// Constructor.
//
// @param couch *couch.http.Couch
// @return *couch.Client
func NewClient(couch *Couch) (*Client) {
this := &Client{
Scheme: Scheme,
Host: Host,
Port: Port,
Username: Username,
Password: Password,
}
Config := util.Map()
Config["Couch.NAME"] = NAME
Config["Couch.VERSION"] = VERSION
Config["Couch.DEBUG"] = DEBUG // set default
// copy Couch configs
config := couch.GetConfig()
if config != nil {
for key, value := range config {
Config[key] = value
}
}
// add scheme if provided
if scheme := config["Scheme"]; scheme != nil {
this.Scheme = scheme.(string)
}
// add host if provided
if host := config["Host"]; host != nil {
this.Host = host.(string)
}
// add port if provided
if port := config["Port"]; port != nil {
this.Port = port.(uint16)
}
// add username if provided
if username := config["Username"]; username != nil {
this.Username = username.(string)
}
// add password if provided
if password := config["Password"]; password != nil {
this.Password = password.(string)
}
Config["Scheme"] = this.Scheme
Config["Host"] = this.Host
Config["Port"] = this.Port
Config["Username"] = this.Username
Config["Password"] = this.Password
// add debug if provided
if debug := couch.Config["debug"]; debug != nil {
Config["Couch.DEBUG"] = debug
}
this.Config = Config
return this
}
// Get request object.
//
// @return *couch.http.Request
func (this *Client) GetRequest() (*http.Request) {
return this.Request
}
// Get response object.
//
// @return *couch.http.Response
func (this *Client) GetResponse() (*http.Response) {
return this.Response
}
// Perform a request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param body interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) DoRequest(
uri string, uriParams interface{}, body interface{}, headers interface{},
) (*http.Response) {
// notation: GET /foo
re, _ := _rex.Compile("^([A-Z]+)\\s+(/.*)")
if re == nil {
panic("Usage: <REQUEST METHOD> <REQUEST URI>!")
}
match := re.FindStringSubmatch(uri)
if len(match) < 3 {
panic("Usage: <REQUEST METHOD> <REQUEST URI>!")
}
this.Request = http.NewRequest(this.Config)
this.Response = http.NewResponse()
// merge host, port and uri
uri = _fmt.Sprintf("%s:%v/%s",
this.Host, this.Port, _str.Trim(match[2], "/ "))
// set request method & uri
this.Request.SetMethod(match[1])
this.Request.SetUri(uri, uriParams)
// set request headers
if headers, _ := headers.(map[string]interface{}); headers != nil {
for key, value := range headers {
this.Request.SetHeader(key, value)
}
}
// set request body
this.Request.SetBody(body)
// perform request
if result := this.Request.Send(); result != "" {
tmp := make([]string, 2)
tmp = _str.SplitN(result, "\r\n\r\n", 2)
if len(tmp) != 2 {
panic("No valid response returned from server!")
}
// set response status & headers
if headers := util.ParseHeaders(_str.TrimSpace(tmp[0])); headers != nil {
if status := headers["0"]; status != "" {
this.Response.SetStatus(headers["0"])
}
for key, value := range headers {
this.Response.SetHeader(key, value)
}
}
// set response body
this.Response.SetBody(tmp[1])
}
// error?
if this.Response.GetStatusCode() >= 400 {
// "" means -use self body-
this.Response.SetError("")
}
return this.Response
}
// Perform a HEAD request.
//
//
// @param uri string
// @param uriParams map[string]interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Head(
uri string, uriParams interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_HEAD +" /"+ uri, uriParams, nil, headers)
}
// Perform a GET request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Get(
uri string, uriParams interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_GET +" /"+ uri, uriParams, nil, headers)
}
// Perform a POST request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param body interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Post(
uri string, uriParams interface{}, body interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_POST +" /"+ uri, uriParams, body, headers)
}
// Perform a PUT request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param body interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Put(
uri string, uriParams interface{}, body interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_PUT +" /"+ uri, uriParams, body, headers)
}
// Perform a DELETE request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Delete(
uri string, uriParams interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_DELETE +" /"+ uri, uriParams, nil, headers)
}
// Perform a COPY request.
//
// @param uri string
// @param uriParams map[string]interface{}
// @param headers map[string]interface{}
// @return *couch.http.Response
// @panics
func (this *Client) Copy(
uri string, uriParams interface{}, headers interface{},
) (*http.Response) {
return this.DoRequest(http.METHOD_COPY +" /"+ uri, uriParams, nil, headers)
}