Skip to content

Commit

Permalink
add support for inveniordm updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 15, 2024
1 parent c0ba92b commit b2db2a7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var versionCmd = &cobra.Command{
Short: "Print the version number of commonmeta",
Long: `All software has versions. This is commonmeta's`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Commonmeta v0.5.23 -- HEAD")
cmd.Println("Commonmeta v0.5.24 -- HEAD")
},
}

Expand Down
63 changes: 51 additions & 12 deletions inveniordm/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,22 @@ func PostAll(list []commonmeta.Data, host string, apiKey string) ([]byte, error)
}
}

// remove InvenioRDM rid after storing it
type Draft struct {
ID string `json:"id"`
}
var draft Draft
var RIDIndex int
for i, v := range data.Identifiers {
if v.IdentifierType == "RID" && v.Identifier != "" {
draft.ID = v.Identifier
RIDIndex = i
}
if RIDIndex != 0 {
data.Identifiers = slices.Delete(data.Identifiers, i, i)
}
}

// workaround until JSON schema validation is implemented
// check for required fields
if inveniordm.Metadata.Title == "" {
Expand All @@ -443,17 +459,44 @@ func PostAll(list []commonmeta.Data, host string, apiKey string) ([]byte, error)
return nil, err
}

// create draft record
requestURL := fmt.Sprintf("https://%s/api/records", host)
req, _ := http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(output))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + apiKey},
}
var requestURL string
var req *http.Request
var resp *http.Response
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if draft.ID != "" {
// create draft record from published record if rid is provided
requestURL = fmt.Sprintf("https://%s/api/records/%s/draft", host, draft.ID)
req, _ = http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(output))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + apiKey},
}
resp, err = client.Do(req)
if err != nil {
return nil, err
}

// update draft record
requestURL = fmt.Sprintf("https://%s/api/records/%s/draft", host, draft.ID)
req, _ = http.NewRequest(http.MethodPut, requestURL, bytes.NewReader(output))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + apiKey},
}
resp, err = client.Do(req)
} else {
// otherwise create draft record
requestURL = fmt.Sprintf("https://%s/api/records", host)
req, _ = http.NewRequest(http.MethodPost, requestURL, bytes.NewReader(output))
req.Header = http.Header{
"Content-Type": {"application/json"},
"Authorization": {"Bearer " + apiKey},
}
resp, err = client.Do(req)
}

if err != nil {
return nil, err
}
Expand All @@ -465,10 +508,6 @@ func PostAll(list []commonmeta.Data, host string, apiKey string) ([]byte, error)
}

// publish draft record
type Draft struct {
ID string `json:"id"`
}
var draft Draft
err = json.Unmarshal(body, &draft)
if err != nil {
return nil, err
Expand Down

0 comments on commit b2db2a7

Please sign in to comment.