-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphaidra-api.go
164 lines (128 loc) · 4.67 KB
/
phaidra-api.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"go.uber.org/zap"
"net/http"
"net/url"
"phaidra-connect/domain"
"strings"
)
func createPhaidraObject(conf config, metadata domain.PhaidraMetadata) error {
apiPath := "/api/resource/create"
conf.log.Debug("creating object in Phaidra", zap.String("apiPath", apiPath))
hydratedMetadata, err := hydratePhaidraObject(conf, metadata)
if err != nil {
return err
}
metadataBuf := new(bytes.Buffer)
err = conf.template.Execute(metadataBuf, hydratedMetadata)
if err != nil {
return err
}
formData := url.Values{
"metadata": {metadataBuf.String()},
}
request, err := http.NewRequest(http.MethodPost, conf.phaidraHost+apiPath, strings.NewReader(formData.Encode()))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.SetBasicAuth(conf.phaidraUser, conf.phaidraPassword)
res, err := http.DefaultClient.Do(request)
if err != nil {
return err
}
resBody := new(bytes.Buffer)
_, err = resBody.ReadFrom(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("error creating object in Phaidra: %s", resBody.String())
}
conf.log.Debug("created object in Phaidra", zap.String("response", resBody.String()), zap.String("apiPath", apiPath))
return nil
}
func searchPhaidraOefos(conf config, oefos string) (*domain.PhaidraOefosMetadata, error) {
apiPath := "/api/vocabulary?uri=oefos2012"
conf.log.Debug("retrieving OEFOS from Phaidra", zap.String("oefos", oefos), zap.String("apiPath", apiPath))
request, err := http.NewRequest(http.MethodGet, conf.phaidraHost+apiPath, nil)
if err != nil {
return nil, err
}
request.SetBasicAuth(conf.phaidraUser, conf.phaidraPassword)
res, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
resBody := new(bytes.Buffer)
_, err = resBody.ReadFrom(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error searching OEFOS in Phaidra: %s", resBody.String())
}
root := new(domain.PhaidraVocabularyRoot)
err = json.Unmarshal(resBody.Bytes(), &root)
if err != nil {
return nil, err
}
vocabulary, deLabel, enLabel := root.TreeSearch(oefos)
if vocabulary == nil {
return nil, fmt.Errorf("OEFOS %s not found in Phaidra", oefos)
}
meta := new(domain.PhaidraOefosMetadata)
meta.FullLabel = []domain.PhaidraMetadataKeyword{
{Value: deLabel, Lang: domain.PhaidraMetadataKeywordLangDE},
{Value: enLabel, Lang: domain.PhaidraMetadataKeywordLangEN},
}
meta.ExactMatch = vocabulary.Id
meta.Notation = oefos
meta.PrefLabel = []domain.PhaidraMetadataKeyword{
{Value: vocabulary.Labels[domain.PhaidraMetadataKeywordLangDE], Lang: domain.PhaidraMetadataKeywordLangDE},
{Value: vocabulary.Labels[domain.PhaidraMetadataKeywordLangEN], Lang: domain.PhaidraMetadataKeywordLangEN},
}
conf.log.Debug("retrieved OEFOS from Phaidra", zap.String("label", enLabel), zap.String("exactMatch", meta.ExactMatch), zap.String("oefos", oefos), zap.String("apiPath", apiPath))
return meta, nil
}
func searchPhaidraOrgUnit(conf config, orgUnitId string) (*domain.PhaidraOrgUnitMetadata, error) {
apiPath := "/api/directory/org_get_units"
conf.log.Debug("retrieving org unit from Phaidra", zap.String("orgUnitId", orgUnitId), zap.String("apiPath", apiPath))
request, err := http.NewRequest(http.MethodGet, conf.phaidraHost+apiPath, nil)
if err != nil {
return nil, err
}
request.SetBasicAuth(conf.phaidraUser, conf.phaidraPassword)
res, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
resBody := new(bytes.Buffer)
_, err = resBody.ReadFrom(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error searching org unit in Phaidra: %s", resBody.String())
}
root := new(domain.PhaidraOrgUnitRoot)
err = json.Unmarshal(resBody.Bytes(), &root)
if err != nil {
return nil, err
}
orgUnit := root.TreeSearch(orgUnitId)
if orgUnit == nil {
return nil, fmt.Errorf("org unit %s not found in Phaidra", orgUnitId)
}
meta := new(domain.PhaidraOrgUnitMetadata)
meta.FullLabel = []domain.PhaidraMetadataKeyword{
{Value: orgUnit.PrefLabel[domain.PhaidraMetadataKeywordLangDE], Lang: domain.PhaidraMetadataKeywordLangDE},
{Value: orgUnit.PrefLabel[domain.PhaidraMetadataKeywordLangEN], Lang: domain.PhaidraMetadataKeywordLangEN},
}
meta.ExactMatch = strings.ReplaceAll(orgUnit.Id, "/", "\\/")
conf.log.Debug("retrieved org unit from Phaidra", zap.String("label", orgUnit.PrefLabel[domain.PhaidraMetadataKeywordLangEN]), zap.String("exactMatch", meta.ExactMatch), zap.String("orgUnitId", orgUnitId), zap.String("apiPath", apiPath))
return meta, nil
}