-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathread.go
49 lines (43 loc) · 1.45 KB
/
read.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
package couchdb
import (
"io"
)
func (db *CouchDB) getRaw(path, query string) (io.Reader, error) {
req, err := db.createRequest("GET", path, query, nil)
if err != nil {
return nil, err
}
r, err := DefaultClient.Do(req)
if err != nil {
return nil, err
}
if r.StatusCode >= 400 {
return nil, responseToCouchError(r)
}
return r.Body, nil
}
// Does a "raw" GET, returning an io.Reader that can be used to parse the returned data yourself.
func (db *CouchDB) GetRaw(path, query string) (io.Reader, error) {
return db.getRaw(escape_docid(path), query)
}
// Same as GetRaw, does a "raw" GET, returning an io.Reader that can be used to parse the returned data yourself.
func (db *CouchDB) GetAttachment(docid, attname, query string) (io.Reader, error) {
if docid == "" {
return nil, MissingDocumentIDError
}
return db.getRaw(escape_docid(docid)+"/"+escape_docid(attname), query)
}
// Accepts a struct or a map[string]something to fill with the doc's data, and a docid path relative to the database, returns error status
func (db *CouchDB) GetDocument(doc interface{}, docid string) error {
if docid == "" {
return MissingDocumentIDError
}
return db.get(doc, escape_docid(docid), "")
}
// As GetDocument, but also accepts a rev. Will attempt to retrieve the document at that rev
func (db *CouchDB) GetDocumentAtRev(doc interface{}, docid, rev string) error {
if docid == "" {
return MissingDocumentIDError
}
return db.get(doc, escape_docid(docid), rev)
}