-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigitalObjects.go
174 lines (143 loc) · 3.99 KB
/
DigitalObjects.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
174
package aspace
import (
"encoding/json"
"fmt"
"io"
)
func (a *ASClient) GetDigitalObjectIDs(repositoryId int) ([]int, error) {
daoIds := []int{}
endpoint := fmt.Sprintf("/repositories/%d/digital_objects?all_ids=true", repositoryId)
response, err := a.get(endpoint, true)
if err != nil {
return daoIds, err
}
body, _ := io.ReadAll(response.Body)
err = json.Unmarshal(body, &daoIds)
if err != nil {
return daoIds, err
}
return daoIds, nil
}
func (a *ASClient) GetDigitalObject(repositoryId int, daoId int) (DigitalObject, error) {
doURI := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, daoId)
return a.GetDigitalObjectFromURI(doURI)
}
func (a *ASClient) GetDigitalObjectFromURI(doURI string) (DigitalObject, error) {
do := DigitalObject{}
response, err := a.get(doURI, true)
if err != nil {
return do, err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return do, err
}
err = json.Unmarshal(body, &do)
if err != nil {
return do, err
}
return do, nil
}
func (a *ASClient) UpdateDigitalObject(repositoryId int, daoId int, dao DigitalObject) (string, error) {
endpoint := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, daoId)
body, err := json.Marshal(dao)
if err != nil {
return "", err
}
response, err := a.post(endpoint, true, string(body))
if err != nil {
return "", err
}
body, err = io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (a *ASClient) CreateDigitalObject(repositoryId int, dao DigitalObject) (string, error) {
endpoint := fmt.Sprintf("/repositories/%d/digital_objects", repositoryId)
body, err := json.Marshal(dao)
if err != nil {
return "", err
}
response, err := a.post(endpoint, true, string(body))
if err != nil {
return "", err
}
body, err = io.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (a *ASClient) DeleteDigitalObject(repositoryId int, doId int) (string, error) {
doURI := fmt.Sprintf("/repositories/%d/digital_objects/%d", repositoryId, doId)
return a.DeleteDigitalObjectFromURI(doURI)
}
func (a *ASClient) DeleteDigitalObjectFromURI(doURI string) (string, error) {
response, err := a.delete(doURI)
if err != nil {
return "", err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return string(body), err
}
return string(body), nil
}
func (a *ASClient) GetRandomDigitalObject() (int, int, error) {
var repositoryID = 0
var digitalObjectID = 0
repositoryIDs, err := a.GetRepositories()
if err != nil {
return repositoryID, digitalObjectID, err
}
repositoryID = repositoryIDs[rGen.Intn(len(repositoryIDs))]
digitalObjectIDs, err := a.GetDigitalObjectIDs(repositoryID)
if err != nil {
return repositoryID, digitalObjectID, err
}
digitalObjectID = digitalObjectIDs[rGen.Intn(len(digitalObjectIDs))]
return repositoryID, digitalObjectID, nil
}
func (a *ASClient) GetDigitalObjectIDsForResource(repositoryId int, resourceId int) ([]string, error) {
doURIs := []string{}
aoURIs, err := a.GetArchivalObjectsForResource(repositoryId, resourceId)
if err != nil {
return doURIs, err
}
for _, aoURI := range aoURIs {
_, aoId, _ := URISplit(aoURI)
ao, err := a.GetArchivalObject(repositoryId, aoId)
if err != nil {
return doURIs, err
}
for _, instance := range ao.Instances {
if instance.InstanceType == "digital_object" {
doURIs = append(doURIs, instance.DigitalObject["ref"])
}
}
}
return doURIs, nil
}
func (do DigitalObject) ContainsUseStatement(role string) bool {
for _, version := range do.FileVersions {
if version.UseStatement == role {
return true
}
}
return false
}
func (a *ASClient) GetDigitalObjectIDsForArchivalObjectFromURI(aoURI string) ([]string, error) {
doURIs := []string{}
ao, err := a.GetArchivalObjectFromURI(aoURI)
if err != nil {
return doURIs, err
}
for _, instance := range ao.Instances {
if instance.InstanceType == "digital_object" {
doURIs = append(doURIs, instance.DigitalObject["ref"])
}
}
return doURIs, nil
}