-
Notifications
You must be signed in to change notification settings - Fork 1
/
documents.go
156 lines (133 loc) · 4.19 KB
/
documents.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
package rspace
import (
"encoding/json"
"errors"
"fmt"
// "net/url"
// "strconv"
)
type DocumentService struct {
BaseService
}
func (ds *DocumentService) documentsUrl() string {
return ds.BaseUrl.String() + "/documents"
}
func (ds *DocumentService) statusUrl() string {
return ds.BaseUrl.String() + "/status"
}
// GetStatus returns the result of the /status endpoint
func (ds *DocumentService) GetStatus() (*Status, error) {
status, err := ds.doGet(ds.statusUrl())
if err != nil {
return nil, err
}
res := Status{}
json.Unmarshal(status, &res)
return &res, nil
}
// Paginated listing of Documents
func (ds *DocumentService) Documents(config RecordListingConfig) (*DocumentList, error) {
url := ds._generateUrl(config, "", false)
return ds._doDocList(url)
}
func (ds *DocumentService) _doDocList(url string) (*DocumentList, error) {
data, err := ds.doGet(url)
if err != nil {
return nil, err
}
var result = DocumentList{}
json.Unmarshal(data, &result)
return &result, nil
}
func (ds *DocumentService) _generateUrl(config RecordListingConfig, searchTerm string, isAdvancedSearch bool) string {
params := config.toParams()
if len(searchTerm) > 0 {
if isAdvancedSearch {
params.Add("advancedQuery", searchTerm)
} else {
params.Add("query", searchTerm)
}
}
encoded := params.Encode()
url := ds.documentsUrl() + "?" + encoded
return url
}
//SearchDocuments performs basic search of a single search term, performing a global search
func (ds *DocumentService) SearchDocuments(config RecordListingConfig, searchTerm string) (*DocumentList, error) {
url := ds._generateUrl(config, searchTerm, false)
return ds._doDocList(url)
}
func (ds *DocumentService) AdvancedSearchDocuments(config RecordListingConfig, searchQuery *SearchQuery) (*DocumentList, error) {
urlStr := ""
if searchQuery != nil {
queryJson, _ := json.Marshal(searchQuery)
urlStr = ds._generateUrl(config, string(queryJson), true)
} else {
urlStr = ds._generateUrl(config, "", false)
}
return ds._doDocList(urlStr)
}
// DocumentById retrieves full document content
func (ds *DocumentService) DocumentById(docId int) (*Document, error) {
url := fmt.Sprintf("%s/%d", ds.documentsUrl(), docId)
data, err := ds.doGet(url)
if err != nil {
return nil, err
}
var result = Document{}
json.Unmarshal(data, &result)
return &result, nil
}
// DeleteDocument attempts to delete the document with the specified ID
func (ds *DocumentService) DeleteDocument(documentId int) (bool, error) {
url := fmt.Sprintf("%s/%d", ds.documentsUrl(), documentId)
return ds.doDelete(url)
}
// DocumentEdit updates an existing RSpace document
func (ds *DocumentService) DocumentEdit(docId int, putData *DocumentPost) (*Document, error) {
if docId == 0 {
return nil, errors.New("docId is required")
}
url := fmt.Sprintf("%s/%d", ds.documentsUrl(), docId)
data, err := ds.doPutJsonBody(putData, url)
if err != nil {
return nil, err
}
result := &Document{}
json.Unmarshal(data, result)
return result, nil
}
// DocumentNew creates a new RSpace document
func (ds *DocumentService) DocumentNew(post *DocumentPost) (*Document, error) {
data, err := ds.doPostJsonBody(post, ds.documentsUrl())
if err != nil {
return nil, err
}
result := &Document{}
json.Unmarshal(data, result)
return result, nil
}
// NewBasicDocumentWithContent creates a new BasicDocument document with name, tags(optional) and content in a
// single text field.
func (ds *DocumentService) NewBasicDocumentWithContent(name string, tags string, contentHtml string) (*Document, error) {
post := BasicPost(name, tags)
content := FieldContent{Content: contentHtml}
fields := make([]FieldContent, 1)
fields[0] = content
post.Fields = fields
return ds.doPostCreateDocument(post)
}
// NewEmptyBasicDocument creates a new, empty BasicDocument with no content.
func (ds *DocumentService) NewEmptyBasicDocument(name string, tags string) (*Document, error) {
post := BasicPost(name, tags)
return ds.doPostCreateDocument(post)
}
func (ds *DocumentService) doPostCreateDocument(postData *DocumentPost) (*Document, error) {
data, err := ds.doPostJsonBody(postData, ds.documentsUrl())
if err != nil {
return nil, err
}
result := &Document{}
json.Unmarshal(data, result)
return result, nil
}