-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
75 lines (59 loc) · 1.67 KB
/
client.go
File metadata and controls
75 lines (59 loc) · 1.67 KB
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
package gocouchDB
type Client struct {
ClientBase
}
func NewClientByDSN(dsn string) *Client{
transport := NewTransport(dsn)
c := new(Client)
c.transport = transport
c.Headers = map[string]string {}
return c
}
func NewClientByTransport(transport ITransport) *Client {
c := new(Client)
c.transport = transport
c.Headers = map[string]string {}
return c
}
// get server info
func (cl *Client)ServerInfo() (j map[string]interface{}, err error) {
return cl.do(GET, "", nil, nil)
}
// create database with dbName
func (cl *Client)CreateDatabase(dbName string) (j map[string]interface{}, err error) {
return cl.do(PUT, dbName, nil, nil)
}
// this is a tricky function
// couchdb only this interface return a list
// not a key-value map json
func (cl *Client)ListAllDatabases() (j []string, err error) {
return cl.requestList(GET, "_all_dbs", nil)
}
// get database
func (cl *Client)GetDatabase(dbName string) (d *Database, err error) {
d = NewDatabase(dbName, cl.transport)
d.SetAuth(cl.Username, cl.Password)
return d, err
}
// replicate database
func (cl *Client)Replicate(task *ReplicateTask) (j map[string]interface{}, err error) {
body := map[string]interface{}{
"continuous": task.Continuous,
"create_target": task.CreateTarget,
"source": task.Source,
"target": task.Target,
}
if task.DocumentIDs != nil {
body["doc_ids"] = task.DocumentIDs
}
if task.Proxy != "" {
body["proxy"] = task.Proxy
}
if task.Cancel {
body["cancel"] = task.Cancel
}
if err != nil {
return j, err
}
return cl.do(POST, "_replicate", body, nil)
}