forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
77 lines (65 loc) · 1.81 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
package couchdb
import (
"context"
"encoding/json"
"io"
"github.com/flimzy/kivik"
"github.com/flimzy/kivik/driver"
"github.com/go-kivik/couchdb/chttp"
)
func (c *client) AllDBs(ctx context.Context, _ map[string]interface{}) ([]string, error) {
var allDBs []string
_, err := c.DoJSON(ctx, kivik.MethodGet, "/_all_dbs", nil, &allDBs)
return allDBs, err
}
func (c *client) DBExists(ctx context.Context, dbName string, _ map[string]interface{}) (bool, error) {
if dbName == "" {
return false, missingArg("dbName")
}
_, err := c.DoError(ctx, kivik.MethodHead, dbName, nil)
if kivik.StatusCode(err) == kivik.StatusNotFound {
return false, nil
}
return err == nil, err
}
func (c *client) CreateDB(ctx context.Context, dbName string, _ map[string]interface{}) error {
if dbName == "" {
return missingArg("dbName")
}
_, err := c.DoError(ctx, kivik.MethodPut, dbName, nil)
return err
}
func (c *client) DestroyDB(ctx context.Context, dbName string, _ map[string]interface{}) error {
if dbName == "" {
return missingArg("dbName")
}
_, err := c.DoError(ctx, kivik.MethodDelete, dbName, nil)
return err
}
func (c *client) DBUpdates() (updates driver.DBUpdates, err error) {
resp, err := c.DoReq(context.Background(), kivik.MethodGet, "/_db_updates?feed=continuous&since=now", nil)
if err != nil {
return nil, err
}
if err := chttp.ResponseError(resp); err != nil {
return nil, err
}
return newUpdates(resp.Body), nil
}
type couchUpdates struct {
body io.ReadCloser
dec *json.Decoder
}
var _ driver.DBUpdates = &couchUpdates{}
func newUpdates(body io.ReadCloser) *couchUpdates {
return &couchUpdates{
body: body,
dec: json.NewDecoder(body),
}
}
func (u *couchUpdates) Next(update *driver.DBUpdate) error {
return u.dec.Decode(update)
}
func (u *couchUpdates) Close() error {
return u.body.Close()
}