This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcloudant.go
245 lines (212 loc) · 6.19 KB
/
cloudant.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
package cloudant
import (
"errors"
"fmt"
"strconv"
request "github.com/parnurzeal/gorequest"
couchdb "github.com/timjacobi/go-couchdb"
)
// Client ...
type Client struct {
Client *couchdb.Client
username string
password string
}
// DB ...
type DB struct {
*couchdb.DB
username string
password string
path string
}
// DB returns the DB object without verifying its existence.
func (c *Client) DB(name string) *DB {
dbPath := c.Client.URL() + "/" + name
return &DB{c.Client.DB(name), c.username, c.password, dbPath}
}
// Options ...
type Options couchdb.Options
// Query ...
type Query struct {
Selector map[string]interface{} `json:"selector"`
Fields []string `json:"fields,omitempty"`
Sort []interface{} `json:"sort,omitempty"`
Limit int `json:"limit,omitempty"`
Skip int `json:"skip,omitempty"`
}
// Index query struct
type Index struct {
Index struct {
Fields interface{} `json:"fields"`
} `json:"index"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Ddoc string `json:"ddoc,omitempty"`
}
// NewClient ...
func NewClient(username string, password string) (*Client, error) {
auth := couchdb.BasicAuth(username, password)
url := fmt.Sprintf("https://%s.cloudant.com", username)
couchClient, err := couchdb.NewClient(url, nil)
couchClient.SetAuth(auth)
return &Client{Client: couchClient, username: username, password: password}, err
}
// IsAlive check whether a server is alive.
func (c *Client) IsAlive() error {
return c.Client.Ping()
}
// CreateDB ensures that a database with the given name exists.
func (c *Client) CreateDB(dbName string) (*DB, error) {
var db *couchdb.DB
var err error
if db, err = c.Client.CreateDB(dbName); err != nil {
return nil, err
}
dbPath := c.Client.URL() + "/" + dbName
return &DB{db, c.username, c.password, dbPath}, nil
}
// EnsureDB ensures that a database with the given name exists.
func (c *Client) EnsureDB(name string) (*DB, error) {
var db *couchdb.DB
var err error
if db, err = c.Client.EnsureDB(name); err != nil {
return nil, err
}
dbPath := c.Client.URL() + "/" + name
return &DB{db, c.username, c.password, dbPath}, nil
}
// DeleteDB ...
func (c *Client) DeleteDB(dbName string) error {
return c.Client.DeleteDB(dbName)
}
// CreateDocument ...
func (db *DB) CreateDocument(doc interface{}) (string, string, error) {
return db.Post(doc)
}
// DeleteDocument ...
func (db *DB) DeleteDocument(id string, rev string) (string, error) {
return db.Delete(id, rev)
}
// UpdateDocument ...
func (db *DB) UpdateDocument(id string, rev string, doc interface{}) (string, error) {
return db.Put(id, doc, rev)
}
// GetDocument ...
func (db *DB) GetDocument(id string, doc interface{}, opts Options) error {
return db.Get(id, doc, couchdb.Options(opts))
}
// GetDocumentRev gets the current document revision.
func (db *DB) GetDocumentRev(id string) (string, error) {
return db.Rev(id)
}
// GetAllDocument ...
func (db *DB) GetAllDocument(result interface{}, opts Options) error {
return db.AllDocs(result, couchdb.Options(opts))
}
// SearchDocument ...
func (db *DB) SearchDocument(query Query) (result []interface{}, err error) {
req := request.New()
path := "/_find"
var data struct {
Docs []interface{}
Bookmark string `json:"bookmark"`
}
_, _, errs := req.SetBasicAuth(db.username, db.password).Post(db.path + path).Send(query).EndStruct(&data)
if errs != nil {
return nil, errs[0]
}
return data.Docs, nil
}
// SetIndex ...
func (db *DB) SetIndex(index Index) error {
req := request.New()
path := "/_index"
resp, _, errs := req.SetBasicAuth(db.username, db.password).Post(db.path + path).Send(index).End()
if errs != nil {
return errs[0]
}
if resp.StatusCode >= 400 {
return errors.New("Error in setting index: " + strconv.Itoa(resp.StatusCode))
}
return nil
}
// CreateDesignDoc ...
func (db *DB) CreateDesignDoc(name string, designJSON string) error {
var data struct {
Ok bool `json:"ok"`
ID string `json:"id"`
Rev string `json:"rev"`
}
req := request.New()
path := "/_design/" + name
_, _, errs := req.SetBasicAuth(db.username, db.password).Put(db.path + path).SendString(designJSON).EndStruct(&data)
if errs != nil {
return errs[0]
}
if data.Ok != true {
return errors.New("Error in creating design doc")
}
return nil
}
// DesignDocument ...
type DesignDocument struct {
ID string `json:"_id"`
Indexes map[string]interface{} `json:"indexes,omitempty"`
Views map[string]interface{} `json:"views,omitempty"`
}
// NewDesignDocument ...
func NewDesignDocument(name string) *DesignDocument {
return &DesignDocument{ID: "_design/" + name}
}
// Get design document.
func (ddoc *DesignDocument) Get(db *DB) error {
return db.GetDocument(ddoc.ID, ddoc, Options{})
}
type searchRow struct {
ID string `json:"id"`
Order []float64 `json:"order"`
Fields interface{} `json:"fields"`
}
// SearchResp ...
type SearchResp struct {
Num int `json:"total_rows"`
Bookmark string `json:"bookmark"`
Rows []searchRow `json:"rows"`
}
// Search indexes, defined in design documents.
// Cloudant doc: https://docs.cloudant.com/search.html
func (ddoc *DesignDocument) Search(db *DB, index, query, bookmark string, limit int) (*SearchResp, error) {
path := "/" + ddoc.ID + "/_search/" + index
body := &SearchResp{}
req := request.New().
SetBasicAuth(db.username, db.password).
Get(db.path + path).
Query("query=" + query).
Query("limit=" + strconv.Itoa(limit))
if bookmark != "" {
req = req.Query("bookmark=" + bookmark)
}
if _, _, errs := req.EndStruct(body); errs != nil {
return nil, errs[len(errs)-1]
}
return body, nil
}
// ViewResp ...
type ViewResp struct {
Num int `json:"total_rows"`
Offset int `json:"offset"`
Rows []interface{} `json:"rows"`
}
// View ...
// Cloudant doc: https://docs.cloudant.com/creating_views.html
func (ddoc *DesignDocument) View(db *DB, view string) (*ViewResp, error) {
path := "/" + ddoc.ID + "/_view/" + view
body := &ViewResp{}
req := request.New().
SetBasicAuth(db.username, db.password).
Get(db.path + path)
if _, _, errs := req.EndStruct(body); errs != nil {
return nil, errs[len(errs)-1]
}
return body, nil
}