forked from koltyakov/gosip
/
mmd.go
159 lines (136 loc) · 3.8 KB
/
mmd.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
package api
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/pnocera/gosip"
"github.com/pnocera/gosip/csom"
)
// Taxonomy session struct
type Taxonomy struct {
client *HTTPClient
config *RequestConfig
endpoint string
}
// NewTaxonomy - taxonomy struct constructor function
func NewTaxonomy(client *gosip.SPClient, siteURL string, config *RequestConfig) *Taxonomy {
return &Taxonomy{
client: NewHTTPClient(client),
endpoint: siteURL,
config: config,
}
}
// Stores gets term stores collection object
func (taxonomy *Taxonomy) Stores() *TermStores {
return &TermStores{
client: taxonomy.client,
config: taxonomy.config,
endpoint: taxonomy.endpoint,
}
}
/* Utility methods */
func appendTaxonomyProp(props []string, prop string) []string {
for _, p := range strings.SplitN(prop, ",", -1) {
p = strings.Trim(p, " ")
found := false
for _, pp := range props {
if pp == p {
found = true
}
}
if !found {
props = append(props, p)
}
}
return props
}
func trimTaxonomyGUID(guid string) string {
guid = strings.ToLower(guid)
guid = strings.Replace(guid, "/guid(", "", 1)
guid = strings.Replace(guid, ")/", "", 1)
return guid
}
func csomResponse(httpClient *HTTPClient, siteURL string, config *RequestConfig, csomBuilder csom.Builder) (map[string]interface{}, error) {
csomPkg, err := csomBuilder.Compile()
if err != nil {
return nil, err
}
jsomResp, err := httpClient.ProcessQuery(siteURL, bytes.NewBuffer([]byte(csomPkg)), config)
if err != nil {
// Retry Terms update conflicts
if strings.Index(err.Error(), "Term update failed because of save conflict") != -1 {
if config == nil {
config = &RequestConfig{}
}
if config.Headers == nil {
config.Headers = map[string]string{}
}
retryStr, ok := config.Headers["X-Gosip-Retry"]
if !ok {
retryStr = "0"
}
retry, _ := strconv.Atoi(retryStr)
config.Headers["X-Gosip-Retry"] = strconv.Itoa(retry + 1)
if retry+1 <= 5 {
return csomResponse(httpClient, siteURL, config, csomBuilder)
}
}
return nil, err
}
var jsomRespArr []interface{}
if err := json.Unmarshal(jsomResp, &jsomRespArr); err != nil {
return nil, err
}
if jsomRespArr[len(jsomRespArr)-1] == nil {
return nil, fmt.Errorf("object not found")
}
res, ok := jsomRespArr[len(jsomRespArr)-1].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("can't cast CSOM response, %v+", jsomRespArr[len(jsomRespArr)-1])
}
return res, nil
}
func csomRespChildItems(httpClient *HTTPClient, siteURL string, config *RequestConfig, csomBuilder csom.Builder) ([]map[string]interface{}, error) {
data, err := csomResponse(httpClient, siteURL, config, csomBuilder)
if err != nil {
return nil, err
}
items, ok := data["_Child_Items_"].([]interface{})
if !ok {
return nil, fmt.Errorf("can't get child items")
}
var resItems []map[string]interface{}
for _, item := range items {
resItem, ok := item.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("can't get child item")
}
resItems = append(resItems, resItem)
}
return resItems, nil
}
func csomRespChildItemsInProp(httpClient *HTTPClient, siteURL string, config *RequestConfig, csomBuilder csom.Builder, prop string) ([]map[string]interface{}, error) {
data, err := csomResponse(httpClient, siteURL, config, csomBuilder)
if err != nil {
return nil, err
}
propData, ok := data[prop].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("can't get property data")
}
items, ok := propData["_Child_Items_"].([]interface{})
if !ok {
return nil, fmt.Errorf("can't get child items")
}
var resItems []map[string]interface{}
for _, item := range items {
resItem, ok := item.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("can't get child item")
}
resItems = append(resItems, resItem)
}
return resItems, nil
}