-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArchivalObjects.go
173 lines (137 loc) · 4.05 KB
/
ArchivalObjects.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
package aspace
import (
"encoding/json"
"fmt"
"io"
)
func (a *ASClient) GetArchivalObjectIDs(repositoryID int) ([]int, error) {
aoIds := []int{}
endpoint := fmt.Sprintf("/repositories/%d/archival_objects?all_ids=true", repositoryID)
response, err := a.get(endpoint, true)
if err != nil {
return aoIds, err
}
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &aoIds)
if err != nil {
return aoIds, err
}
return aoIds, nil
}
func (a *ASClient) GetArchivalObject(repositoryId int, aoId int) (ArchivalObject, error) {
aoURI := fmt.Sprintf("/repositories/%d/archival_objects/%d", repositoryId, aoId)
return a.GetArchivalObjectFromURI(aoURI)
}
func (a *ASClient) GetArchivalObjectFromURI(aoURI string) (ArchivalObject, error) {
ao := ArchivalObject{}
reponse, err := a.get(aoURI, true)
if err != nil {
return ao, err
}
body, err := io.ReadAll(reponse.Body)
if err != nil {
return ao, err
}
err = json.Unmarshal(body, &ao)
if err != nil {
return ao, err
}
return ao, nil
}
func (a *ASClient) GetArchivalObjectsForResource(repositoryId int, resourceId int) ([]string, error) {
aos := []string{}
rootNode, err := a.GetRootNode(repositoryId, resourceId)
if err != nil {
return aos, err
}
aos = []string{}
for _, node := range rootNode.PrecomputedWaypoints[""].Nodes {
aos = append(aos, node.URI)
}
return aos, nil
}
func (a *ASClient) UpdateArchivalObject(repositoryId int, archivalObjectId int, archivalObject ArchivalObject) (string, error) {
responseMessage := ""
endpoint := fmt.Sprintf("/repositories/%d/archival_objects/%d", repositoryId, archivalObjectId)
body, err := json.Marshal(archivalObject)
if err != nil {
return responseMessage, err
}
response, err := a.post(endpoint, true, string(body))
if err != nil {
return responseMessage, err
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return responseMessage, err
}
responseMessage = string(responseBody)
return responseMessage, nil
}
// func getChildArchivalObjectURIs(children []ResourceTree, aos *[]string) {
// for _, child := range children {
// *aos = append(*aos, child.RecordURI)
// if child.HasChildren {
// getChildArchivalObjectURIs(child.Children, aos)
// }
// }
// }
func (a ASClient) DeleteArchivalObject(repositoryID int, archivalObjectID int) (string, error) {
responseMessage := ""
endpoint := fmt.Sprintf("/repositories/%d/archival_objects/%d", repositoryID, archivalObjectID)
response, err := a.delete(endpoint)
if err != nil {
return responseMessage, err
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return responseMessage, err
}
responseMessage = string(responseBody)
return responseMessage, nil
}
// func getChildArchivalObjectURIsFiltered(children []ResourceTree, aos *[]string, filter string) {
// matcher := regexp.MustCompile(filter)
// for _, child := range children {
// if matcher.MatchString(strings.Join(child.InstanceTypes, " ")) == true {
// *aos = append(*aos, child.RecordURI)
// }
// if child.HasChildren {
// getChildArchivalObjectURIs(child.Children, aos)
// }
// }
// }
func (a *ASClient) GetRandomArchivalObject(repositoryID int, resourceID int) (int, int, error) {
aoURIs, err := a.GetArchivalObjectsForResource(repositoryID, resourceID)
if err != nil {
return repositoryID, 0, err
}
aoURI := aoURIs[rGen.Intn(len(aoURIs))]
_, aoID, _ := URISplit(aoURI)
return repositoryID, aoID, nil
}
func (a *ASClient) SearchArchivalObjects(repoID int, s string) ([]ArchivalObject, error) {
aos := []ArchivalObject{}
iresults, err := a.Search(repoID, "archival_object", s, 1)
if err != nil {
return aos, err
}
lastPage := iresults.LastPage
//iterate here through pages
for i := 1; i < lastPage; i++ {
results, err := a.Search(repoID, "archival_object", s, i)
for _, r := range results.Results {
ao_json := []byte(fmt.Sprintf("%v", r["json"]))
if err != nil {
return aos, err
}
ao := ArchivalObject{}
err := json.Unmarshal(ao_json, &ao)
if err != nil {
return aos, err
}
aos = append(aos, ao)
}
}
return aos, nil
}