-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
65 lines (58 loc) · 1.56 KB
/
document.go
File metadata and controls
65 lines (58 loc) · 1.56 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
package arango
import (
"encoding/json"
"io/ioutil"
"net/http"
)
type Document map[string]interface{}
func (d Document) Id() string { return d["_id"].(string) }
func (d Document) Key() string { return d["_key"].(string) }
func (d Document) Rev() string { return d["_rev"].(string) }
func Find(handle string) (d Document, err error) {
data, err := getRaw("/_api/document/" + handle)
if err != nil {
return
}
err = json.Unmarshal(data, &d)
return
}
func FindIf(handle, etag string, match bool) (d Document, err error) {
req, err := http.NewRequest("GET", endpoint+"/_api/document/"+handle, nil)
if err != nil {
return
}
if match {
req.Header.Add("If-Match", etag)
} else {
req.Header.Add("If-None-Match", etag)
}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode == 200 {
// Parse the response into the document
var data []byte
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(data, &d) // unmarshall into the document and return
} else if !((match && resp.StatusCode == 412) || (!match && resp.StatusCode == 304) || resp.StatusCode == 404) {
panic(apiChanedPanic) // did not get a expected status code based on match
}
return
}
func CreateDocument(doc map[string]interface{}, collection string, createCollection bool) (d Document, err error) {
path := "/_api/document?collection=" + collection
if createCollection {
path += "&createCollection=true"
}
v, err := post(path, doc)
if err != nil {
return
}
d, err = Find(v["_id"].(string))
return
}